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)
Thu, Nov 14, 12:30 PM
Unknown Object (File)
Thu, Nov 14, 12:21 AM
Unknown Object (File)
Wed, Nov 13, 10:13 PM
Unknown Object (File)
Mon, Nov 11, 11:13 AM
Unknown Object (File)
Sun, Nov 10, 2:09 AM
Unknown Object (File)
Sun, Nov 10, 1:15 AM
Unknown Object (File)
Sun, Nov 10, 12:12 AM
Unknown Object (File)
Sat, Nov 9, 11:55 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