zsh-users
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: Zach Riggle <zachriggle@gmail.com>
Cc: zsh-users@zsh.org
Subject: Re: Use of left- and right-padding with a variable padding char
Date: Sat, 24 Jul 2021 12:08:59 +0200	[thread overview]
Message-ID: <CAHYJk3Tm9MSTCOnjeRqjWuvkCGMYhY62kVRKJ1UqckvHLBMKCw@mail.gmail.com> (raw)
In-Reply-To: <CAMP9c5nbz-MnZB3VN6kD=13=7oD-naKSL+19KQXFQQLTZy-ZHg@mail.gmail.com>

On 7/24/21, Zach Riggle <zachriggle@gmail.com> wrote:
> As always, thank you all so much for your time.  It's genuinely
> appreciated.
>
> I want to define a function "center", which takes $1="Some String" and
> $2="-" or some other default padding value.
>
> The Zsh manual on expansion says that I can do something like this,
> which works well and as intended:
>
>     function center() {
>         local text="${1:-hellworld}"
>         local -i columns=${COLUMNS:-$(tput cols)}
>         columns=$(( columns/2 ))
>         echo ${(l:${columns}::=:::r:${columns}::=:::)text}
>     }
>
> This works very well, and correctly centers "${text}" across the width
> of the terminal, with "=" padding on both sides.  Perfect.
>
> However, I also want to make the padding character ("=" in the above
> example) be variable.
>
> The following does NOT work as intended, and I'm not sure why.  My
> understanding is that variable expansion is inside-out, so it SHOULD
> work.
>
>     function center() {
>         local text="${1:-hellworld}"
>         local pad="${2:-=}"
>         local -i columns=${COLUMNS:-$(tput cols)}
>         columns=$(( columns/2 ))
>         echo ${(l:${columns}::${pad}:::r:${columns}::${pad}:::)text}
>     }
>     center "hello" "_"
>
> The value in $1 is correctly centered, but it is padded with the
> LITERAL value '${pad}', rather than the expansion of ${pad}.
>
> I expect there's some documentation that I haven't read yet, any tips
> would be useful.

Add p and drop the braces, as in
  echo ${(pl:${columns}::$pad:::r:${columns}::$pad:::)text}


       p      Recognize  the  same  escape  sequences as the print
              builtin in string arguments to any of the flags described
              below that follow  this  argument.

              Alternatively,  with  this  option string arguments
              may be in the form $var in which case the value of
              the  variable  is  substituted.   Note this  form  is
              strict;  the  string argument does not undergo general
              parameter expansion.



-- 
Mikael Magnusson


      reply	other threads:[~2021-07-24 10:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-24  7:47 Zach Riggle
2021-07-24 10:08 ` Mikael Magnusson [this message]

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=CAHYJk3Tm9MSTCOnjeRqjWuvkCGMYhY62kVRKJ1UqckvHLBMKCw@mail.gmail.com \
    --to=mikachu@gmail.com \
    --cc=zachriggle@gmail.com \
    --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).