Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: (m)-flag for boundary cases
- X-seq: zsh-workers 40931
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: Re: (m)-flag for boundary cases
- Date: Sun, 2 Apr 2017 15:57:49 -0700
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=from:message-id:date:in-reply-to:comments:references:to:subject :mime-version; bh=gf30Kjxu/FaVJXlyXEuWeUHtQZ2u/RAShHEb3EM/2+0=; b=lS6FzW0ynv1lMov1z6qycaUQEKPJ7tLdBRCjI3efD9tTQVuPmEl6PgwnRK1Rl+jcvV 8U9KQkBpykL5TXGAkds2GiHFbNL8wIEpTiaHgg2BuViUG63kci2AsJYEPoQyvxYT+xaB XA4ozLWcl8LqqzaPKOuyTRxEFZYAPvLFXDEfxB/9jb2xcif0traY/+4L5XJIu8NFMy53 Rflhve5bEmDaQXkXPZvUI284rgVcMp8ztN2eOvjMZ6tjH+fHCdf52I4c70ttYSwvQmg6 MA/npbmfpjp53udNHEXBHYoLJ9eewEblOt29mmJ0kYGOTwzY8zV9QEHabaXAQKbmXL1o IgqA==
- In-reply-to: <etPan.58d12455.2eb141f2.378f@MacMini.local>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <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.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author