commit:     b35701d05b15a831d8319c046203f7a70be339cb
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Sat Jun 15 03:59:11 2024 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sat Jun 15 04:39:29 2024 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=b35701d0

app-shells/bash: declare the genfun_set_win_title function unconditionally

Presently, revisions 5.1_p16-r10, 5.2_p26-r3 and bash-5.3_alpha-r2
refrain from declaring the genfun_set_win_title function at all in the
case that the tty belongs to sshd(8). This is to avoid cluttering the
shell's operating environment in situations where the decision is made
not to append 'genfun_set_win_title' to the PROMPT_COMMANDS array.

One might ask why it should not always be appended to the array. The
explanation for this is that Gentoo Linux does not exist in a vacuum;
not all operating systems default to initialising bash in such a way
that it can be assumed that the title will be set at each prompt (or at
all). Where SSH is involved, the server has no knowledge whatsoever of
the particulars of the client OS or its operating environment. This
would previously give rise to the following scenario.

1. User runs ssh(1) from non-Gentoo to connect to sshd(8) on Gentoo
2. The remote shell alters the window title
3. The user eventually exits the remote shell.
4. The window title is never restored to its prior value

Put simply, there is no way for the remote side to know what the
existing window title is, much less guarantee that it be restored on the
client side.

All that being said - and rather unsurprisingly - some Gentoo users will
care nothing for these considerations or are simply operating in a
homogenous environment where they are not an immediate concern. Try to
accommodate the wishes of such users more effectively by declaring the
function unconditionally. Consequently, they will have the option of
restoring Gentoo's historical behaviour in a somewhat straightforward
manner. That is, by setting PROMPT_COMMAND in ~/.bashrc or in an
/etc/bash/bashrc.d/ drop-in to the effect of the following.

PROMPT_COMMAND=(genfun_set_win_title)

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Bug: https://bugs.gentoo.org/934309
Signed-off-by: Sam James <sam <AT> gentoo.org>

 ...-5.1_p16-r10.ebuild => bash-5.1_p16-r11.ebuild} |  0
 ...sh-5.2_p26-r3.ebuild => bash-5.2_p26-r4.ebuild} |  0
 ....3_alpha-r2.ebuild => bash-5.3_alpha-r3.ebuild} |  0
 .../bash/files/bashrc.d/10-gentoo-title.bash       | 49 +++++++++++++---------
 4 files changed, 29 insertions(+), 20 deletions(-)

diff --git a/app-shells/bash/bash-5.1_p16-r10.ebuild 
b/app-shells/bash/bash-5.1_p16-r11.ebuild
similarity index 100%
rename from app-shells/bash/bash-5.1_p16-r10.ebuild
rename to app-shells/bash/bash-5.1_p16-r11.ebuild

diff --git a/app-shells/bash/bash-5.2_p26-r3.ebuild 
b/app-shells/bash/bash-5.2_p26-r4.ebuild
similarity index 100%
rename from app-shells/bash/bash-5.2_p26-r3.ebuild
rename to app-shells/bash/bash-5.2_p26-r4.ebuild

diff --git a/app-shells/bash/bash-5.3_alpha-r2.ebuild 
b/app-shells/bash/bash-5.3_alpha-r3.ebuild
similarity index 100%
rename from app-shells/bash/bash-5.3_alpha-r2.ebuild
rename to app-shells/bash/bash-5.3_alpha-r3.ebuild

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 1fbf17c26327..0246d4fa36f0 100644
--- a/app-shells/bash/files/bashrc.d/10-gentoo-title.bash
+++ b/app-shells/bash/files/bashrc.d/10-gentoo-title.bash
@@ -1,5 +1,31 @@
 # /etc/bash/bashrc.d/10-gentoo-title.bash
 
+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
+       }
+
+       # Sets the 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.
+       genfun_set_win_title() {
+               genfun_sanitise_cwd
+               printf '\033]2;%s@%s - %s\007' "${USER}" "${HOSTNAME%%.*}" 
"${_cwd}"
+       }
+
+       genfun_set_win_title
+}
+
 # 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.
@@ -15,27 +41,14 @@ case ${TERM} in
                # 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.
+               # Users wishing to coerce the historical behaviour have the
+               # option of setting PROMPT_COMMAND=(genfun_set_win_title).
                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.
+# 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 +61,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

Reply via email to