Bug 464384

Summary: Up / down history damaged if last command output did not end in newline
Product: [Applications] konsole Reporter: donquixote <py7smpbu>
Component: historyAssignee: Konsole Developer <konsole-devel>
Status: RESOLVED NOT A BUG    
Severity: normal CC: matan, ninjalj
Priority: NOR    
Version: 21.12.3   
Target Milestone: ---   
Platform: Ubuntu   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description donquixote 2023-01-16 17:12:39 UTC
SUMMARY

I have observed this behavior for a long time, I think in different terminals.
Atm I am using KDE and Konsole, so I report it here first.


STEPS TO REPRODUCE

Open a terminal.
Run `echo -n hello`.
Use "Up" key to get the previous command.
Use "Down" arrow key to get back to the empty prompt.
Use "Up" again.



EXPECTED RESULT

After running `echo -n hello`:
```
~ $ echo -n hello
hello~ $ 
```

After "Up" arrow key:
```
~ $ echo -n hello
hello~ $ echo -n hello
```

After "Down" arrow key:
```
~ $ echo -n hello
hello~ $
```

After second "Up" arrow key:
```
~ $ echo -n hello
hello~ $ echo -n hello
```


OBSERVED RESULT

(first two steps as in "Expected")

After "Down" arrow key:
```
~ $ echo -n hello
hell
```

After second "Up" arrow key:
```
~ $ echo -n hello
hellecho -n hello
```


NOTES

The behavior gets worse if the last command output was multiple lines long, or the command itself is multiple lines long.




SOFTWARE/OS VERSIONS

Operating System: Ubuntu 22.04
KDE Plasma Version: 5.24.7
KDE Frameworks Version: 5.92.0
Qt Version: 5.15.3
Kernel Version: 5.15.0-56-generic (64-bit)
Graphics Platform: X11
Comment 1 donquixote 2023-01-16 17:26:29 UTC
Could also be a problem with bash or xterm.
Comment 2 donquixote 2023-01-16 20:35:15 UTC
This seems to be my own problem, caused by a PS1 setting in .bashrc.
So, probably not a problem of Konsole.
Sorry for the noise!
Comment 3 donquixote 2023-01-16 21:16:24 UTC
Actually no.
After normalizing $PS1, I still see this problem occur.

See also https://unix.stackexchange.com/questions/731987/terminal-history-up-down-broken-if-last-command-output-does-not-end-in-newline
Comment 4 ninjalj 2023-01-21 22:31:59 UTC
That's just bash (actually readline) thinking that the prompt starts at the leftmost position of the current line, then when you press Down, just moving to position 5, and clearing to the end of line.

When you press Down, bash sends the following to the terminal:

\r\33[C\33[C\33[C\33[C\33[K

That's a carriage return to move to the leftmost position of the current line, four (the length of the prompt) CUF (Cursor Forward) control sequences, and an EL (Erase in Line) control sequence, which with parameter 0 (default) clears from the current cursor position to the end of the line.
Comment 5 donquixote 2023-01-24 04:24:30 UTC
@ninjalj
Thanks for the response in both places.
I am not happy with this behavior of bash, but I don't really know where to take it from here.
I don't know where I would file feature requests for bash.
Perhaps I just need to find an alternative to bash.
Comment 6 Matan Ziv-Av 2023-01-24 12:20:17 UTC
Add '\[\e]133;L\a\]' at the start of PS1. This is a conditional newline, so it does nothing if the previous output ended in newline, and a newline if the last output did not end in newline.
Comment 7 donquixote 2023-01-25 01:15:58 UTC
> Add '\[\e]133;L\a\]' at the start of PS1. This is a conditional newline, so it does nothing if the previous output ended in newline, and a newline if the last output did not end in newline.

This sounds great, but it does nothing for me..

~ $ echo $PS1
\[\e]133;L\a\]\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;34m\]\w\[\033[00m\] \$
~ $ echo -n hello
hello~ $ 
~ $ export PS1="\[\e]133;L\a\] \$"
 $ecoh^C
 $export PS1="\[\e]133;L\a\] \$ "
 $ echo -n hello
hello $ 

The "hello" is still always on the same line.

Btw, where could I look up these character sequences?
Comment 8 donquixote 2023-01-25 01:21:26 UTC
This person is asking for something similar,
https://stackoverflow.com/questions/58626170/print-a-blank-line-in-bash-only-after-output
Comment 9 Matan Ziv-Av 2023-01-25 11:51:49 UTC
(In reply to donquixote from comment #7)
> > Add '\[\e]133;L\a\]' at the start of PS1. This is a conditional newline, so it does nothing if the previous output ended in newline, and a newline if the last output did not end in newline.
> 
> This sounds great, but it does nothing for me..
> 
> ~ $ echo $PS1
> \[\e]133;L\a\]\[\e]0;\u@\h:

This escape sequence is only supported since 22.08

(In reply to donquixote from comment #8)
> This person is asking for something similar,
> https://stackoverflow.com/questions/58626170/print-a-blank-line-in-bash-only-
> after-output

It is a feature that is easy to add to konsole, but currently there is nothing that does this. There are some options to make finding command start easier (separating line, alternating background).