0

flatpak prints output as a table:

$ 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

However, if it gets piped to something, like cat or grep, then the output is no longer formatted as a table, and the table headers disappear:

$ 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

How do I force flatpak to format the output as a table even when the output is piped to another process?

2
  • Redirect the output to a file, and then dump it using a hex editor. (I like bvi..) Look for linefeed characters (hex 0A).
    – RonJohn
    Commented Aug 14, 2023 at 14:42
  • The tool likely detects that the output is or is not a terminal and changes its behaviour accordingly. This is a fairly common thing for tools that are expected to be run in both interactive and non-interactive scenarios to do. From reading the manual, it's not immediately apparent that this behaviour can be changed. I'm not turning this into an answer as I'm not a user of flatpack.
    – Kusalananda
    Commented Aug 14, 2023 at 14:58

1 Answer 1

1

Inspection of what system calls flatpak search does shows that the flatpak command checks the standard output for being an actual terminal, and gets its width in characters¹, to lay out the output (you can test that: make a smaller console window, and run the the same flatpak search command again: it's not containing the exact same output!).

Because redirection into a file doesn't have a "number of displayable columns", flatpak doesn't try to do the same nice layout as for displaying.

Hey Flimm, this sounds a lot like you really shouldn't be using the "human-pretty" output that e.g. flatpak search produces, but call the underlying programs yourself. However:

flatpak does by default separate the columns in its output with tabulators (Tab, , \t, however you call it), and that makes it easy for you to just separate the individual columns and do the layouting yourself, the way you want it. For example:

COLUMNS=application,version,name
flatpak search --columns "${COLUMNS}" gimp | mlr --itsv --opprint --barred --implicit-csv-header label "${COLUMNS}"

yields

+------------------------------------+---------+--------------------------------+
| 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                   |
+------------------------------------+---------+--------------------------------+



¹ How I did that:

A quick strace -o /tmp/flatpak.strace flatpak search, followed by reading /tmp/flatpak.strace:

grep ioctl /tmp/strace

giving us

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
1
  • Thank you for going above and beyond what I was expecting!
    – Flimm
    Commented Aug 14, 2023 at 18:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .