Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: optimal expansions?





On 2024-04-20 00:42, Roman Perepelitsa wrote:

Hafta start using lines like this all the time.  You guys never start a function without one of these 'emulate' instructions -- make sure that the func has the environment it needs.
emulate zsh -o no_unset -o pipe_fail -o err_exit
    () {
      local last_pkg pkg file
So you break it into words automatically here, and any spaced filenames end up in 'file' anyway! 
      apt-file search "$1" | while read -r pkg file; do
        if [[ $pkg != $last_pkg ]]; then
          [[ -n $last_pkg ]] && print
          last_pkg=$pkg
... this line is Basque to me.  It works but I have no idea how.  Anyway that's for me to research.  It baffles me that there's color codes in there.
          print -P "%B%2F${pkg//\%/%%}%f%b"
        fi
Seems we want '-r' most of the time.  And the '--' should always be there.  Now that I know not to use newlines for splitting.  And of course that obviates most of my '(@f)'s too.
        print -r -- $file
      done
    } "$@"

This implementation fixes a couple of bugs in addition to those
mentioned by Lawrence: it gets rid of the empty line at start of the
output, and leaves whitespace in file names unchanged.
To pick nits I want the leading empty line.  But I'm betting your code will be faster.  I'll keep Lawrence's edits until such times as I understand that 'print -P' tho. 





Messages sorted by: Reverse Date, Date, Thread, Author