Re: Prompt messed up if PS1 contains ANSI escape sequences

2023-09-07 Thread alex xmb ratchev
On Thu, Sep 7, 2023, 17:41 Kerin Millar  wrote:

> On Thu, 7 Sep 2023 17:33:45 +0200
> alex xmb ratchev  wrote:
>
> > On Thu, Sep 7, 2023, 16:51 Kerin Millar  wrote:
> >
> > > On Thu, 7 Sep 2023 15:53:03 +0200
> > > alex xmb ratchev  wrote:
> > >
> > > > On Thu, Sep 7, 2023, 15:46 Gioele Barabucci 
> wrote:
> > > >
> > > > > On 07/09/23 15:00, alex xmb ratchev wrote:
> > > > > > u have to \[ esc-seq \]
> > > > > > eg inside \[ and \]
> > > > > >
> > > > > > PS1=$'\u\[\e[1m\]\h\[\e[0m- '
> > > > > >
> > > > > > should display hostname bold
> > > > >
> > > > > Thanks for the suggestion, but adding \] does not really fix the
> > > > > problem, it just masks it in many cases (better than nothing).
> > > > >
> > > > > Try:
> > > > >
> > > > > $ long_name="$(printf 'abcdef0123456789/%.0s' {0..20})"
> > > > > $ mkdir -p /tmp/$long_name
> > > > > $ cd /tmp/$long_name
> > > > > $ PS1='\n\[\e[1m\]\w\[\e[m\] \$ '
> > > > >
> > > >
> > > > foo=$' .. '
> > > > not
> > > > foo=' .. '
> > >
> > > $'' quoting is not required for that particular definition of PS1.
> > >
> >
> > o cause \e gets expanded .. ? didnt know ..
>
> Yes. \n also.
>

n was clear e was new
.. thxx .. greets ..

-- 
> Kerin Millar
>


Re: Prompt messed up if PS1 contains ANSI escape sequences

2023-09-07 Thread Kerin Millar
On Thu, 7 Sep 2023 17:33:45 +0200
alex xmb ratchev  wrote:

> On Thu, Sep 7, 2023, 16:51 Kerin Millar  wrote:
> 
> > On Thu, 7 Sep 2023 15:53:03 +0200
> > alex xmb ratchev  wrote:
> >
> > > On Thu, Sep 7, 2023, 15:46 Gioele Barabucci  wrote:
> > >
> > > > On 07/09/23 15:00, alex xmb ratchev wrote:
> > > > > u have to \[ esc-seq \]
> > > > > eg inside \[ and \]
> > > > >
> > > > > PS1=$'\u\[\e[1m\]\h\[\e[0m- '
> > > > >
> > > > > should display hostname bold
> > > >
> > > > Thanks for the suggestion, but adding \] does not really fix the
> > > > problem, it just masks it in many cases (better than nothing).
> > > >
> > > > Try:
> > > >
> > > > $ long_name="$(printf 'abcdef0123456789/%.0s' {0..20})"
> > > > $ mkdir -p /tmp/$long_name
> > > > $ cd /tmp/$long_name
> > > > $ PS1='\n\[\e[1m\]\w\[\e[m\] \$ '
> > > >
> > >
> > > foo=$' .. '
> > > not
> > > foo=' .. '
> >
> > $'' quoting is not required for that particular definition of PS1.
> >
> 
> o cause \e gets expanded .. ? didnt know ..

Yes. \n also.

-- 
Kerin Millar



Re: Prompt messed up if PS1 contains ANSI escape sequences

2023-09-07 Thread alex xmb ratchev
On Thu, Sep 7, 2023, 16:51 Kerin Millar  wrote:

> On Thu, 7 Sep 2023 15:53:03 +0200
> alex xmb ratchev  wrote:
>
> > On Thu, Sep 7, 2023, 15:46 Gioele Barabucci  wrote:
> >
> > > On 07/09/23 15:00, alex xmb ratchev wrote:
> > > > u have to \[ esc-seq \]
> > > > eg inside \[ and \]
> > > >
> > > > PS1=$'\u\[\e[1m\]\h\[\e[0m- '
> > > >
> > > > should display hostname bold
> > >
> > > Thanks for the suggestion, but adding \] does not really fix the
> > > problem, it just masks it in many cases (better than nothing).
> > >
> > > Try:
> > >
> > > $ long_name="$(printf 'abcdef0123456789/%.0s' {0..20})"
> > > $ mkdir -p /tmp/$long_name
> > > $ cd /tmp/$long_name
> > > $ PS1='\n\[\e[1m\]\w\[\e[m\] \$ '
> > >
> >
> > foo=$' .. '
> > not
> > foo=' .. '
>
> $'' quoting is not required for that particular definition of PS1.
>

o cause \e gets expanded .. ? didnt know ..

cheers

-- 
> Kerin Millar
>


Re: Prompt messed up if PS1 contains ANSI escape sequences

2023-09-07 Thread Kerin Millar
On Thu, 7 Sep 2023 15:53:03 +0200
alex xmb ratchev  wrote:

> On Thu, Sep 7, 2023, 15:46 Gioele Barabucci  wrote:
> 
> > On 07/09/23 15:00, alex xmb ratchev wrote:
> > > u have to \[ esc-seq \]
> > > eg inside \[ and \]
> > >
> > > PS1=$'\u\[\e[1m\]\h\[\e[0m- '
> > >
> > > should display hostname bold
> >
> > Thanks for the suggestion, but adding \] does not really fix the
> > problem, it just masks it in many cases (better than nothing).
> >
> > Try:
> >
> > $ long_name="$(printf 'abcdef0123456789/%.0s' {0..20})"
> > $ mkdir -p /tmp/$long_name
> > $ cd /tmp/$long_name
> > $ PS1='\n\[\e[1m\]\w\[\e[m\] \$ '
> >
> 
> foo=$' .. '
> not
> foo=' .. '

$'' quoting is not required for that particular definition of PS1.

-- 
Kerin Millar



Re: Prompt messed up if PS1 contains ANSI escape sequences

2023-09-07 Thread Gioele Barabucci

On 07/09/23 16:24, Gioele Barabucci wrote:

On 07/09/23 16:15, Greg Wooledge wrote:

On Thu, Sep 07, 2023 at 04:03:39PM +0200, Gioele Barabucci wrote:
The following snippet shows that, even with the final \], Bash 
produces the
same erroneous output and miscalculates the cursor position (it just 
needs a

longer prompt):

 $ long_name="$(printf 'abcdef0123456789/%.0s' {0..20})"
 $ mkdir -p /tmp/$long_name
 $ cd /tmp/$long_name
 $ PS1=$'\n\[\e[1m\]\w\[\e[m\] \$ '

 Now press the up arrow, then the down arrow)


Have you tried testing with several different terminal emulators? 


Yes. I tried with xterm, gnome-terminal and the native Linux VT. They 
all show the same problem.


In the heat of the testing I mistakenly used bash 5.0, not bash 5.2.

Bash 5.2.12 does not show this problem.

Regards,

--
Gioele Barabucci



Re: Prompt messed up if PS1 contains ANSI escape sequences

2023-09-07 Thread alex xmb ratchev
On Thu, Sep 7, 2023, 16:26 Andreas Kähäri  wrote:

> On Thu, Sep 07, 2023 at 04:03:39PM +0200, Gioele Barabucci wrote:
> > On 07/09/23 15:50, Greg Wooledge wrote:
> > > On Thu, Sep 07, 2023 at 03:46:23PM +0200, Gioele Barabucci wrote:
> > > > On 07/09/23 15:00, alex xmb ratchev wrote:
> > > > > u have to \[ esc-seq \]
> > > > > eg inside \[ and \]
> > > > >
> > > > > PS1=$'\u\[\e[1m\]\h\[\e[0m- '
> > > > >
> > > > > should display hostname bold
> > > >
> > > > Thanks for the suggestion, but adding \] does not really fix the
> problem, it
> > > > just masks it in many cases (better than nothing).
> > >
> > > The \[ \] wrappings are required.  They're not "masking" the problem.
> > > Your prompt is literally set incorrectly without them.
> >
> > Agreed that \] is required. With "masking" I mean that the use of \]
> > prevents the problem I'm referring to from showing up easily. But the
> > problem is still there even when \] is used.
> >
> > The following snippet shows that, even with the final \], Bash produces
> the
> > same erroneous output and miscalculates the cursor position (it just
> needs a
> > longer prompt):
> >
> > $ long_name="$(printf 'abcdef0123456789/%.0s' {0..20})"
> > $ mkdir -p /tmp/$long_name
> > $ cd /tmp/$long_name
> > $ PS1=$'\n\[\e[1m\]\w\[\e[m\] \$ '
> >
> > Now press the up arrow, then the down arrow)
> >
> > Regards,
> >
> > --
> > Gioele Barabucci
>
> I can't really reproduce the issue that you are seeing, but isn't the \n
> (the newline) also a non-printable character that should be wrapped in
> \[ \], since it has no width?
>

just from my past trying with multiline prompts , no , only esc seq
bash may parse \n there specially , for internal use .. displayment

-- 
> Andreas (Kusalananda) Kähäri
> Uppsala, Sweden
>
> .
>
>


Re: Prompt messed up if PS1 contains ANSI escape sequences

2023-09-07 Thread Andreas Kähäri
On Thu, Sep 07, 2023 at 04:03:39PM +0200, Gioele Barabucci wrote:
> On 07/09/23 15:50, Greg Wooledge wrote:
> > On Thu, Sep 07, 2023 at 03:46:23PM +0200, Gioele Barabucci wrote:
> > > On 07/09/23 15:00, alex xmb ratchev wrote:
> > > > u have to \[ esc-seq \]
> > > > eg inside \[ and \]
> > > > 
> > > > PS1=$'\u\[\e[1m\]\h\[\e[0m- '
> > > > 
> > > > should display hostname bold
> > > 
> > > Thanks for the suggestion, but adding \] does not really fix the problem, 
> > > it
> > > just masks it in many cases (better than nothing).
> > 
> > The \[ \] wrappings are required.  They're not "masking" the problem.
> > Your prompt is literally set incorrectly without them.
> 
> Agreed that \] is required. With "masking" I mean that the use of \]
> prevents the problem I'm referring to from showing up easily. But the
> problem is still there even when \] is used.
> 
> The following snippet shows that, even with the final \], Bash produces the
> same erroneous output and miscalculates the cursor position (it just needs a
> longer prompt):
> 
> $ long_name="$(printf 'abcdef0123456789/%.0s' {0..20})"
> $ mkdir -p /tmp/$long_name
> $ cd /tmp/$long_name
> $ PS1=$'\n\[\e[1m\]\w\[\e[m\] \$ '
> 
> Now press the up arrow, then the down arrow)
> 
> Regards,
> 
> -- 
> Gioele Barabucci

I can't really reproduce the issue that you are seeing, but isn't the \n
(the newline) also a non-printable character that should be wrapped in
\[ \], since it has no width?

-- 
Andreas (Kusalananda) Kähäri
Uppsala, Sweden

.



Re: Prompt messed up if PS1 contains ANSI escape sequences

2023-09-07 Thread Gioele Barabucci

On 07/09/23 16:15, Greg Wooledge wrote:

On Thu, Sep 07, 2023 at 04:03:39PM +0200, Gioele Barabucci wrote:

The following snippet shows that, even with the final \], Bash produces the
same erroneous output and miscalculates the cursor position (it just needs a
longer prompt):

 $ long_name="$(printf 'abcdef0123456789/%.0s' {0..20})"
 $ mkdir -p /tmp/$long_name
 $ cd /tmp/$long_name
 $ PS1=$'\n\[\e[1m\]\w\[\e[m\] \$ '

 Now press the up arrow, then the down arrow)


Have you tried testing with several different terminal emulators? 


Yes. I tried with xterm, gnome-terminal and the native Linux VT. They 
all show the same problem.


--
Gioele Barabucci



Re: Prompt messed up if PS1 contains ANSI escape sequences

2023-09-07 Thread Greg Wooledge
On Thu, Sep 07, 2023 at 04:03:39PM +0200, Gioele Barabucci wrote:
> The following snippet shows that, even with the final \], Bash produces the
> same erroneous output and miscalculates the cursor position (it just needs a
> longer prompt):
> 
> $ long_name="$(printf 'abcdef0123456789/%.0s' {0..20})"
> $ mkdir -p /tmp/$long_name
> $ cd /tmp/$long_name
> $ PS1=$'\n\[\e[1m\]\w\[\e[m\] \$ '
> 
> Now press the up arrow, then the down arrow)

I did this in bash 5.2.15 (Debian 12 package version 5.2.15-2+b2) in
rxvt-unicode (Debian 12 package version 9.30-2+b4), and I can't see
whatever problem you're reporting.  Typing a long command (one which
goes past the 80th column and scrolls the terminal's text upward) and
then backspacing all of it acts as expected.  As does pressing up arrow
until a sufficiently long command is recalled from history.

Have you tried testing with several different terminal emulators?  Be
sure to include xterm among them, as that one tends to be extremely
well-behaved in situations where the big fancy ones are not.  I'm a fan
of rxvt or rxvt-unicode, which have both been kind to me, but good old
xterm should always be considered.



Re: Prompt messed up if PS1 contains ANSI escape sequences

2023-09-07 Thread Gioele Barabucci

On 07/09/23 15:50, Greg Wooledge wrote:

On Thu, Sep 07, 2023 at 03:46:23PM +0200, Gioele Barabucci wrote:

On 07/09/23 15:00, alex xmb ratchev wrote:

u have to \[ esc-seq \]
eg inside \[ and \]

PS1=$'\u\[\e[1m\]\h\[\e[0m- '

should display hostname bold


Thanks for the suggestion, but adding \] does not really fix the problem, it
just masks it in many cases (better than nothing).


The \[ \] wrappings are required.  They're not "masking" the problem.
Your prompt is literally set incorrectly without them.


Agreed that \] is required. With "masking" I mean that the use of \] 
prevents the problem I'm referring to from showing up easily. But the 
problem is still there even when \] is used.


The following snippet shows that, even with the final \], Bash produces 
the same erroneous output and miscalculates the cursor position (it just 
needs a longer prompt):


$ long_name="$(printf 'abcdef0123456789/%.0s' {0..20})"
$ mkdir -p /tmp/$long_name
$ cd /tmp/$long_name
$ PS1=$'\n\[\e[1m\]\w\[\e[m\] \$ '

Now press the up arrow, then the down arrow)

Regards,

--
Gioele Barabucci



Re: Prompt messed up if PS1 contains ANSI escape sequences

2023-09-07 Thread alex xmb ratchev
On Thu, Sep 7, 2023, 15:46 Gioele Barabucci  wrote:

> On 07/09/23 15:00, alex xmb ratchev wrote:
> > u have to \[ esc-seq \]
> > eg inside \[ and \]
> >
> > PS1=$'\u\[\e[1m\]\h\[\e[0m- '
> >
> > should display hostname bold
>
> Thanks for the suggestion, but adding \] does not really fix the
> problem, it just masks it in many cases (better than nothing).
>
> Try:
>
> $ long_name="$(printf 'abcdef0123456789/%.0s' {0..20})"
> $ mkdir -p /tmp/$long_name
> $ cd /tmp/$long_name
> $ PS1='\n\[\e[1m\]\w\[\e[m\] \$ '
>

foo=$' .. '
not
foo=' .. '

(Adapted from .)
>
> Now press up to recall the last command and press down to go back to an
> empty command line. Various stray characters will still be visible and
> the cursor will be in the wrong place.
>
> Also try to type any command (say `echo`) and you will notice that "cho"
> ends up displayed in the wrong line.
>
> Regards,
>
> --
> Gioele Barabucci
>


Re: Prompt messed up if PS1 contains ANSI escape sequences

2023-09-07 Thread Greg Wooledge
On Thu, Sep 07, 2023 at 03:46:23PM +0200, Gioele Barabucci wrote:
> On 07/09/23 15:00, alex xmb ratchev wrote:
> > u have to \[ esc-seq \]
> > eg inside \[ and \]
> > 
> > PS1=$'\u\[\e[1m\]\h\[\e[0m- '
> > 
> > should display hostname bold
> 
> Thanks for the suggestion, but adding \] does not really fix the problem, it
> just masks it in many cases (better than nothing).

The \[ \] wrappings are required.  They're not "masking" the problem.
Your prompt is literally set incorrectly without them.

>From the man page:

PROMPTING
[...]
  \[ begin  a sequence of non-printing characters, which could
 be used to embed a terminal  control  sequence  into  the
 prompt
  \] end a sequence of non-printing characters

See also  for more details.



Re: Prompt messed up if PS1 contains ANSI escape sequences

2023-09-07 Thread Gioele Barabucci

On 07/09/23 15:00, alex xmb ratchev wrote:

u have to \[ esc-seq \]
eg inside \[ and \]

PS1=$'\u\[\e[1m\]\h\[\e[0m- '

should display hostname bold


Thanks for the suggestion, but adding \] does not really fix the 
problem, it just masks it in many cases (better than nothing).


Try:

$ long_name="$(printf 'abcdef0123456789/%.0s' {0..20})"
$ mkdir -p /tmp/$long_name
$ cd /tmp/$long_name
$ PS1='\n\[\e[1m\]\w\[\e[m\] \$ '

(Adapted from .)

Now press up to recall the last command and press down to go back to an 
empty command line. Various stray characters will still be visible and 
the cursor will be in the wrong place.


Also try to type any command (say `echo`) and you will notice that "cho" 
ends up displayed in the wrong line.


Regards,

--
Gioele Barabucci



Re: Prompt messed up if PS1 contains ANSI escape sequences

2023-09-07 Thread alex xmb ratchev
sorry \] missing bug

PS1=$'\u\[\e[1m\]\h\[\e[0m\]- '

On Thu, Sep 7, 2023, 15:00 alex xmb ratchev  wrote:

> u have to \[ esc-seq \]
> eg inside \[ and \]
>
> PS1=$'\u\[\e[1m\]\h\[\e[0m- '
>
> should display hostname bold
>
> On Thu, Sep 7, 2023, 14:55 Gioele Barabucci  wrote:
>
>> Bash 5.2.15 (Debian 12) will produce erroneous output when PS1 contains
>> escape sequences. It will also misplace the cursor and corrupt the state
>> of the current line.
>>
>> To replicate this issue:
>>
>> 1. Modify PS1 to contain one or more ANSI escape sequences, for example
>> "\e[45m" (tput setab 5, changes the background color to purple) or
>> "\e(B\e[m" (tput sgr0, reset for xterm terminals):
>>
>>  $ PS1="x$(printf '\e(B\e[m') \\\$ "
>>
>> 2. Press the up arrow to recall the last command. The command line will
>> now show [■ indicates the position of the cursor]:
>>
>>  x $ PS1="x$(printf '\e(B\e[m') \\\$ "■
>>
>> 3. Press the down arrow to go back to the empty prompt.
>>
>> What one should see at this point is just the prompt
>>
>>  x $ ■
>>
>> Instead, left-overs from the recalled line will be visible and the
>> cursor will be placed in the wrong spot:
>>
>>  x $ PS1="x■
>>
>> The amount of garbage left on the prompt is proportional to the number
>> of escape sequences:
>>
>> PS1="x$(printf '\e(B\e[m')\\\$ " => 6 left-over chars
>> PS1="x$(printf '\e(B\e[m%.0s' {1..2})\\\$ " => 12 left-over chars
>> PS1="x$(printf '\e(B\e[m%.0s' {1..4})\\\$ " => 24 left-over chars
>>
>> Tested with TERM = linux, xterm, xterm-256color.
>>
>> Regards,
>>
>> (This bug has also been reported at .)
>>
>> --
>> Gioele Barabucci
>>
>>


Re: Prompt messed up if PS1 contains ANSI escape sequences

2023-09-07 Thread alex xmb ratchev
u have to \[ esc-seq \]
eg inside \[ and \]

PS1=$'\u\[\e[1m\]\h\[\e[0m- '

should display hostname bold

On Thu, Sep 7, 2023, 14:55 Gioele Barabucci  wrote:

> Bash 5.2.15 (Debian 12) will produce erroneous output when PS1 contains
> escape sequences. It will also misplace the cursor and corrupt the state
> of the current line.
>
> To replicate this issue:
>
> 1. Modify PS1 to contain one or more ANSI escape sequences, for example
> "\e[45m" (tput setab 5, changes the background color to purple) or
> "\e(B\e[m" (tput sgr0, reset for xterm terminals):
>
>  $ PS1="x$(printf '\e(B\e[m') \\\$ "
>
> 2. Press the up arrow to recall the last command. The command line will
> now show [■ indicates the position of the cursor]:
>
>  x $ PS1="x$(printf '\e(B\e[m') \\\$ "■
>
> 3. Press the down arrow to go back to the empty prompt.
>
> What one should see at this point is just the prompt
>
>  x $ ■
>
> Instead, left-overs from the recalled line will be visible and the
> cursor will be placed in the wrong spot:
>
>  x $ PS1="x■
>
> The amount of garbage left on the prompt is proportional to the number
> of escape sequences:
>
> PS1="x$(printf '\e(B\e[m')\\\$ " => 6 left-over chars
> PS1="x$(printf '\e(B\e[m%.0s' {1..2})\\\$ " => 12 left-over chars
> PS1="x$(printf '\e(B\e[m%.0s' {1..4})\\\$ " => 24 left-over chars
>
> Tested with TERM = linux, xterm, xterm-256color.
>
> Regards,
>
> (This bug has also been reported at .)
>
> --
> Gioele Barabucci
>
>


Prompt messed up if PS1 contains ANSI escape sequences

2023-09-07 Thread Gioele Barabucci
Bash 5.2.15 (Debian 12) will produce erroneous output when PS1 contains 
escape sequences. It will also misplace the cursor and corrupt the state 
of the current line.


To replicate this issue:

1. Modify PS1 to contain one or more ANSI escape sequences, for example 
"\e[45m" (tput setab 5, changes the background color to purple) or 
"\e(B\e[m" (tput sgr0, reset for xterm terminals):


$ PS1="x$(printf '\e(B\e[m') \\\$ "

2. Press the up arrow to recall the last command. The command line will 
now show [■ indicates the position of the cursor]:


x $ PS1="x$(printf '\e(B\e[m') \\\$ "■

3. Press the down arrow to go back to the empty prompt.

What one should see at this point is just the prompt

x $ ■

Instead, left-overs from the recalled line will be visible and the 
cursor will be placed in the wrong spot:


x $ PS1="x■

The amount of garbage left on the prompt is proportional to the number 
of escape sequences:


PS1="x$(printf '\e(B\e[m')\\\$ " => 6 left-over chars
PS1="x$(printf '\e(B\e[m%.0s' {1..2})\\\$ " => 12 left-over chars
PS1="x$(printf '\e(B\e[m%.0s' {1..4})\\\$ " => 24 left-over chars

Tested with TERM = linux, xterm, xterm-256color.

Regards,

(This bug has also been reported at .)

--
Gioele Barabucci