help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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 14:57:24 -0400
User-agent: Mutt/1.10.1 (2018-07-13)

On Wed, Jun 19, 2019 at 01:35:16PM -0500, Jesse Hathaway wrote:
> On Wed, Jun 19, 2019 at 1:33 PM Greg Wooledge <address@hidden> wrote:
> >
> > On Wed, Jun 19, 2019 at 11:18:29AM -0700, Andy Chu wrote:
> > > The @P techniques works if you don't have color in your prompt.  If you
> > > have colors I'm not sure there's a way to do it in general.
> >
> > If the prompt has colors or other terminal escape sequences, then the
> > question "calculate its length" is ambiguous at best.  Does the OP want
> > the byte length for storage, or the visible character length for
> > display width?
> 
> I was hoping for the visible character length.

Well, let's see then...

wooledg:~$ x='\[x\]foo bar'; printf %s "${x@P}" | od -tx1 -An
 01 78 02 66 6f 6f 20 62 61 72

I suppose you could use the @P expansion and then strip out every
sequence of characters between $'\x01' and $'\x02' inclusive (which
may appear any number of times within the expanded result).  What's
left should be the visible characters (or at least what your PS1
variable claims are the visible characters).



reply via email to

[Prev in Thread] Current Thread [Next in Thread]