Re: [Help-bash] Expand a prompt string to calculate its length?
From:
Greg Wooledge
Subject:
Re: [Help-bash] Expand a prompt string to calculate its length?
Date:
Wed, 19 Jun 2019 15:21:34 -0400
User-agent:
Mutt/1.10.1 (2018-07-13)
On Wed, Jun 19, 2019 at 11:57:28AM -0700, Andy Chu wrote:
> 1. colors and ANSI (compensated for by \[ and \] as mentioned)
>
> 2. Unicode characters. All shells appear use wcwidth() or some
> variant to compute this.
>
>http://man7.org/linux/man-pages/man3/wcwidth.3.html
>
> So depending on the complexity of the prompt, there are 2 issues to overcome.
>
> AFAIK there's not an easy pure-bash solution for either problem, short
> of reimplementing PS1, which is probably technically possible in bash
> but something you don't want to do. I did this when implementing
> bash-compatible prompts for Oil [1].
The @P operator in bash 4.4 overcomes the first, and bash itself already
handles the second.
wooledg:~$ shopt -s extglob
wooledg:~$ glob=$'\x01*([!\x02])\x02'
wooledg:~$ NOTPS1='foo\[xyz\]ñä'
wooledg:~$ tmp=${NOTPS1@P}; tmp2=${tmp//$glob/}
wooledg:~$ echo "<$tmp> <${#tmp}> <$tmp2> <${#tmp2}>"
<fooxyzñä> <10> <fooñä> <5>
I didn't test with characters outside the Unicode BMP, but this case
seems to work. Note that the <$tmp> piece actually contains two
invisible characters (01 and 02), hence the length of 10. <$tmp2>
contains 3 ASCII characters and 2 UTF-8 characters, and is counted with
a length of 5.
Is it pretty? No. But it's not totally ridiculous.