Page MenuHomeFreeBSD

sh: Allow an ASCII ESC in PS
ClosedPublic

Authored by otis on Dec 14 2022, 4:03 PM.
Tags
None
Referenced Files
Unknown Object (File)
Sun, May 5, 11:56 AM
Unknown Object (File)
Sat, Apr 27, 6:26 PM
Unknown Object (File)
Fri, Apr 26, 10:24 AM
Unknown Object (File)
Thu, Apr 25, 7:42 PM
Unknown Object (File)
Mar 15 2024, 8:50 AM
Unknown Object (File)
Mar 12 2024, 9:38 PM
Unknown Object (File)
Mar 11 2024, 3:37 PM
Unknown Object (File)
Mar 11 2024, 1:34 PM
Subscribers

Details

Summary

Introduce new prompt format character, '\e' that emits an ASCII ESC
character. This might come handy to use ANSI sequences in PS1, for
example.

Example in ~/.shrc:

PS1="\e[7m\u@\h\e[0m:\w \\$ "

This is my naive approach, as this solves my use case.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

otis requested review of this revision.Dec 14 2022, 4:03 PM
jilles requested changes to this revision.Dec 14 2022, 5:06 PM

Adding support for escape sequences might make it important to increase PROMPTLEN.

bin/sh/parser.c
2067–2069

The \001 codes for EL_PROMPT_ESC need to be around the whole escape sequence, not just the character with code 27, so that libedit's idea of the cursor position remains correct. This is like bash's \[ and \].

Consideration: patch libedit instead so it knows how long escape sequences are (\033[ "CSI" sequences end with certain characters, \033] "OSC" sequences end with \033\\ or \007, perhaps a bit more). Terminals should be sufficiently consistent now, and this relieves the user from adding special sequences to the prompt.

2069

The above check i < PROMPTLEN - 1 is not sufficient to prevent overflow here.

This revision now requires changes to proceed.Dec 14 2022, 5:06 PM
otis marked 2 inline comments as done.Dec 14 2022, 7:56 PM
jilles requested changes to this revision.Dec 18 2022, 9:04 PM
jilles added inline comments.
bin/sh/parser.c
2070

Adding the ESC character implicitly from \[ makes some sense in a vacuum, but it's not compatible with bash (in neither direction), so I don't like it. A simple solution would be to make \[ and \] generate \001 and add \e as before.

This revision now requires changes to proceed.Dec 18 2022, 9:04 PM
This revision is now accepted and ready to land.Dec 22 2022, 2:48 PM