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

Re: coloring a substitution



On Tue, Nov 8, 2022 at 9:57 PM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>
> I'm seeing if I can make my function 100% zsh native.  The 'sed' works.
> '$color' is just incrementing integers in a for loop starting at 31 (red):
>
>   recent[$aa]=$( print $recent[$aa] | sed -r
> "s/$filter/\x1b\[$color;1m$filter\x1b\[0m/Ig" )

I'm assuming that:

- `print` is equivalent to `print -r --` here
- $filter doesn't have any metacharacters
- it's intentional that $filter is matched case-insensitively and
whatever matches is replaced literally by $filter

The last assumption looks the most weird to me and there is a chance
that this isn't what you intended the code to do.

> ... so I'm trying to get zsh string replacement to do the same job:
>
> recent[$aa]=${recent[$aa]/\x1b\[$color;1m$filter\x1b\[0m/}

Try this:

  recent[$aa]=${recent[$aa]//(#i)$filter/$'\e['$color';1m'$filter$'\e[0m'}

Roman.




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