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: Andy Chu
Subject: Re: [Help-bash] Expand a prompt string to calculate its length?
Date: Wed, 19 Jun 2019 11:57:28 -0700

On Wed, Jun 19, 2019 at 11:50 AM Jesse Hathaway <address@hidden> 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.


This doesn't help you, but for completeness, there are 2 issues where
the byte string length is different from the display width:

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].

Andy

[1] http://www.oilshell.org/



reply via email to

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