gentoo-ebuilds/app-shells/bash/files/bashrc.d/10-gentoo-title-r2.bash
Kerin Millar 0193ee11e3
app-shells/bash: add 5.2_p37-r1 with several fixes
Backport two supplemental patches from the devel branch so as to address
some remaining defects affecting the revised read_mbchar() function.
For further information, please refer to pitfall #65 at
https://mywiki.wooledge.org/BashPitfalls, along with the commit messages
of the patches themselves. The read builtin should now behave as well as
it did in 4.4, and as well as it will in the upcoming 5.3-rc2 release.

Whitelist the Contour and Ghostty terminal emulators for colour support.

In the case that colour support is detected, refrain from defining any
aliases for which either an alias or function is found already to exist
by the same name. The motivation for making this change stems from a
discussion at https://forums.gentoo.org/viewtopic-t-1170323.html.

Define COLORTERM=1 in the environment of dircolors(1). The methods by
which dircolors determines whether colour is supported are inferior to
those of Gentoo. Indeed, at the point that dircolors is executed, it
will already have been determined that colour is supported with some
confidence. Defining COLORTERM=1 coerces dircolors into presuming so in
turn, increasing the range of terminal emulators for which Gentoo is
able to define LS_COLORS.

Define _cwd as a local variable in genfun_set_win_title(). Consequently,
_cwd is no longer a reserved variable name from the perspective of the
user.

Whitelist the Contour terminal emulator for XTWINOPS support.

Signed-off-by: Kerin Millar <kfm@plushkava.net>
Signed-off-by: Sam James <sam@gentoo.org>
2025-05-02 07:06:27 +01:00

83 lines
3.1 KiB
Bash

# /etc/bash/bashrc.d/10-gentoo-title.bash
# For information regarding the control sequences used, please refer to
# https://invisible-island.net/xterm/ctlseqs/ctlseqs.html.
genfun_set_win_title() {
# Advertise the fact that the presently running interactive shell will
# update the title. Doing so allows for its subprocesses to determine
# whether it is safe to set the title of their own accord. Note that 0
# refers to the value of Ps within the OSC Ps ; Pt BEL sequence.
export SHELL_SETS_TITLE=0
# 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
}
# Sets the window title with the Set Text Parameters control 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() {
local _cwd
genfun_sanitise_cwd
printf '\033]0;%s@%s - %s\007' "${USER}" "${HOSTNAME%%.*}" "${_cwd}"
}
genfun_set_win_title
}
unset -v SHELL_SETS_TITLE
# 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.
case ${TERM} in
alacritty*|contour|foot*|tmux*)
# The terminal emulator also supports XTWINOPS. If the PTY was
# created by sshd(8) then push the current window title to the
# stack and arrange for it to be popped upon exiting. Xterm also
# supports this but there are far too many terminal emulators
# that falsely identify as being xterm-compatible.
if [[ ${SSH_TTY} && ${SSH_TTY} == "$(tty)" ]]; then
trap 'printf "\033[23;0t"' EXIT
printf '\033[22;0t'
fi
;;
rxvt-unicode*|st-256color|xterm*)
# If the PTY was created by sshd(8) then proceed no further.
# Alas, there exist many operating environments in which the
# title would otherwise not be restored upon ssh(1) exiting.
# Those wanting for the title to be set regardless may adjust
# ~/.bashrc or create a bashrc.d drop-in to set PROMPT_COMMAND.
# For example, PROMPT_COMMAND=(genfun_set_win_title).
if [[ ${SSH_TTY} && ${SSH_TTY} == "$(tty)" ]]; then
return
fi
;;
screen*)
# If the PTY was created by sshd(8) and screen(1) was launched
# prior to the SSH session beginning, as opposed to afterwards,
# proceed no further. It is another case in which there would be
# no guarantee of the title being restored upon ssh(1) exiting.
if [[ ! ${WINDOW} && ${SSH_TTY} && ${SSH_TTY} == "$(tty)" ]]; then
return
fi
;;
*)
return
esac
# Arrange for the title to be updated each time the primary prompt is displayed.
PROMPT_COMMAND+=('genfun_set_win_title')