Skip to content

fishify functions/completions #6520

Open

Description

@ammgws

The following functions/completions use sed etc in some way that may be able to be sensibly replaced by fish builtins such as string. Opening this issue so can track which ones have been completed.

sed:

  • completions/abook.fish (eba099b)
  • completions/apropos.fish (69afb1b)
  • completions/bundle.fish
  • completions/emerge.fish
  • completions/eselect.fish
  • completions/highlight.fish
  • completions/killall.fish
  • completions/mplayer.fish
  • completions/mvn.fish
  • completions/netctl.fish
  • completions/open.fish (b1bbb58)
  • completions/pactl.fish
  • completions/perl.fish
  • completions/setxkbmap.fish
  • completions/ssh.fish
  • completions/vared.fish (7d89cad)
  • functions/__fish_complete_gpg_key_id.fish
  • functions/__fish_make_completion_signals.fish
  • functions/__fish_print_modules.fish
  • functions/__fish_print_packages.fish
  • functions/__fish_pwd.fish
  • functions/fish_svn_prompt.fish

awk:

  • completions/adb.fish
  • completions/cwebp.fish
  • completions/ezjail-admin.fish
  • completions/find.fish
  • completions/grunt.fish
  • completions/helm.fish
  • completions/heroku.fish
  • completions/lsusb.fish
  • completions/make.fish
  • completions/p4.fish
  • completions/psql.fish
  • completions/rmmod.fish (6004d04)
  • completions/vim-addons.fish
  • functions/__fish_complete_convert_options.fish
  • functions/__fish_complete_man.fish
  • functions/__fish_describe_command.fish
  • functions/__fish_print_addresses.fish
  • functions/__fish_print_packages.fish

tr:

  • completions/aura.fish
  • completions/grunt.fish
  • completions/pactree.fish
  • completions/pkgfile.fish
  • completions/rfkill.fish
  • completions/rsync.fish
  • functions/__fish_make_completion_signals.fish
  • functions/__fish_parse_configure.fish
  • functions/__fish_print_packages.fish
  • functions/__fish_print_xdg_desktop_file_ids.fish
  • functions/fish_svn_prompt.fish

echo $xx | string (can replace with string -- $xx):

  • completions/j.fish
  • completions/unzip.fish

cat:

  • completions/gradle.fish (0de217d)
  • completions/zypper.fish
  • functions/__fish_complete_ant_targets.fish
  • functions/__fish_parse_configure.fish
  • functions/__fish_portage_print_repository_names.fish
  • functions/__fish_portage_print_repository_paths.fish
  • functions/__fish_print_packages.fish
  • functions/__fish_print_xdg_mimetypes.fish
  • functions/psub.fish

tail:

  • completions/cargo.fish (9bf5dfd)
  • completions/ezjail-admin.fish
  • completions/kcmshell5.fish
  • completions/phpunit.fish
  • functions/__fish_complete_pids.fish
  • functions/__fish_print_packages.fish

head:

  • completions/apt.fish
  • completions/j.fish
  • completions/ssh.fish
  • completions/zpool.fish
  • functions/__fish_complete_zfs_rw_properties.fish
  • functions/fish_npm_helper.fish

grep:

cut:

  • completions/amixer.fish (30459b0)
  • completions/bat.fish (f036d01)
  • completions/fossil.fish
  • completions/fuser.fish (30459b0)
  • completions/journalctl.fish (30459b0)
  • completions/kill.fish (c1140bc)
  • completions/machinectl.fish
  • completions/modprobe.fish
  • completions/pactree.fish
  • completions/pkgfile.fish
  • completions/svn.fish (30459b0)
  • completions/tokei.fish (30459b0)
  • completions/useradd.fish (30459b0)
  • completions/xprop.fish
  • functions/__fish_complete_groups.fish
  • functions/__fish_complete_gpg_user_id.fish
  • functions/__fish_complete_users.fish (4943ab6)
  • functions/__fish_crux_packages.fish
  • functions/__fish_is_zfs_feature_enabled.fish
  • functions/__fish_print_packages.fish
  • functions/__fish_systemctl_services.fish
  • functions/fish_svn_prompt.fish
  • functions/help.fish (eaf313f)

The following will not be implemented for now

test -n (replace with string length -q). (see here):

  • share/config.fish
  • share/functions/psub.fish
  • tests/test_util.fish
  • share/functions/__fish_config_interactive.fish
  • share/functions/vared.fish
  • share/functions/__fish_abbr_old.fish
  • share/functions/__fish_cancel_commandline.fish
  • share/functions/_fish_systemctl.fish
  • share/functions/__fish_complete_man.fish
  • share/functions/fish_clipboard_paste.fish
  • share/functions/help.fish
  • share/functions/history.fish
  • share/functions/fish_clipboard_copy.fish
  • share/functions/__fish_whatis_current_token.fish
  • share/functions/fish_git_prompt.fish
  • share/functions/umask.fish
  • share/functions/__fish_print_hostnames.fish
  • share/functions/__fish_not_contain_opt.fish
  • share/functions/pushd.fish
  • share/completions/sudo.fish
  • share/functions/__fish_print_help.fish
  • share/functions/__fish_contains_opt.fish
  • share/completions/virsh.fish
  • share/completions/pacman.fish
  • share/functions/trap.fish
  • share/completions/df.fish
  • share/completions/doas.fish
  • share/completions/p4.fish
  • share/completions/aura.fish
  • share/completions/dconf.fish
  • share/completions/sed.fish
  • share/completions/yaourt.fish
  • share/completions/pacaur.fish
  • share/completions/tar.fish
  • share/completions/adb.fish
  • share/completions/magento.fish
  • share/completions/env.fish
  • tests/checks/complete.fish
  • share/tools/web_config/sample_prompts/robbyrussell.fish
  • share/tools/web_config/sample_prompts/nim.fish
  • share/tools/web_config/sample_prompts/debian_chroot.fish
  • share/tools/web_config/sample_prompts/sorin.fish

Activity

  1. added this to the fish-future milestone on Jan 21, 2020
  2. changed the title [-]Stringify functions/completions[/-] [+]fishify functions/completions[/+] on Jan 22, 2020
  3. faho commented on Jan 22, 2020

    @faho
    Member

    Note that sometimes it might make sense to keep the external tools - e.g. in some cases awk is so fast you might think it was trying to stop Dr Eggman.

    It also makes sense to show that grep et al work in fish in the tutorial. Most people will also keep on using them in most cases, and that's okay.

    I mainly see two uses for using our builtins (given that we're already dog-fooding plenty):

    • In some cases it's faster than spinning up an external process (tho this advantage has gotten quite a bit smaller with us getting faster at doing that)

    • Increasing portability - some systems have e.g. sed implementations that suck, so being able to use a tool we control is a boon

  4. mqudsi commented on Feb 2, 2020

    @mqudsi
    Contributor

    The reason why I intentionally still use sed and grep in some fish scripts: they don't buffer (the entire content, at any rate). Until buffering is fixed, we should be very careful when replacing these with string equivalents.

  5. 6A61736F6E206E61646572 commented on Feb 2, 2020

    @6A61736F6E206E61646572
    Contributor

    @mqudsi For educational purposes, do you have a (contrived) example for the above situations where sed/grep would be better suited?

  6. krobelus commented on Feb 3, 2020

    @krobelus
    Contributor

    The buffering matters when you process a sizeable amount of data (megabytes).
    Compared to their string equivalent, awk or perl make this example from tshark.fish almost three times faster, probably because of buffering.

    $ time command tshark -G fields | string replace -r '[^\t]*\t([^\t]*)\t([^\t]*)' '$2\t$1' > /dev/null
    ________________________________________________________
    Executed in    1.46 secs   fish           external 
       usr time    2.09 secs  1698.90 millis  387.31 millis 
       sys time    0.33 secs  237.31 millis   90.84 millis 
    
    
    time command tshark -G fields | perl -ne '/[^\t]*\t([^\t]*)\t([^\t\n]*)/; print "$2\t$1\n";' >/dev/null
    
    ________________________________________________________
    Executed in  557.14 millis    fish           external 
       usr time  619.38 millis    1.90 millis  617.48 millis 
       sys time  135.60 millis    0.51 millis  135.09 millis
  7. 6A61736F6E206E61646572 commented on Feb 3, 2020

    @6A61736F6E206E61646572
    Contributor

    Thanks. I was just wondering how often one would come across a situation like that in our completion/etc fish scripts.

  8. mqudsi commented on Feb 3, 2020

    @mqudsi
    Contributor

    It would be a big deal if we ever got async completions, but I'm not sure which is further off (proper simultaneous execution of piped contexts or async reporting of available completions).

    But there is a considerable amount of cases where buffering artificially constrains even non-interactive processes. For example, just yesterday I wanted to pipe the (reversed) output of a 68 GiB "since the start of time" log file, perform a manipulation on each line, then select the most recent n events predicated on the value of the manipulated line - this is something that could (and does) often come up in writing completions; here's what my case looked like:

    tac /var/log/foo.log | string replace -rf '.*\b([^\b]+bot[^\b]+).*' '\$1' | head -n20

    The length of /var/log/foo.log should not matter, as I want to stop after 20 matches. But with buffering, fish needs to process the entirety of the log file and only then does it pass anything along to head, which only uses the smallest sliver of what string generated.

    The only way to do this in fish (aside from creating and using a pipe then running the commands in the background) would have been to replace the no-brainer one-liner above with an actual "script" that read a line at a time, processed each line, incremented a counter, and then bailed when the desired count was reached.

  9. faho commented on Feb 3, 2020

    @faho
    Member

    But there is a considerable amount of cases where buffering artificially constrains even non-interactive processes.

    Sure. If you're piping gigabytes.

    However, I wouldn't overstate how often this happens, or how important this is.

    E.g. @krobelus example has 204000 lines. Put a head -n 10000 in there, and the difference all but disappears. Most completions deal with much smaller data sets.

    Also that's with perl or awk. Try sed instead and we're much faster.

    Please use our tools by default, unless you have a good reason (yes, performance counts) not to, and in that case please make it work according to the standard, so it works everywhere.

  10. mqudsi commented on Feb 3, 2020

    @mqudsi
    Contributor

    Put a head -n 10000 in there, and the difference all but disappears. Most completions deal with much smaller data sets.

    Which is great, unless you want to limit based off the final count after manipulation rather than before.

    But, yes. Definitely prefer string where it's an option.

  11. Mazorius commented on Apr 6, 2020

    @Mazorius
    Contributor

    With the following PR I improved gradle completion: #6864

    This also removes usage of cat, echo and more.

  12. reopened this on Apr 6, 2020
  13. reopened this on Feb 13, 2022
  14. Nahor commented on Aug 4, 2026

    @Nahor
    Contributor

    Late to the discussion but:

    • process creation is really slow on Cygwin/MSYS2 (notably because they have to emulate fork()):

      $ time for i in (seq 1 1000); builtin true; end
      
      ________________________________________________________
      Executed in   19.31 millis    fish           external
         usr time   15.00 millis    0.00 micros   15.00 millis
         sys time    0.00 millis    0.00 micros    0.00 millis
      
      $ time for i in (seq 1 1000); command true; end
      
      ________________________________________________________
      Executed in   12.42 secs    fish         external
         usr time    4.96 secs    0.39 secs    4.57 secs
         sys time    5.05 secs    1.00 secs    4.05 secs
      

      so avoiding using external commands on MSYS2 would be a significant benefit in most scenarios, especially when things need to be fast (completion, prompt).
      For comparision, in WSL:

      $ time for i in (seq 1 1000); builtin true; end
      
      ________________________________________________________
      Executed in    2.01 millis    fish           external
         usr time    1.12 millis  489.00 micros  629.00 micros
         sys time    0.96 millis  960.00 micros    0.00 micros
      
      $ time for i in (seq 1 1000); command true; end
      
      ________________________________________________________
      Executed in  773.17 millis    fish           external
         usr time  500.62 millis   81.92 millis  418.70 millis
         sys time  347.73 millis  289.33 millis   58.40 millis
    • @krobelus example is in favor of string on my machine under MSYS2

      $ time /ucrt64/bin/tshark -G fields | string replace -r '[^\t]*\t([^\t]*)\t([^\t]*).*' '$2\t$1' > /dev/null
      
      ________________________________________________________
      Executed in  507.19 millis    fish           external
         usr time  250.00 millis  250.00 millis    0.00 millis
         sys time   46.00 millis   31.00 millis   15.00 millis
      
      $ time /ucrt64/bin/tshark -G fields | perl -ne '/[^\t]*\t([^\t]*)\t([^\t\n]*)/; print "$2\t$1\n";' >/dev/null
      
      ________________________________________________________
      Executed in    2.27 secs      fish           external
         usr time   93.00 millis    0.00 millis   93.00 millis
         sys time  109.00 millis   16.00 millis   93.00 millis
      
      $ time /ucrt64/bin/tshark -G fields | awk -F\t '{print $3"\t"$2}' > /dev/null
      
      ________________________________________________________
      Executed in    2.88 secs      fish           external
         usr time  125.00 millis    0.00 millis  125.00 millis
         sys time   93.00 millis   16.00 millis   77.00 millis
      

      but then also I get this:

      $ time /ucrt64/bin/tshark -G fields > /dev/null
      
      ________________________________________________________
      Executed in   19.83 secs      fish           external
         usr time    0.00 micros    0.00 micros    0.00 micros
         sys time    0.00 micros    0.00 micros    0.00 micros
      
      $ time /ucrt64/bin/tshark -G fields > tmp.txt
      
      ________________________________________________________
      Executed in  502.98 millis    fish           external
         usr time    0.00 micros    0.00 micros    0.00 micros
         sys time    0.00 micros    0.00 micros    0.00 micros
      

      so there may be some issues with using tshark itself and it may not be the best example to use in the first place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions