zsh-workers
 help / color / mirror / code / Atom feed
* Feature request – substitutions similar to +, -, :+, :-
@ 2017-11-18 18:05 Sebastian Gniazdowski
  2017-11-18 18:22 ` Sebastian Gniazdowski
  0 siblings, 1 reply; 10+ messages in thread
From: Sebastian Gniazdowski @ 2017-11-18 18:05 UTC (permalink / raw)
  To: zsh-workers

Hello
I constantly save lines of code by using +,-,:+,:- substitutions. Example: there's "$theme[button-color]", which can be empty, so no need for $reset_color. Solution: 

button_txt="$theme[button-color]My Button${theme[button-color]:+$reset_color}"

Sometimes I was needing similar substitution but checking for concrete value. So, substitute "abc" if value is "xyz", etc. However it's a hard time to find a place in the substitution where the value could be specified. However, a step in right direction would be boolean test for 1 or 0, or maybe like in zstyle also for true/false, yes/no.

So this would be possible:

echo "I'm ${${theme[use-name]:&Patrick}:-a subscriber}, hello."

:& would substitute on "1", not substitute otherwise, so also not for "". Analogically :^ would substitute on "0". Those substitutions would definitely often save lines of code.

--  
Sebastian Gniazdowski
psprint /at/ zdharma.org


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

* Re: Feature request – substitutions similar to +, -, :+, :-
  2017-11-18 18:05 Feature request – substitutions similar to +, -, :+, :- Sebastian Gniazdowski
@ 2017-11-18 18:22 ` Sebastian Gniazdowski
  2017-11-18 18:46   ` Bart Schaefer
       [not found]   ` <etPan.5a10805d.57d72d7c.137@AirmailxGenerated.am>
  0 siblings, 2 replies; 10+ messages in thread
From: Sebastian Gniazdowski @ 2017-11-18 18:22 UTC (permalink / raw)
  To: zsh-workers

On 18 Nov 2017 at 19:05:25, Sebastian Gniazdowski (psprint@zdharma.org) wrote:
> echo "I'm ${${theme[use-name]:&Patrick}:-a subscriber}, hello."

Just realized that with the description of :& I gave, this would work the same, except not only for "1", but any non-empty string:

echo "I'm ${${theme[use-name]:+Patrick}:-a subscriber}

So it's a matter of getting consensus on details of :& and :^. Sorry that I don't have access to my revelations of how good boolean test would be. I've had them when was writing code, if I get some again I'll send them.

-- 
Sebastian Gniazdowski
psprint /at/ zdharma.org


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

* Re: Feature request – substitutions similar to +, -, :+, :-
  2017-11-18 18:22 ` Sebastian Gniazdowski
@ 2017-11-18 18:46   ` Bart Schaefer
       [not found]   ` <etPan.5a10805d.57d72d7c.137@AirmailxGenerated.am>
  1 sibling, 0 replies; 10+ messages in thread
From: Bart Schaefer @ 2017-11-18 18:46 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: zsh-workers

On Sat, Nov 18, 2017 at 10:22 AM, Sebastian Gniazdowski
<psprint@zdharma.org> wrote:
>
> On 18 Nov 2017 at 19:05:25, Sebastian Gniazdowski (psprint@zdharma.org) wrote:
> > echo "I'm ${${theme[use-name]:&Patrick}:-a subscriber}, hello."
>
> Just realized that with the description of :& I gave, this would work the same, except not only for "1", but any non-empty string:
>
> echo "I'm ${${theme[use-name]:+Patrick}:-a subscriber}
>
> So it's a matter of getting consensus on details of :& and :^.

If I understand your example correctly, $theme[use-name] is either
unset/empty, or it contains "1" or "0".

So I think :& is just  ${(M)theme[use-name]:#1} and :^ is ${theme[use-name]:#0}.

For $options, ${(M)options[...]:#on} and ${options[...]:#off}, so this
trick works with non-numeric booleans too.

You can do something similar with ${var/pat/repl} if you need to test
for substrings.


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

* Re: Feature request – substitutions similar to +, -, :+, :-
       [not found]   ` <etPan.5a10805d.57d72d7c.137@AirmailxGenerated.am>
@ 2017-11-25 11:15     ` Sebastian Gniazdowski
  2017-12-03  9:11       ` Ternary expression in Zsh (was: Feature request – substitutions similar to +, -, :+, :-) Sebastian Gniazdowski
  0 siblings, 1 reply; 10+ messages in thread
From: Sebastian Gniazdowski @ 2017-11-25 11:15 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-workers

On 18 Nov 2017 at 19:46:18, Bart Schaefer (schaefer@brasslantern.com) wrote:
> If I understand your example correctly, $theme[use-name] is either
> unset/empty, or it contains "1" or "0".
> 
> So I think :& is just ${(M)theme[use-name]:#1} and :^ is ${theme[use-name]:#0}.

It looks for me that this would filter value "1" or filter-out value "0". It seems that this doesn't translate to code-shortening one-liner. I was rather wrong in my original post when I wrote about boolean substitutions targetting 0 and 1 values. Direct access to use case is needed to filter useful thing (while I was doing retrospection). However today an IRC user had one use case:

:- does:

if (VAR) { return VAR } else { return "something else"; }

new :- (with proposed :^ form) should:

if (VAR) { return "" } else { return "something else" }

So it's about clearing default output. This would allow somewhat boolean-style code, like:

% DOCS=yes (or DOCS="")
% find ${${DOCS:^Pictures}:-Documents}

This does: If DOCS empty (checked by :^ substitution), then use "Pictures", otherwise, use "Documents" (because :- detects empty default output <-> DOCS is true, not-empty).

For boolean evaluations, clearing actual output is intuitively expected. What's left is just that as long as ^ associates with negation action, a better form would be good, that would associate with empty value, like minus "-", ":-".

-- 
Sebastian Gniazdowski
psprint /at/ zdharma.org


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

* Ternary expression in Zsh (was: Feature request – substitutions similar to +, -, :+, :-)
  2017-11-25 11:15     ` Sebastian Gniazdowski
@ 2017-12-03  9:11       ` Sebastian Gniazdowski
  2017-12-05  6:55         ` Stephane Chazelas
  2017-12-05  8:24         ` Sebastian Gniazdowski
  0 siblings, 2 replies; 10+ messages in thread
From: Sebastian Gniazdowski @ 2017-12-03  9:11 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-workers

On 25 Nov 2017 at 12:15:42, Sebastian Gniazdowski (psprint@zdharma.org) wrote:
> if (VAR) { return "" } else { return "something else" }

Kept the main point of previous messages. New point – it is actually possible to have ternary expression in Zsh:

% var=abc
% echo ${${${(M)var#abc(#e)}:+ternary1}:-ternary2}
ternary1
% var=abcd
% echo ${${${(M)var#abc(#e)}:+ternary1}:-ternary2}
ternary2

It's a one-liner "If val is xyz, then substitute". Pretty neat thing, maybe there are similar tricks possible? I have to implement something and cannot fiddle with this now.

--  
Sebastian Gniazdowski
psprint /at/ zdharma.org


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

* Re: Ternary expression in Zsh (was: Feature request – substitutions similar to +, -, :+, :-)
  2017-12-03  9:11       ` Ternary expression in Zsh (was: Feature request – substitutions similar to +, -, :+, :-) Sebastian Gniazdowski
@ 2017-12-05  6:55         ` Stephane Chazelas
  2017-12-05  8:06           ` Tom M.
  2017-12-05  8:24         ` Sebastian Gniazdowski
  1 sibling, 1 reply; 10+ messages in thread
From: Stephane Chazelas @ 2017-12-05  6:55 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Bart Schaefer, zsh-workers

2017-12-03 10:11:10 +0100, Sebastian Gniazdowski:
> On 25 Nov 2017 at 12:15:42, Sebastian Gniazdowski (psprint@zdharma.org) wrote:
> > if (VAR) { return "" } else { return "something else" }
> 
> Kept the main point of previous messages. New point – it is actually possible to have ternary expression in Zsh:
[...]

See also:

$ true; echo ${${=:-green red}[2-!?]}
green
$ false; echo ${${=:-green red}[2-!?]}
red

-- 
Stephane


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

* Re: Ternary expression in Zsh (was: Feature request – substitutions similar to +, -, :+, :-)
  2017-12-05  6:55         ` Stephane Chazelas
@ 2017-12-05  8:06           ` Tom M.
  2017-12-05  9:04             ` Bart Schaefer
  0 siblings, 1 reply; 10+ messages in thread
From: Tom M. @ 2017-12-05  8:06 UTC (permalink / raw)
  To: Sebastian Gniazdowski, Bart Schaefer, zsh-workers

> $ true; echo ${${=:-green red}[2-!?]}
> green
> $ false; echo ${${=:-green red}[2-!?]}
> red

The above fails for me:
% true; echo ${${=:-green red}[2-!?]}
recvmsg(3, {msg_name=NULL, msg_namelen=0,
msg_iov=[{iov_base="\1\0\24\0\0\0\0\0?\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
iov_len=4096}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 32
recvmsg(3, {msg_namelen=0}, 0)          = -1 EAGAIN (Resource
temporarily unavailable)
recvmsg(3, {msg_name=NULL, msg_namelen=0,
msg_iov=[{iov_base="\1\0\25\0\0\0\0\0@\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
iov_len=4096}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 32
recvmsg(3, {msg_namelen=0}, 0)          = -1 EAGAIN (Resource
temporarily unavailable)
recvmsg(3, {msg_name=NULL, msg_namelen=0,
msg_iov=[{iov_base="\1\0\26\0\0\0\0\0A\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
iov_len=4096}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 32
recvmsg(3, {msg_namelen=0}, 0)          = -1 EAGAIN (Resource
temporarily unavailable)
recvmsg(3, {msg_name=NULL, msg_namelen=0,
msg_iov=[{iov_base="\1\0\27\0\0\0\0\0B\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
iov_len=4096}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 32
recvmsg(3, {msg_namelen=0}, 0)          = -1 EAGAIN (Resource
temporarily unavailable)
recvmsg(3, {msg_name=NULL, msg_namelen=0,
msg_iov=[{iov_base="\1\0\30\0\0\0\0\0C\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
iov_len=4096}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 32
recvmsg(3, {msg_name=NULL, msg_namelen=0,
msg_iov=[{iov_base="\1\0\31\0\0\0\0\0D\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
iov_len=4096}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 32
recvmsg(3, {msg_name=NULL, msg_namelen=0,
msg_iov=[{iov_base="\1\0\32\0\0\0\0\0E\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"...,
iov_len=4096}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 64
recvmsg(3, {msg_name=NULL, msg_namelen=0,
msg_iov=[{iov_base="\1\0\34\0\0\0\0\0G\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
iov_len=4096}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 32
recvmsg(3, {msg_namelen=0}, 0)          = -1 EAGAIN (Resource
temporarily unavailable)
recvmsg(3, {msg_name=NULL, msg_namelen=0,
msg_iov=[{iov_base="\1\0\35\0\0\0\0\0H\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
iov_len=4096}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 32
recvmsg(3, {msg_namelen=0}, 0)          = -1 EAGAIN (Resource
temporarily unavailable)
recvmsg(3, {msg_name=NULL, msg_namelen=0,
msg_iov=[{iov_base="\1\0\36\0\0\0\0\0I\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
iov_len=4096}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 32
recvmsg(3, {msg_namelen=0}, 0)          = -1 EAGAIN (Resource
temporarily unavailable)
recvmsg(3, {msg_name=NULL, msg_namelen=0,
msg_iov=[{iov_base="\1\0\37\0\0\0\0\0J\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
iov_len=4096}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 32
recvmsg(3, {msg_namelen=0}, 0)          = -1 EAGAIN (Resource
temporarily unavailable)
recvmsg(3, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="\1\0
\0\0\0\0\0K\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
iov_len=4096}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 32
recvmsg(3, {msg_namelen=0}, 0)          = -1 EAGAIN (Resource
temporarily unavailable)
recvmsg(3, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="\1\0,
iov_len=4096}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0)

And if run from zsh -f:
% true; echo ${${=:-green red}[2-!?]}
zsh: no such event: ]}

Any idea what can be responsible for this?


--
T.


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

* Re: Ternary expression in Zsh (was: Feature request – substitutions similar to +, -, :+, :-)
  2017-12-03  9:11       ` Ternary expression in Zsh (was: Feature request – substitutions similar to +, -, :+, :-) Sebastian Gniazdowski
  2017-12-05  6:55         ` Stephane Chazelas
@ 2017-12-05  8:24         ` Sebastian Gniazdowski
  2017-12-05  9:05           ` Bart Schaefer
  1 sibling, 1 reply; 10+ messages in thread
From: Sebastian Gniazdowski @ 2017-12-05  8:24 UTC (permalink / raw)
  To: zsh-workers

On 3 Dec 2017 at 10:11:10, Sebastian Gniazdowski (psprint@zdharma.org) wrote:
> % var=abc
> % echo ${${${(M)var#abc(#e)}:+ternary1}:-ternary2}
> ternary1
> % var=abcd
> % echo ${${${(M)var#abc(#e)}:+ternary1}:-ternary2}
> ternary2

Upgrade that doesn't need extendedglob (#e needs it):

% var=abc
% echo ${${${(M)var:#abc}:+ternary1}:-ternary2}
ternary1
% var=abcd
% echo ${${${(M)var:#abc}:+ternary1}:-ternary2}
ternary2

Substituted :# for # and (#e). :# wants whole string, so no #e needed. Maybe this is what Bart was aiming at in previous topic.

--  
Sebastian Gniazdowski
psprint /at/ zdharma.org


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

* Re: Ternary expression in Zsh (was: Feature request – substitutions similar to +, -, :+, :-)
  2017-12-05  8:06           ` Tom M.
@ 2017-12-05  9:04             ` Bart Schaefer
  0 siblings, 0 replies; 10+ messages in thread
From: Bart Schaefer @ 2017-12-05  9:04 UTC (permalink / raw)
  To: zsh-workers; +Cc: Tom M.

On Tue, Dec 5, 2017 at 12:06 AM, Tom M. <boojum@stercus-accidit.com> wrote:
>> $ true; echo ${${=:-green red}[2-!?]}
>> green
>> $ false; echo ${${=:-green red}[2-!?]}
>> red
>
> The above fails for me:
> [...]
> And if run from zsh -f:
> % true; echo ${${=:-green red}[2-!?]}
> zsh: no such event: ]}

Stephane has forgotten that he has "setopt nobanghist".

You can get the same effect with $? except the sense is reversed:

% true; echo ${${=:-green red}[2-$?]}
red
% false; echo ${${=:-green red}[2-$?]}
green

This avoids the issue with banghist, but beware of commands that
return other than 0 or 1 for success/failure.


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

* Re: Ternary expression in Zsh (was: Feature request – substitutions similar to +, -, :+, :-)
  2017-12-05  8:24         ` Sebastian Gniazdowski
@ 2017-12-05  9:05           ` Bart Schaefer
  0 siblings, 0 replies; 10+ messages in thread
From: Bart Schaefer @ 2017-12-05  9:05 UTC (permalink / raw)
  To: zsh-workers

On Tue, Dec 5, 2017 at 12:24 AM, Sebastian Gniazdowski
<psprint@zdharma.org> wrote:
>
> Substituted :# for # and (#e). :# wants whole string, so no #e needed. Maybe this is what Bart was aiming at in previous topic.

Exactly.


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

end of thread, other threads:[~2017-12-05  9:06 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-18 18:05 Feature request – substitutions similar to +, -, :+, :- Sebastian Gniazdowski
2017-11-18 18:22 ` Sebastian Gniazdowski
2017-11-18 18:46   ` Bart Schaefer
     [not found]   ` <etPan.5a10805d.57d72d7c.137@AirmailxGenerated.am>
2017-11-25 11:15     ` Sebastian Gniazdowski
2017-12-03  9:11       ` Ternary expression in Zsh (was: Feature request – substitutions similar to +, -, :+, :-) Sebastian Gniazdowski
2017-12-05  6:55         ` Stephane Chazelas
2017-12-05  8:06           ` Tom M.
2017-12-05  9:04             ` Bart Schaefer
2017-12-05  8:24         ` Sebastian Gniazdowski
2017-12-05  9:05           ` Bart Schaefer

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