zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-workers@zsh.org
Subject: Re: (m)-flag for boundary cases
Date: Sun, 2 Apr 2017 15:57:49 -0700	[thread overview]
Message-ID: <170402155749.ZM9915@torch.brasslantern.com> (raw)
In-Reply-To: <etPan.58d12455.2eb141f2.378f@MacMini.local>

On Mar 21,  2:02pm, Sebastian Gniazdowski wrote:
} Subject: RE: (m)-flag for boundary cases
}
} > dw=3    # Desired width
} > echo ${(mr:dw::_:)a[1,${(m)#a[1,dw-1]}>dw?dw/2:dw]}
} 
} Mixed character widths are possibile.
} 
[...]
} 
} So if one could fix the trailing-a absence, it seems it would work.

    # dw is the "desired width" [or "display width"] function
    # Declare to accept a string because subscripts hate extra commas,
    # and because we need to pass a parameter by name not by math value.
    # Call it in math context (e.g., a scalar slice subscript) like:
    #   dw(paramname:width)
    # Returns largest index w of a full character in $paramname, such
    # that ${paramname[1,w]} occupies $width or fewer display positions.
    dw() {                                  
      integer w
      set -- "${(@s,:,)1}"
      [[ -z ${(P)1} ]] && return -1                                
      for ((w=$2; ${(m)#${(P)1}[1,w]} > $2; w-- )); do :; done 
      return w
    }
    functions -M -s dw

Given the above this example --

    echo ${(mr:9::_:)string[1,dw(string:9)]}

-- always outputs a string nine display positions wide, padded on the
right with underscores, regardless of the mix of character widths in
the value of $string.

Optimizations, if any, left to the reader.


  reply	other threads:[~2017-04-02 22:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-21 13:02 Sebastian Gniazdowski
2017-04-02 22:57 ` Bart Schaefer [this message]
  -- strict thread matches above, loose matches on Subject: below --
2017-03-20 14:33 Sebastian Gniazdowski
2017-03-20 16:00 ` Sebastian Gniazdowski
2017-03-20 16:43 ` Bart Schaefer

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=170402155749.ZM9915@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@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).