flatpak
将输出打印为表格:
$ flatpak search firefox
Name Description Application ID Version Branch Remotes
Firefox Fast, Private & Safe Web Browser org.mozilla.firefox 116.0.2 stable flathub
Mullvad B… Free the internet from mass surveillance …ullvad.MullvadBrowser 12.5.2 stable flathub
Mojave-GTK Mojave-Style Theme for GTK Flatpak Applications …tk3theme.Mojave-light 0.1 3.22 flathub
但是,如果它通过管道传输到某些内容,例如cat
or grep
,则输出不再格式化为表,并且表头消失:
$ flatpak search firefox | cat
Firefox Fast, Private & Safe Web Browser org.mozilla.firefox 116.0.2 stable flathub
Mullvad Browser Free the internet from mass surveillance net.mullvad.MullvadBrowser 12.5.2 stableflathub
Mojave-GTK Mojave-Style Theme for GTK Flatpak Applications org.gtk.Gtk3theme.Mojave-light 0.1 3.22 flathub
即使输出通过管道传输到另一个进程,如何强制 flatpak 将输出格式化为表格?
答案1
检查系统调用的内容flatpak search
表明,flatpak 命令检查标准输出是否为实际终端,并获取其宽度(以字符为单位),以布局输出(您可以测试这一点:创建一个较小的控制台窗口,然后运行相同的命令)flatpak search
再次命令:它不包含完全相同的输出!)。
由于重定向到文件没有“可显示列数”,因此 Flatpak 不会尝试执行与显示相同的良好布局。
嘿弗利姆,这听起来很多就像您确实不应该使用例如flatpak search
产生的“人类漂亮”输出一样,而是自己调用底层程序。然而:
flatpak
Tab默认情况下,它会使用制表符( 、↹、 ,无论您如何称呼它)分隔输出中的列\t
,这使您可以轻松地分隔各个列并按照您想要的方式自行进行布局。例如:
COLUMNS=application,version,name
flatpak search --columns "${COLUMNS}" gimp | mlr --itsv --opprint --barred --implicit-csv-header label "${COLUMNS}"
产量
+------------------------------------+---------+--------------------------------+
| application | version | name |
+------------------------------------+---------+--------------------------------+
| org.gimp.GIMP.Manual | 2.10 | GIMP User Manual |
| org.gimp.GIMP | 2.10.34 | GNU Image Manipulation Program |
| org.gimp.GIMP.Plugin.Resynthesizer | 2.0.3 | Resynthesizer |
| org.gimp.GIMP.Plugin.Resynthesizer | 2.0.3 | Resynthesizer |
| org.gimp.GIMP.Plugin.Lensfun | 0.2.4 | GimpLensfun |
| org.gimp.GIMP.Plugin.Lensfun | 0.2.4 | GimpLensfun |
| org.gimp.GIMP.Plugin.Fourier | 0.4.3 | Fourier |
| org.gimp.GIMP.Plugin.Fourier | 0.4.3 | Fourier |
| org.gimp.GIMP.Plugin.BIMP | 2.6 | BIMP |
| org.gimp.GIMP.Plugin.BIMP | 2.5 | BIMP |
| org.gimp.GIMP.Plugin.LiquidRescale | 0.7.2 | LiquidRescale |
| org.gimp.GIMP.Plugin.LiquidRescale | 0.7.2 | LiquidRescale |
| org.gimp.GIMP.Plugin.GMic | 3.2.6 | G'MIC |
| org.gimp.GIMP.Plugin.GMic | 2.9.6 | G'MIC |
| org.gimp.GIMP.Plugin.FocusBlur | 3.2.6 | FocusBlur |
| org.gimp.GIMP.Plugin.FocusBlur | 3.2.6 | FocusBlur |
| org.gabmus.swatch | 0.1 | Swatch |
| com.github.unrud.djpdf | 0.4.3 | Scans to PDF |
+------------------------------------+---------+--------------------------------+
¹ 我是如何做到的:
快速strace -o /tmp/flatpak.strace flatpak search
阅读 /tmp/flatpak.strace:
grep ioctl /tmp/strace
给我们
ioctl(1, TIOCGWINSZ, {ws_row=61, ws_col=232, ws_xpixel=2552, ws_ypixel=1403}) = 0
ioctl(1, TCGETS, {c_iflag=ICRNL|IXON|IUTF8, c_oflag=NL0|CR0|TAB0|BS0|VT0|FF0|OPOST|ONLCR, c_cflag=B38400|CS8|CREAD, c_lflag=ISIG|ICANON|ECHO|ECHOE|ECHOK|IEXTEN|ECHOCTL|ECHOKE, ...}) = 0