From c2ecfe60ac286236b292cc896f5a503b790d5dca Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Wed, 22 May 2024 13:30:15 -0500 Subject: [PATCH] completions/flatpak: Fix compatibility with older versions Filter out ansi control characters and pretty formatting from older versions that don't detect the absence of a tty. Closes #10514. --- share/completions/flatpak.fish | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/share/completions/flatpak.fish b/share/completions/flatpak.fish index 3ead2bf10..e34eda921 100644 --- a/share/completions/flatpak.fish +++ b/share/completions/flatpak.fish @@ -48,10 +48,20 @@ complete -f -c flatpak -n "not __fish_seen_subcommand_from $commands" -a mask -d complete -f -c flatpak -n "not __fish_seen_subcommand_from $commands" -a permission-remove -d 'Remove item from permission store' complete -f -c flatpak -n "not __fish_seen_subcommand_from $commands" -a permission-set -d 'Set permissions' -complete -f -c flatpak -n "__fish_seen_subcommand_from run" -a "(flatpak list --app --columns=application,name)" -complete -f -c flatpak -n "__fish_seen_subcommand_from info uninstall" -a "(flatpak list --columns=application,name)" -complete -f -c flatpak -n "__fish_seen_subcommand_from enter kill" -a "(flatpak ps --columns=instance,application)" -complete -f -c flatpak -n "__fish_seen_subcommand_from remote-info remote-ls remote-modify remote-delete" -a "(flatpak remotes --columns=name,title)" +# Ancient versions of flatpak seem to assume output is always a tty and emit pretty-formatted output, with +# a variable number of spaces for alignment, trailing \CR, and ANSI color/formatting sequences. This function is akin +# to `flatpak $argv[1] $argv[2..]` but sanitizes the output to make it completion-friendly. +function __fish_flatpak + # We can't rely on the first line being the headers because modern flatpak will omit it when running in a "subshell". + # Based off our invocations, we expect the first column of actual output to never contain a capitalized letter + # (unlike the header row). + flatpak $argv | string replace -rf '^([^A-Z]+)(?: +|\t)(.*?)\s*$' '$1\t$2' +end + +complete -f -c flatpak -n "__fish_seen_subcommand_from run" -a "(__fish_flatpak list --app --columns=application,name)" +complete -f -c flatpak -n "__fish_seen_subcommand_from info uninstall" -a "(__fish_flatpak list --columns=application,name)" +complete -f -c flatpak -n "__fish_seen_subcommand_from enter kill" -a "(__fish_flatpak ps --columns=instance,application)" +complete -f -c flatpak -n "__fish_seen_subcommand_from remote-info remote-ls remote-modify remote-delete" -a "(__fish_flatpak remotes --columns=name,title)" # Note that "remote-ls" opens an internet connection, so we don't want to complete "install" # Plenty of the other stuff is too free-form to complete (e.g. remote-add).