add PROMPT_SEPARATOR, PROMPT_DIRECTORY, and PROMPT_START/PROMPT_END (#2239152)

With thanks to Thomas M Steenholdt for suggesting and pushing for this
This commit is contained in:
Jens Petersen 2023-11-13 11:12:05 +08:00
parent f2daf2ccc6
commit 27cb5add3f
3 changed files with 75 additions and 29 deletions

View File

@ -1,5 +1,7 @@
# Simple colored bash prompt
Defined in `/etc/profile.d/bash-color-prompt.sh`
The prompt color theme can be customized simply by setting
the `PROMPT_COLOR` envvar, and optionally `PROMPT_DIR_COLOR`.
@ -9,18 +11,70 @@ and `PROMPT_COLOR='1;33;53'` is bright/bold yellow with overline.
`PROMPT_DIR_COLOR` similarly changes the color of the working directory,
which otherwise defaults to `PROMPT_COLOR`.
The prompt can be bracketed to look more like the traditional Fedora prompt
by setting `PROMPT_BRACKETS`.
## Coloring Examples
Set `prompt_color_force` (before source'ing) to force usage/updating of color PS1:
by default it is only setup cautiously if `$TERM` ends in "color"
```shell
PROMPT_COLOR=0 # disable colors/attribs
PROMPT_COLOR=1 # bold prompt
PROMPT_COLOR=2 # dim prompt
PROMPT_COLOR=4 # underline prompt
PROMPT_COLOR='2;7' # dim reverse video
PROMPT_COLOR='42' # green background
PROMPT_COLOR='53' # overline separator
PROMPT_COLOR='1;33;44' # bold yellow on blue
PROMPT_COLOR='43;30' # black on yellow
PROMPT_COLOR='1;32' # bold green
PROMPT_COLOR='' # unset defaults to green
```
```shell
eval $(grep ANSI_COLOR /etc/os-release)
PROMPT_COLOR="$ANSI_COLOR"
```
See <https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters> for ANSI codes details.
## More variables
### `PROMPT_START` (optional)
Displayed at the start of the prompt.
### `PROMPT_SEPARATOR`
Default: `PROMPT_SEPARATOR=':'`
eg you can change it to `'\n` or `' \t\n'
### `PROMPT_DIRECTORY`
Default: `PROMPT_DIRECTORY='\w'`
eg you can change it to `'\W'`
### `PROMPT_ERROR`
Set (eg `PROMPT_ERROR=1`) if you want to append red error exit code to
the prompt.
### `PROMPT_END`
Displayed at the end of the prompt (before `\$`).
## Traditional Red Hat prompt
```
PROMPT_START='['
PROMPT_END=']'
PROMPT_SEPARATOR=' '
PROMPT_DIRECTORY='\W'
PROMPT_COLOR='0'
```
## Sourcing
Set `prompt_color_force` (before source'ing) to force usage/updating of color
PS1: by default it is only setup cautiously if `$TERM` ends in "color"
*and* PS1 is the fedora or bash default.
Also you can set `PROMPT_ERROR` if you want to append red error exit code to the prompt.
```shellsession
$ prompt_color_force=1
$ source /etc/profile.d/bash-color-prompt.sh
```
See `/etc/profile.d/bash-color-prompt.sh` for the actual script.
Please open issues against [shell-color-prompt](https://src.fedoraproject.org/rpms/shell-color-prompt).
## References
See <https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters> etc for the ANSI codes.
## Contribute
Please open issues against
[shell-color-prompt](https://src.fedoraproject.org/rpms/shell-color-prompt).

View File

@ -1,19 +1,4 @@
# see also /usr/share/doc/bash-color-prompt/README.md
# example colorings:
# PROMPT_COLOR=0 # disable colors/attribs
# PROMPT_COLOR=1 # bold prompt
# PROMPT_COLOR=2 # dim prompt
# PROMPT_COLOR=4 # underline prompt
# PROMPT_COLOR='2;7' # dim reverse video
# PROMPT_COLOR='42' # green background
# PROMPT_COLOR='53' # overline separator
# PROMPT_COLOR='1;33;44' # bold yellow on blue
# PROMPT_COLOR='43;30' # black on yellow
# PROMPT_COLOR='1;32' # bold green
# PROMPT_COLOR='' # unset defaults to green
# See <https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters> for ANSI codes
# see /usr/share/doc/bash-color-prompt/README.md
## to truncate \w dirpath set:
# PROMPT_DIRTRIM=3
@ -21,6 +6,8 @@
# only set for color terminals and if PS1 unchanged from bash or fedora defaults
if [ '(' "$PS1" = "[\u@\h \W]\\$ " -o "$PS1" = "\\s-\\v\\\$ " ')' -a "${TERM: -5}" = "color" -o -n "${prompt_color_force}" ]; then
PS1='${PROMPT_BRACKETS:+[}\[\e[${PROMPT_COLOR:-32}m\]${container:+⬢ }\u@\h\[\e[0m\]:\[\e[${PROMPT_DIR_COLOR:-${PROMPT_COLOR:-32}}m\]\w${PROMPT_ERROR:+\[\e[0;31m\]${?#0}}\[\e[0m\]${PROMPT_BRACKETS:+]}\$ '
PROMPT_SEPARATOR=':'
PROMPT_DIRECTORY='\w'
PS1='${PROMPT_START@P}\[\e[${PROMPT_COLOR:-32}m\]${container:+⬢ }\u@\h\[\e[0m\]${PROMPT_SEPARATOR@P}\[\e[${PROMPT_DIR_COLOR:-${PROMPT_COLOR:-32}}m\]${PROMPT_DIRECTORY@P}${PROMPT_ERROR:+\[\e[0;31m\]${?#0}}\[\e[0m\]${PROMPT_END@P}\$ '
fi

View File

@ -1,5 +1,5 @@
Name: shell-color-prompt
Version: 0.2.1
Version: 0.3
Release: 1%{?dist}
Summary: Color prompt for bash shell
@ -41,6 +41,11 @@ install -m 644 -D -t %{buildroot}%{profiledir} bash-color-prompt.sh
%changelog
* Mon Nov 13 2023 Jens Petersen <petersen@redhat.com> - 0.3-1
- add PROMPT_SEPARATOR and PROMPT_DIRECTORY default variables (#2239152)
- add optional PROMPT_START and PROMPT_END (replaces PROMPT_BRACKETS)
- expand README.md
* Sat Nov 11 2023 Jens Petersen <petersen@redhat.com> - 0.2.1-1
- add a container symbol prefix