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

Re: optimal expansions?



On Fri, Apr 19, 2024 at 11:22 PM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>
> Script:
>
>     grn=$'\e[32;1m'
>     nrm=$'\e[0m'
>
>     var=( "${(@f)$(apt-file search $1)}" )
>     targ=
>     var2=()
>
>     for ((i=1; i<=$#var; i++ )); do
>         if [[ "$targ" != "${${=var[i]}[1]}" ]]; then
>             targ="${${=var[i]}[1]}"
>             var2+="\n${grn}${${=var[i]}[1]}${nrm}" # Copy first word of line.
>         fi
>         var2+="${${=var[i]}[2,-1]}" # Copy the rest of the line no matter how many words.
>     done
>
>     print -l "$var2[@]"

I'd do it like this:

    #!/usr/bin/env zsh

    emulate zsh -o no_unset -o pipe_fail -o err_exit

    () {
      local last_pkg pkg file
      apt-file search "$1" | while read -r pkg file; do
        if [[ $pkg != $last_pkg ]]; then
          [[ -n $last_pkg ]] && print
          last_pkg=$pkg
          print -P "%B%2F${pkg//\%/%%}%f%b"
        fi
        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.

Roman.




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