zsh-users
 help / color / mirror / code / Atom feed
* coloring a substitution
@ 2022-11-08 20:56 Ray Andrews
  2022-11-08 21:10 ` Roman Perepelitsa
  0 siblings, 1 reply; 9+ messages in thread
From: Ray Andrews @ 2022-11-08 20:56 UTC (permalink / raw)
  To: Zsh Users

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" )

... so I'm trying to get zsh string replacement to do the same job:

recent[$aa]=${recent[$aa]/\x1b\[$color;1m$filter\x1b\[0m/}

... but it's not working, can it be repaired?  I'll bet it can, it's 
just a question of getting the backslashes right or some other tweak in 
the grammar.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: coloring a substitution
  2022-11-08 20:56 coloring a substitution Ray Andrews
@ 2022-11-08 21:10 ` Roman Perepelitsa
  2022-11-08 23:49   ` Ray Andrews
  0 siblings, 1 reply; 9+ messages in thread
From: Roman Perepelitsa @ 2022-11-08 21:10 UTC (permalink / raw)
  To: Ray Andrews; +Cc: Zsh Users

On Tue, Nov 8, 2022 at 9:57 PM Ray Andrews <rayandrews@eastlink.ca> 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.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: coloring a substitution
  2022-11-08 21:10 ` Roman Perepelitsa
@ 2022-11-08 23:49   ` Ray Andrews
  2022-11-09  0:11     ` Bart Schaefer
  2022-11-09  8:19     ` Roman Perepelitsa
  0 siblings, 2 replies; 9+ messages in thread
From: Ray Andrews @ 2022-11-08 23:49 UTC (permalink / raw)
  To: zsh-users


On 2022-11-08 13:10, Roman Perepelitsa wrote:

> - `print` is equivalent to `print -r --` here 
Right!  It's one of those things that's easy to overlook until some 
weird name bites you.
>
>    recent[$aa]=${recent[$aa]//(#i)$filter/$'\e['$color';1m'$filter$'\e[0m'}

Therewego, > $'\e[' < replaces  > \x1b\[ <.

Thanks Roman.

BTW as it stands now my filtering issue on the other thread:

For globbing for files on the disk: ($zsh_case = '(#i)'):

     [ "$wild_msg" = 'WILD' ] \
                 && eval "wholedisk=( $zsh_case/**/*$1*(/N) )" \
                 || eval "wholedisk=( $zsh_case/**/$1(/N) )"

For string searches in the directory stack:

for (( aa = 1; aa <= $#cc; aa++ )); do

# These are full names, $1 may be only a partial match if we are WILD.
dirname=${cc[$aa]:t}

if [[ "$scope_msg" = 'BROAD'                  && $dirname = (#i)*$1* ]] \
|| [[ "$scope_msg" = 'Case INsensitive TAME' && $dirname:u = $1:u ]] \
|| [[ "$scope_msg" = 'Case Sensitive WILD'      && $dirname =~ $1 ]] \
|| [[ "$scope_msg" = 'EXACT'                  && $dirname = $1 ]]; then 
; # Do nothing, we have a match.
else cc[$aa]=    # Kill the line, it does not match.
fi
done

... any further improvements?

> Roman.
>


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: coloring a substitution
  2022-11-08 23:49   ` Ray Andrews
@ 2022-11-09  0:11     ` Bart Schaefer
  2022-11-09  0:32       ` Ray Andrews
  2022-11-09  8:19     ` Roman Perepelitsa
  1 sibling, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2022-11-09  0:11 UTC (permalink / raw)
  To: Ray Andrews; +Cc: zsh-users

On Tue, Nov 8, 2022 at 3:49 PM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> Therewego, > $'\e[' < replaces  > \x1b\[ <.

Actually $'\x1b[' would work too.  The important bit is that $'...'
triggers interpretation of those.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: coloring a substitution
  2022-11-09  0:11     ` Bart Schaefer
@ 2022-11-09  0:32       ` Ray Andrews
  0 siblings, 0 replies; 9+ messages in thread
From: Ray Andrews @ 2022-11-09  0:32 UTC (permalink / raw)
  To: zsh-users


On 2022-11-08 16:11, Bart Schaefer wrote:
> On Tue, Nov 8, 2022 at 3:49 PM Ray Andrews <rayandrews@eastlink.ca> wrote:
>> Therewego, > $'\e[' < replaces  > \x1b\[ <.
> Actually $'\x1b[' would work too.  The important bit is that $'...'
> triggers interpretation of those.

So sed figures out the interpretation of that escape by itself but zsh 
want's to make it explicit.  That's fine.  There's always the issue of 
what's literal unless made special vs what's special unless made literal 
and if you gain the one you lose the other. No free lunch.


>


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: coloring a substitution
  2022-11-08 23:49   ` Ray Andrews
  2022-11-09  0:11     ` Bart Schaefer
@ 2022-11-09  8:19     ` Roman Perepelitsa
  2022-11-11 22:24       ` Ray Andrews
  1 sibling, 1 reply; 9+ messages in thread
From: Roman Perepelitsa @ 2022-11-09  8:19 UTC (permalink / raw)
  To: Ray Andrews; +Cc: zsh-users

On Wed, Nov 9, 2022 at 12:50 AM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> For globbing for files on the disk: ($zsh_case = '(#i)'):
>
>      [ "$wild_msg" = 'WILD' ] \
>                  && eval "wholedisk=( $zsh_case/**/*$1*(/N) )" \
>                  || eval "wholedisk=( $zsh_case/**/$1(/N) )"

Try this instead:

  if [[ $wild_msg = WILD ]]; then
   wholedisk=( $~zsh_case/**/*$~1*(/N) )
  else
   wholedisk=( $~zsh_case/**/$~1(/N) )
  fi

These cases would more traditionally be called "partial match" and "full match".

> if [[ "$scope_msg" = 'BROAD'                  && $dirname = (#i)*$1* ]] \
> || [[ "$scope_msg" = 'Case INsensitive TAME' && $dirname:u = $1:u ]] \
> || [[ "$scope_msg" = 'Case Sensitive WILD'      && $dirname =~ $1 ]] \
> || [[ "$scope_msg" = 'EXACT'                  && $dirname = $1 ]]; then

Try this instead:

  if [[ $scope_msg = 'BROAD'                  && $dirname = (#i)*$~1* ]] ||
     [[ $scope_msg = 'Case INsensitive TAME' && $dirname = (#i)$~1 ]] ||
     [[ $scope_msg = 'Case Sensitive WILD'      && $dirname = *$~1* ]] ||
     [[ $scope_msg = 'EXACT'                  && $dirname = $~1 ]]; then

If this works, consider using more traditional names for these cases:

- case-insensitive partial
- case-insensitive full
- case-sensitive partial
- case-sensitive full

You can also shorten "case-insensitive" to "icase" and
"case-sensitive" to nothing. This is a commonly used naming
convention.

You can also use $~zsh_case in here.


Roman.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: coloring a substitution
  2022-11-09  8:19     ` Roman Perepelitsa
@ 2022-11-11 22:24       ` Ray Andrews
  2022-11-11 22:39         ` Roman Perepelitsa
  0 siblings, 1 reply; 9+ messages in thread
From: Ray Andrews @ 2022-11-11 22:24 UTC (permalink / raw)
  To: zsh-users


On 2022-11-09 00:19, Roman Perepelitsa wrote:
> Try this instead:
>    if [[ $scope_msg = 'BROAD'                  && $dirname = (#i)*$~1* ]] ||
>       [[ $scope_msg = 'Case INsensitive TAME' && $dirname = (#i)$~1 ]] ||
>       [[ $scope_msg = 'Case Sensitive WILD'      && $dirname = *$~1* ]] ||
>       [[ $scope_msg = 'EXACT'                  && $dirname = $~1 ]]; then
>
That's in place Roman, works identically to mine and I'll keep it on 
your say so, but what are the fine points on why your lines are better?  
I understand the '$~' now so that's accepted.  As I now have it:

NEW:
     if [[ "$scope_msg" = 'BROAD'                  && $dirname == 
(#i)*$~1* ]] \
     || [[ "$scope_msg" = 'Case INsensitive TAME' && $dirname == (#i)$~1 
]] \
     || [[ "$scope_msg" = 'Case Sensitive WILD'      && $dirname == 
*$~1* ]] \
     || [[ "$scope_msg" = 'EXACT'                  && $dirname == $~1 
]]; then ; # Do nothing, we have a match.
     else cc[$aa]=    # Kill the line, it does not match.
     fi
OLD:
     if [[ "$scope_msg" = 'BROAD'                  && $dirname = 
(#i)*$1* ]] \
     || [[ "$scope_msg" = 'Case INsensitive TAME' && $dirname:u = $1:u ]] \
     || [[ "$scope_msg" = 'Case Sensitive WILD'      && $dirname =~ $1 ]] \
     || [[ "$scope_msg" = 'EXACT'                  && $dirname = $1 ]]; 
then ; # Do nothing, we have a match.
     else cc[$aa]=    # Kill the line, it does not match.
     fi


> If this works, consider using more traditional names for these cases:
>
> - case-insensitive partial
> - case-insensitive full
> - case-sensitive partial
> - case-sensitive full

I'm having fun experimenting with these terms and no one but me will 
ever  read them so I amuse myself.  I like 'exact'.  'broad' is 
useless.  Yeah, 'full' and 'partial' are more standard and perfectly 
clear. Tx.




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: coloring a substitution
  2022-11-11 22:24       ` Ray Andrews
@ 2022-11-11 22:39         ` Roman Perepelitsa
  2022-11-12  3:36           ` Ray Andrews
  0 siblings, 1 reply; 9+ messages in thread
From: Roman Perepelitsa @ 2022-11-11 22:39 UTC (permalink / raw)
  To: Ray Andrews; +Cc: zsh-users

On Fri, Nov 11, 2022 at 11:25 PM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> On 2022-11-09 00:19, Roman Perepelitsa wrote:
> > Try this instead:
> >    if [[ $scope_msg = 'BROAD'                  && $dirname = (#i)*$~1* ]] ||
> >       [[ $scope_msg = 'Case INsensitive TAME' && $dirname = (#i)$~1 ]] ||
> >       [[ $scope_msg = 'Case Sensitive WILD'      && $dirname = *$~1* ]] ||
> >       [[ $scope_msg = 'EXACT'                  && $dirname = $~1 ]]; then
> >
> That's in place Roman, works identically to mine and I'll keep it on
> your say so, but what are the fine points on why your lines are better?

These four cases are {case-sensitive, case-insensitive} x {full,
partial}. Moreover, the implementation of every case differs only on
these two bits: case insensitivity is always encoded as (#i) and
partial match always as *..*.

The only bizarre thing here is the names of these cases. They don't
reflect the simplicity of the code. The code is in fact so simple that
it's not obvious that it needs any cases other than 'EXACT': $1 can
have (#i) in it to make the match case-insensitive and *..* to make it
partial. It can even have the asterisk only one one side for prefix
and suffix matches.

>      if [[ "$scope_msg" = 'BROAD'                  && $dirname =
> (#i)*$1* ]] \
>      || [[ "$scope_msg" = 'Case INsensitive TAME' && $dirname:u = $1:u ]] \
>      || [[ "$scope_msg" = 'Case Sensitive WILD'      && $dirname =~ $1 ]] \
>      || [[ "$scope_msg" = 'EXACT'                  && $dirname = $1 ]];
> then

Can you similarly describe these four cases, the way I've done in the
first paragraph above? I cannot. All four look uniquely different, so
the inventive names (BROAD, TAME, WILD, EXACT) almost look justified.

Roman.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: coloring a substitution
  2022-11-11 22:39         ` Roman Perepelitsa
@ 2022-11-12  3:36           ` Ray Andrews
  0 siblings, 0 replies; 9+ messages in thread
From: Ray Andrews @ 2022-11-12  3:36 UTC (permalink / raw)
  To: zsh-users


On 2022-11-11 14:39, Roman Perepelitsa wrote:
>
> These four cases are {case-sensitive, case-insensitive} x {full,
> partial}. Moreover, the implementation of every case differs only on
> these two bits: case insensitivity is always encoded as (#i) and
> partial match always as *..*.
It is better.  More consistent.  I have no idea if the internal 
machinery is different but anyway the performance is identical.
>
> The only bizarre thing here is the names of these cases.

Don't worry about my naming, that's all just fooling around, but every 
one of my functions has these same four cases and I'm trying to work up 
a universal way of talking about it that compact since sometimes I have 
longish messages and making them as compact as possible is a good idea.  
I'll keep fooling around with it.


> Can you similarly describe these four cases, the way I've done in the
> first paragraph above? I cannot. All four look uniquely different, so
> the inventive names (BROAD, TAME, WILD, EXACT) almost look justified.

Right now I'm leaning to ?? FULL PARTIAL and EXACT.  But what's better 
than BROAD?  I don't like it, need something better.  Tho I do like 
WILD, because it's intuitively suggestive of wildcards. Not that it 
really matters, as I said this is entirely for my own use.

Thanks for the help, I learned a great deal.




^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-11-12  3:37 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-08 20:56 coloring a substitution Ray Andrews
2022-11-08 21:10 ` Roman Perepelitsa
2022-11-08 23:49   ` Ray Andrews
2022-11-09  0:11     ` Bart Schaefer
2022-11-09  0:32       ` Ray Andrews
2022-11-09  8:19     ` Roman Perepelitsa
2022-11-11 22:24       ` Ray Andrews
2022-11-11 22:39         ` Roman Perepelitsa
2022-11-12  3:36           ` Ray Andrews

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).