summaryrefslogtreecommitdiff
path: root/app-shells/bash/files
diff options
context:
space:
mode:
authorKerin Millar <kfm@plushkava.net>2024-07-03 01:22:58 +0100
committerSam James <sam@gentoo.org>2024-07-06 23:08:37 +0100
commitee5ffd376d99c6eeba7432c637a7f17438c2f02e (patch)
treea74762ec8690f2241c8d1efccc03d60e69e8a8ac /app-shells/bash/files
parentec5469ca923c9c5bb1607075fb3d9bc090690ef9 (diff)
downloadgentoo-ee5ffd376d99c6eeba7432c637a7f17438c2f02e.tar.gz
gentoo-ee5ffd376d99c6eeba7432c637a7f17438c2f02e.tar.bz2
gentoo-ee5ffd376d99c6eeba7432c637a7f17438c2f02e.zip
app-shells/bash: add 5.2_p26-r7 with XTWINOPS support
Xterm is able to push and pop window titles to a stack and there are a few other terminal emulators that can do so (alacritty and foot being the only ones that I am aware of at the present time). Take advantage of this feature so as to reinstate automatic window title setting in the case that the PTY is owned by sshd(8). Unfortunately, there are a lot of terminal emulators that falsely advertise themselves as being xterm-compatible, making it impossible to reliably identify xterm itself. However, we can reliably identify alacritty and foot so let's support those two to begin with. Have the genfun_set_win_title function export a variable named SHELL_SETS_TITLE upon the first occasion that it is called. Presently, nothing responds to this variable but the intention is to eventually have portage respond to it. Portage implements heuristics and behaviours that are horrifyingly broken. For instance, it considers the mere presence of PROMPT_COMMAND as somehow proving that the interactive shell uses it for nothing other than to set the title, despite the fact that: - the contents of PROMPT_COMMAND may be arbitrarily defined by the user - the purpose of PROMPT_COMMAND is whatever the user may wish it to be - nobody in their right mind would export PROMPT_COMMAND - PROMPT_COMMAND can be an array since 5.1 (making it unexportable) Worse still, in the event that portage is somehow able to ascertain the value of PROMPT_COMMAND, it takes its first element and proceeds to inject its value into an invocation of either sh, $SHELL or bash -c, irrespective of the consequences. No, I'm not making this up. As such, the purpose of the SHELL_SETS_TITLE variable is to act as a straightforward indicator that an interactive shell exists as an ancestor process and that it will take it upon itself to set a fresh window title upon its primary prompt being displayed. Signed-off-by: Kerin Millar <kfm@plushkava.net> Signed-off-by: Sam James <sam@gentoo.org>
Diffstat (limited to 'app-shells/bash/files')
-rw-r--r--app-shells/bash/files/bashrc.d/10-gentoo-title-r1.bash74
1 files changed, 74 insertions, 0 deletions
diff --git a/app-shells/bash/files/bashrc.d/10-gentoo-title-r1.bash b/app-shells/bash/files/bashrc.d/10-gentoo-title-r1.bash
new file mode 100644
index 000000000000..1dc7b63bf422
--- /dev/null
+++ b/app-shells/bash/files/bashrc.d/10-gentoo-title-r1.bash
@@ -0,0 +1,74 @@
+# /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() {
+ 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
+ screen*|tmux*)
+ ;;
+ alacritty|foot*)
+ # 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
+ ;;
+ *)
+ return
+esac
+
+# Arrange for the title to be updated each time the primary prompt is displayed.
+PROMPT_COMMAND+=('genfun_set_win_title')