From c611d04191a77909792d760aa0a40cf0a6cef841 Mon Sep 17 00:00:00 2001 From: Alarig Le Lay Date: Mon, 1 Jul 2024 14:36:09 +0200 Subject: [PATCH] app-shells/bash/files/bashrc.d: files from ::gentoo Signed-off-by: Alarig Le Lay --- .../bash/files/bashrc.d/10-gentoo-color.bash | 3 +- .../bash/files/bashrc.d/10-gentoo-title.bash | 68 +++++++++---------- .../bashrc.d/15-gentoo-bashrc-check.bash | 24 +++++++ 3 files changed, 58 insertions(+), 37 deletions(-) create mode 100644 app-shells/bash/files/bashrc.d/15-gentoo-bashrc-check.bash diff --git a/app-shells/bash/files/bashrc.d/10-gentoo-color.bash b/app-shells/bash/files/bashrc.d/10-gentoo-color.bash index 6192bfa..f0c5983 100644 --- a/app-shells/bash/files/bashrc.d/10-gentoo-color.bash +++ b/app-shells/bash/files/bashrc.d/10-gentoo-color.bash @@ -34,7 +34,8 @@ elif (( gentoo_color == 16777216 )); then export COLORTERM=truecolor fi -if (( gentoo_color <= 0 )) || ! genfun_has_readline; then +# For direxpand to be missing indicates that bash is lacking readline support. +if (( gentoo_color <= 0 )) || [[ ! $(shopt -p direxpand 2>/dev/null) ]]; then # Define a prompt without color. PS1='\u@\h \w \$ ' elif (( EUID == 0 )); then diff --git a/app-shells/bash/files/bashrc.d/10-gentoo-title.bash b/app-shells/bash/files/bashrc.d/10-gentoo-title.bash index 1fbf17c..2b2e233 100644 --- a/app-shells/bash/files/bashrc.d/10-gentoo-title.bash +++ b/app-shells/bash/files/bashrc.d/10-gentoo-title.bash @@ -1,41 +1,41 @@ # /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 +genfun_set_win_title() { + # 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 expansion, for reasons of cleanliness. + genfun_sanitise_cwd() { + _cwd=${PWD##*/} + if [[ ! ${_cwd} ]]; then + _cwd=${PWD} + elif [[ ${_cwd} == *[![:graph:]]* ]]; then + _cwd=${_cwd@Q} 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 + # Sets the window title with the Set Text Parameters sequence. For + # screen, the sequence defines the hardstatus (%h) and for tmux, the + # pane_title (#T). For graphical terminal emulators, it is normal for + # the title bar to be affected. + genfun_set_win_title() { + genfun_sanitise_cwd + printf '\033]0;%s@%s - %s\007' "${USER}" "${HOSTNAME%%.*}" "${_cwd}" + } + + genfun_set_win_title } -# 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. +# Proceed no further if the TTY is that of sshd(8) and if not running a terminal +# multiplexer. Alas, there exist many operating environments in which the window +# title would otherwise not be restored upon ssh(1) exiting. Those who wish for +# the title to be set unconditionally may adjust ~/.bashrc - or create a custom +# bashrc.d drop-in - to define PROMPT_COMMAND=(genfun_set_win_title). +if [[ ${SSH_TTY} && ${TERM} != @(screen|tmux)* && ${SSH_TTY} == "$(tty)" ]]; then + return +fi + +# Determine whether the terminal can handle the Set Text Parameters sequence. # 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. @@ -48,9 +48,5 @@ case ${TERM} in st-256color |\ 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 diff --git a/app-shells/bash/files/bashrc.d/15-gentoo-bashrc-check.bash b/app-shells/bash/files/bashrc.d/15-gentoo-bashrc-check.bash new file mode 100644 index 0000000..8f2b040 --- /dev/null +++ b/app-shells/bash/files/bashrc.d/15-gentoo-bashrc-check.bash @@ -0,0 +1,24 @@ +# /etc/bash/bashrc.d/15-gentoo-bashrc-check.bash + +# Some users have ~/.bashrc as a copy of ${FILESDIR}/bashrc which either matches +# exactly or is only trivially modified. Such is an improper state of affairs +# and results in the bashrc.d drop-ins being sourced twice. Warn them that they +# should use the skeleton file instead. This drop-in should be removed no sooner +# than one year from the date of its introduction. + +if [[ -e ${TMPDIR:-/tmp}/.gentoo-bashrc-check-${EUID} || ! -f ~/.bashrc ]]; then + return +fi + +{ + if grep -qxF 'for sh in /etc/bash/bashrc.d/* ; do' -- ~/.bashrc; then + cat >&3 <<'EOF' +WARNING! Your ~/.bashrc file is based on an old copy of /etc/bash/bashrc, which +is not intended for use within a home directory. Please either delete ~/.bashrc +or replace it with a copy of /etc/skel/.bashrc before optionally customizing it +further. Neglecting to do so may result in bash behaving unexpectedly. + +EOF + fi + touch -- "${TMPDIR:-/tmp}/.gentoo-bashrc-check-${EUID}" +} 3>&2 2>/dev/null