version 0.4 with PROMPT_USERHOST and colorpre and colorsuf

drop container and PROMPT_ERROR
This commit is contained in:
Jens Petersen 2023-11-14 00:52:17 +08:00
parent 81ed607e32
commit c475f4e18b
3 changed files with 77 additions and 20 deletions

View File

@ -6,7 +6,7 @@ The prompt color theme can be customized simply by setting
the `PROMPT_COLOR` envvar, and optionally `PROMPT_DIR_COLOR`.
For example `PROMPT_COLOR='2;7'` is dim inverse-video
and `PROMPT_COLOR='1;33;53'` is bright/bold yellow with overline.
or `PROMPT_COLOR='44'` gives a blue background.
`PROMPT_DIR_COLOR` similarly changes the color of the working directory,
which otherwise defaults to `PROMPT_COLOR`.
@ -34,11 +34,22 @@ PROMPT_COLOR="$ANSI_COLOR"
See <https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters> for ANSI codes details.
## More variables
## Configurable variables
bash-color-prompt's PS1 has the following general structure:
`[PROMPT_START] PROMPT_USERHOST^PROMPT_SEPARATOR PROMPT_DIRECTORY^[PROMPT_END]`
(`^` means color reset) described below:
### `PROMPT_START` (optional)
Displayed at the start of the prompt.
### `PROMPT_USERHOST`
Default: `PROMPT_USERHOST='\u@\h'`
Defaults to `PROMPT_COLOR`.
See the PROMPTING section of `man bash` or `info bash` for
the available backslash-escaped special characters.
### `PROMPT_SEPARATOR`
Default: `PROMPT_SEPARATOR=':'`
@ -49,9 +60,7 @@ 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.
Defaults to `PROMPT_DIR_COLOR` if set or `PROMPT_COLOR`.
### `PROMPT_END`
Displayed at the end of the prompt (before `\$`).
@ -72,15 +81,52 @@ The prompt can be reset to default with the `prompt_default` function.
Use the `prompt_default_os` function to use `ANSI_COLOR` from `/etc/os-release`.
## 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.
```shellsession
$ prompt_color_force=1
$ source /etc/profile.d/bash-color-prompt.sh
## Multiline prompt example
```
PROMPT_START='\t\n'
PROMPT_COLOR='30;43'
PROMPT_DIR_COLOR='44'
PROMPT_SEPARATOR='\n'
```
You can also set say `PS0='\t'` to timestamp the start of commands.
## Git branch integration
```
function prompt_command {
ref=$(/usr/bin/git rev-parse --abbrev-ref HEAD 2> /dev/null)
git_branch=${ref:+:$ref}
}
PROMPT_COMMAND=prompt_command
PROMPT_END='${git_branch@P}'
```
## Container support
Basic container support can be setup with say:
```
PROMPT_START='${container:+⬢ }'
```
though putting it in `PROMPT_USERHOST` might make more sense.
## Show exit code for error in red
```
PROMPT_END="${colorpre@P}31${colorsuf@P}'${?#0}'"%{colorreset}"
```
## Sourcing
Set `prompt_color_force`
(before sourcing `/etc/bashrc` or `bash-color-prompt.sh` directly)
to turn on bash-color-prompt unconditionally,
otherwise by default it is only setup cautiously
if `$TERM` ends in "color" *and* PS1 is the fedora or bash default.
For example:
```shellsession
if [ -t 0 ]; then
prompt_color_force=1
source /etc/profile.d/bash-color-prompt.sh
fi
```
can be added to `~/.bashrc` to turn on bash-color-prompt "everywhere".
## Contribute
Please open issues against

View File

@ -6,9 +6,14 @@
# 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
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}\$ '
PROMPT_COLOR="${PROMPT_COLOR:-32}"
PROMPT_USERHOST="${PROMPT_USERHOST:-\u@\h}"
PROMPT_SEPARATOR="${PROMPT_SEPARATOR:-:}"
PROMPT_DIRECTORY="${PROMPT_DIRECTORY:-\w}"
colorpre='\[\e['
colorsuf='m\]'
colorreset="${colorpre}0${colorsuf}"
PS1='${PROMPT_START@P}'"${colorpre}"'${PROMPT_COLOR}'"${colorsuf}"'${PROMPT_USERHOST@P}'"${colorreset}"'${PROMPT_SEPARATOR@P}'"${colorpre}"'${PROMPT_DIR_COLOR:-${PROMPT_COLOR}}'"${colorsuf}"'${PROMPT_DIRECTORY@P}'"${colorreset}"'${PROMPT_END@P}\$'"${colorreset} "
prompt_default() {
PROMPT_COLOR="$1"
@ -21,7 +26,7 @@ if [ '(' "$PS1" = "[\u@\h \W]\\$ " -o "$PS1" = "\\s-\\v\\\$ " ')' -a "${TERM: -5
prompt_traditional() {
PROMPT_COLOR="${1:-0}"
PROMPT_DIR_COLOR="${2}"
PROMPT_DIR_COLOR="$2"
PROMPT_SEPARATOR=' '
PROMPT_DIRECTORY='\W'
PROMPT_START='['

View File

@ -1,6 +1,6 @@
Name: shell-color-prompt
Version: 0.3
Release: 2%{?dist}
Version: 0.4
Release: 1%{?dist}
Summary: Color prompt for bash shell
License: GPL-2.0-or-later
@ -41,7 +41,13 @@ install -m 644 -D -t %{buildroot}%{profiledir} bash-color-prompt.sh
%changelog
* Mon Nov 13 2023 Jens Petersen <petersen@redhat.com> - 0.3-2
* Mon Nov 13 2023 Jens Petersen <petersen@redhat.com> - 0.4-1
- define PROMPT_COLOR
- add PROMPT_USERHOST default variable
- add colorpre and colorsuf variables
- drop PROMPT_EPROR
- drop built-in container support for now
- reset color after $ prompt
- only define default functions if setting PS1
* Mon Nov 13 2023 Jens Petersen <petersen@redhat.com> - 0.3-1