diff options
| author | Kerin Millar <kfm@plushkava.net> | 2024-04-18 05:17:37 +0100 |
|---|---|---|
| committer | Sam James <sam@gentoo.org> | 2024-04-20 08:29:29 +0100 |
| commit | 268b2e7c07d97bd9e833d239d786a0314c3b09ec (patch) | |
| tree | db6ae8097b33820539211f7fa21bc53abda9f70c /app-shells/bash/files | |
| parent | bef7e2e81feac111a926eec4f2697a3e0901ae7a (diff) | |
| download | gentoo-268b2e7c07d97bd9e833d239d786a0314c3b09ec.tar.gz gentoo-268b2e7c07d97bd9e833d239d786a0314c3b09ec.tar.bz2 gentoo-268b2e7c07d97bd9e833d239d786a0314c3b09ec.zip | |
app-shells/bash: restructure the system-wide bashrc for 5.1 and 5.2
This commit simplifies /etc/bash/bashrc by separating out the
functionality that is relatively complicated - perhaps even opinionated
on the part of Gentoo - into files that are installed to the
/etc/bash/bashrc.d directory. The intention is to preserve the overall
Gentoo flavour, while making it easier for users to customise their
operating environment than was the case before, and to be able to easily
suppress functionality that they may not wish for. The exact changes are
described herewith.
No longer will a superfluous error message be printed in the case that
bash was not compiled with readline support.
Files within /etc/bash/bashrc.d must now have a suffix of either ".sh"
or ".bash" in order to be sourced. This better reflects the way in which
/etc/profile works and should be a little safer.
Two new files are introduced:
- /etc/bash/bashrc.d/10-gentoo-color.bash
- /etc/bash/bashrc.d/10-gentoo-title.bash
Users may suppress these with INSTALL_MASK, should they wish to do so.
The NO_COLOR variable is now respected, provided that is is defined
prior to the sourcing of 10-gentoo-color.bash. It should be noted that
ssh users have the option of transmitting this variable by configuring
both ssh(1) and sshd(8) accordingly.
The way in which terminals are evaluated for colour support has been
greatly improved. There are now three heuristics involved. The first
method is to determine whether COLORTERM is already set as an
environment variable. This is an effective method because modern
terminal emulators commonly set the variable so as to advertise 24-bit
colour support. Further, Gentoo already whitelists the COLORTERM
variable in both ssh(1) and sshd(8). The second method is to use the
ncurses implementation of tput(1) to determine whether colour is
supported. The third method is to fall back to a traditional whitelist
of TERM patterns. However, I have overhauled this list based on an
arduous survey of terminal emulators during which I collected empirical
evidence as to which of them actually belong on the list. As such, the
coverage of this method of last resort is broader.
The COLORTERM variable will now be set for terminal emulators that are
found to support 24-bit colour but which do not set the variable by
themselves.
Colour-supporting aliases will now be defined for all of the following
utilities: diff, dir, egrep, fgrep, grep, ls and vdir.
Out of an abundance of caution, the -- operand is now used to signify
end-of-options in the case that dircolors(1) is being passed a pathname
incorporating the user's home directory.
PROMPT_COMMAND will now be defined as an array, as is supported for bash
5.1 or greater. It is more convenient because additional commands can
simply be appended to the array.
No longer will the "Title Definition String" and/or "Set Text Parameter"
sequences be injected into the value of PS1. This keeps the value of PS1
clean and results in fewer side effects in the event that the user
wishes to customise the prompt.
PROMPT_COMMAND will now be used to contain commands that print the
"Title Definition String" and/or "Text Parameter Sequences", depending
on the characteristics of the operating environment. The precise
behaviour is conveyed from hereon.
If the value of TERM is found to be that of the screen or tmux terminal
multiplexers, PROMPT_COMMAND will be set so as to invoke a function that
prints the Title Definition String sequence. The effect of the sequence
is to define the window title for screen, and the pane title for tmux.
The title will incoporate the hostname in short form.
If, on the other hand, the value of TERM is not found to be that of a
terminal multiplexer, a test is performed to see whether the tty is that
of sshd(8). If it is, then no further processing will occur. The reason
for this is it that there is no way for Gentoo to know the
characteristics of the operating environment where ssh(1) happens to be
running at the time. Sadly, there are many cases in which the window
title would simply not be restored after ssh(1) exists, which amounts to
a poor user experience.
Assuming that processing has not ceased at this point, the value of TERM
will be matched against a whitelist of modern terminals that are known
to support the Set Text Parameters Sequence, and to support UTF-8
correctly. If a match is made then PROMPT_COMMAND will be amended so as
to invoke a function that prints the aforementioned sequence. The effect
of the sequence is to define the hardstatus for screen, the window name
for tmux and the window title for graphical terminal emulators. The
title will incorporate the username, the hostname in short form and the
basename of the current working directory. Said basename will be
sanitised where appropriate, by employing the ${param@Q} form of
parameter expansion. Doing so improves the user experience by ensuring
that, where the basename contains anything other than (visible)
graphemes, the title will always show a valid, legible shell word.
It should be noted that users may now easily opt out of the title
setting behaviour by either unsetting PROMPT_COMMAND or by re-defining
it, which was not possible before. At the same time, users that like to
customise the value of PROMPT_COMMAND now have the option of appending
their custom commands to the array, duly preserving the default Gentoo
behaviour.
Signed-off-by: Kerin Millar <kfm@plushkava.net>
Bug: https://bugs.gentoo.org/show_bug.cgi?id=554086
Bug: https://bugs.gentoo.org/show_bug.cgi?id=926742
Signed-off-by: Sam James <sam@gentoo.org>
Diffstat (limited to 'app-shells/bash/files')
| -rw-r--r-- | app-shells/bash/files/bashrc-r1 | 21 | ||||
| -rw-r--r-- | app-shells/bash/files/bashrc.d/10-gentoo-color.bash | 67 | ||||
| -rw-r--r-- | app-shells/bash/files/bashrc.d/10-gentoo-title.bash | 55 |
3 files changed, 143 insertions, 0 deletions
diff --git a/app-shells/bash/files/bashrc-r1 b/app-shells/bash/files/bashrc-r1 new file mode 100644 index 000000000000..61202b61f141 --- /dev/null +++ b/app-shells/bash/files/bashrc-r1 @@ -0,0 +1,21 @@ +# /etc/bash/bashrc + +# Proceed no further in the case of a non-interactive shell. +if [[ $- != *i* ]]; then + return +fi + +# Disable completion when the input buffer is empty. Requires readline support. +shopt -s no_empty_cmd_completion 2>/dev/null + +# Append to HISTFILE rather than overwrite upon exiting, per bug #139609. +shopt -s histappend + +# Initialise PROMPT_COMMAND as an array, which is permitted as of bash 5.1. +PROMPT_COMMAND=() + +for _ in /etc/bash/bashrc.d/*; do + if [[ $_ == *.@(bash|sh) && -r $_ ]]; then + source "$_" + fi +done diff --git a/app-shells/bash/files/bashrc.d/10-gentoo-color.bash b/app-shells/bash/files/bashrc.d/10-gentoo-color.bash new file mode 100644 index 000000000000..5a6df5690c08 --- /dev/null +++ b/app-shells/bash/files/bashrc.d/10-gentoo-color.bash @@ -0,0 +1,67 @@ +# /etc/bash/bashrc.d/10-gentoo-color.bash + +if [[ ${NO_COLOR} ]]; then + # Respect the user's wish not to use color. See https://no-color.org/. + gentoo_color=0 +elif [[ ${COLORTERM@a} == *x* && ${COLORTERM} == @(24bit|truecolor) ]]; then + # The COLORTERM environment variable can reasonably be trusted here. + # See https://github.com/termstandard/colors for further information. + gentoo_color=1 +elif unset -v COLORTERM; ! gentoo_color=$(tput colors 2>/dev/null); then + # Either ncurses is not installed or no terminfo database could be + # found. Fall back to a whitelist which covers the majority of terminal + # emulators and virtual console implementations known to support color + # and which remain (somewhat) popular. This will rarely happen, so the + # list need not be exhaustive. + case ${TERM} in + *color* |\ + *direct* |\ + [Ekx]term* |\ + alacritty |\ + aterm |\ + dtterm |\ + foot* |\ + jfbterm |\ + linux |\ + mlterm |\ + rxvt* |\ + screen* |\ + tmux* |\ + wsvt25* ) gentoo_color=1 + esac +elif (( gentoo_color == 16777216 )); then + # Truecolor support is available. Advertise it. + export COLORTERM=truecolor +fi + +if (( gentoo_color <= 0 )); then + # Define a prompt without color. + PS1='\u@\h \w \$ ' +elif (( EUID == 0 )); then + # If root, omit the username and print the hostname in red. + PS1='\[\e[01;31m\]\h\[\e[01;34m\] \w \$\[\e[00m\] ' +else + # Otherwise, print the username and hostname in green. + PS1='\[\e[01;32m\]\u@\h\[\e[01;34m\] \w \$\[\e[00m\] ' +fi + +if (( gentoo_color > 0 )); then + # Colorize the output of grep and several coreutils utilities. + for _ in diff dir egrep fgrep grep ls vdir; do + alias "$_=$_ --color=auto" + done + + # Enable colors for ls(1) and some other utilities that respect the + # LS_COLORS variable. Prefer ~/.dir_colors, per bug #64489. + if hash dircolors 2>/dev/null; then + if [[ -f ~/.dir_colors ]]; then + eval "$(dircolors -b -- ~/.dir_colors)" + elif [[ -f /etc/DIR_COLORS ]]; then + eval "$(dircolors -b /etc/DIR_COLORS)" + else + eval "$(dircolors -b)" + fi + fi +fi + +unset -v gentoo_color diff --git a/app-shells/bash/files/bashrc.d/10-gentoo-title.bash b/app-shells/bash/files/bashrc.d/10-gentoo-title.bash new file mode 100644 index 000000000000..56afcf213045 --- /dev/null +++ b/app-shells/bash/files/bashrc.d/10-gentoo-title.bash @@ -0,0 +1,55 @@ +# /etc/bash/bashrc.d/10-gentoo-title.bash + +# Set window title with the Title Definition String sequence. For screen, the +# sequence defines the window title (%t) and for tmux, the pane_title (#T). +# For tmux to be affected requires that its allow-rename option be enabled. +# https://www.gnu.org/software/screen/manual/html_node/Control-Sequences.html +case ${TERM} in + screen*|tmux*) + genfun_set_pane_title() { + printf '\033k%s\033\\' "${HOSTNAME%%.*}" + } + PROMPT_COMMAND+=('genfun_set_pane_title') + ;; + *) + # If the TTY is that of sshd(8) then proceed no further. Alas, + # there exist many operating environments in which the window + # title would otherwise not be restored upon ssh(1) exiting. + if [[ ${SSH_TTY} && ${SSH_TTY} == "$(tty)" ]]; then + return + fi +esac + +# Assigns the basename of the current working directory, having sanitised it +# with @Q parameter expansion. Useful for paths containing newlines and such. +# As a special case, names consisting entirely of graphemes shall not undergo +# the parameter expansion, for reasons of cleanliness. +genfun_sanitise_cwd() { + _cwd=${PWD##*/} + if [[ ! ${_cwd} ]]; then + _cwd=${PWD} + elif [[ ${_cwd} == *[![:graph:]]* ]]; then + _cwd=${_cwd@Q} + fi +} + +# Set window title with the Set Text Parameters sequence. For screen, the +# sequence defines the hardstatus (%h) and for tmux, the window_name (#W). +# For graphical terminal emulators, it is normal for the title bar be affected. +# The only terminals permitted here are those for which there is empirical +# evidence that the sequence is supported and that the UTF-8 character encoding +# is handled correctly. Quite rightly, this precludes many vintage terminals. +# https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands +case ${TERM} in + alacritty |\ + foot* |\ + rxvt-unicode* |\ + screen* |\ + tmux* |\ + xterm* ) + genfun_set_win_title() { + genfun_sanitise_cwd + printf '\033]2;%s@%s - %s\007' "${USER}" "${HOSTNAME%%.*}" "${_cwd}" + } + PROMPT_COMMAND+=('genfun_set_win_title') +esac |
