zsh-users
 help / color / mirror / code / Atom feed
From: Ray Andrews <rayandrews@eastlink.ca>
To: zsh-users@zsh.org
Subject: Re: The elements of enlightenment
Date: Mon, 5 Dec 2022 11:35:24 -0800	[thread overview]
Message-ID: <819c9630-f315-8b1c-cb52-3d3ae8de2d5c@eastlink.ca> (raw)
In-Reply-To: <CAN=4vMp2r4Syu-CuF=gxed5om9JQ+NDQm6LvNZRBE4d5a4v1xw@mail.gmail.com>


On 2022-12-05 08:24, Roman Perepelitsa wrote:
> This is the standard way of splitting the output of a command into
> lines and dropping empty lines:
>
>    list=( ${(f)"$(setopt)"} )
>
The thing is I finally really understand it.  I thought newlines always 
broke elements.  I thought that what you saw was what you had.
>    () {
>      local k v
>      local -A color=(on 32 off 33)
>      for k v in ${(kv)options}; do
>        printf '%-20s = \e[%sm%s\e[0m\n' $k ${color[$v]} $v
>      done
>    }

That's most educational, I had no idea you could do that 'for k v in' 
double capture.  And that's a cool demonstration of the use of an 
associative array -- the name can be called by a variable. Powerful.  
But, since setopt is a builtin anyway, why not just use it as is?  I 
love replacing binaries with native zsh code, but setopt is part of the 
family anyway so why not just take it's output?  The function I'm 
playing with requires the array as I have it, just getting the printout 
isn't enough.  I tweaked your code like this:

local k v
local list=()
local -A color=(on ${red} off ${nrm})        # My color variables do the 
predictable thing.
     for k v in ${(kv)options}; do
         list+=$( printf '%-20s %s%s\e[0m\n' $k ${color[$v]} $v )
     done

... to get my list.  The equivalent as I now have it is:

local list=( "$( setopt )" )
list=( ${(f)list} )
list=( "${list[@]/ off/${red} off${nrm}}" )    # red = 'off' here cuz 
that's the way Sebastian had it.

Which is best?  Probably hardly matters, but still, it's good to know.  
printf is heavy, no?  setopt might be lighter?




  parent reply	other threads:[~2022-12-05 19:36 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-05 15:57 Ray Andrews
2022-12-05 16:24 ` Roman Perepelitsa
2022-12-05 18:30   ` Bart Schaefer
2022-12-05 19:35   ` Ray Andrews [this message]
2022-12-05 19:44     ` Bart Schaefer
2022-12-05 19:58     ` Roman Perepelitsa
2022-12-05 21:20       ` Ray Andrews
2022-12-05 21:49         ` Roman Perepelitsa
2022-12-05 23:52           ` Ray Andrews
2022-12-06  0:17             ` Bart Schaefer
2022-12-06  1:34               ` Ray Andrews
2022-12-06 18:55             ` Roman Perepelitsa
2022-12-06 19:45               ` Bart Schaefer
2022-12-06 20:01                 ` Roman Perepelitsa
2022-12-06 20:07                   ` Bart Schaefer
2022-12-06 20:22                     ` Roman Perepelitsa
2022-12-06 23:52               ` Lawrence Velázquez
2022-12-07  1:04                 ` Lawrence Velázquez
2022-12-05 21:49         ` Bart Schaefer
2022-12-05 21:51           ` Bart Schaefer
2022-12-05 22:24           ` Ray Andrews
2022-12-05 23:24             ` Bart Schaefer
2022-12-06  1:47               ` Ray Andrews
2022-12-06  6:39 ` Lawrence Velázquez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=819c9630-f315-8b1c-cb52-3d3ae8de2d5c@eastlink.ca \
    --to=rayandrews@eastlink.ca \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).