Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 554086 - app-shells/bash: make PS1 setup more amenable to window title customization
Summary: app-shells/bash: make PS1 setup more amenable to window title customization
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: [OLD] Unspecified (show other bugs)
Hardware: All Linux
: Normal enhancement (vote)
Assignee: Gentoo's Team for Core System packages
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-07-06 17:00 UTC by kfm
Modified: 2024-04-20 07:36 UTC (History)
1 user (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
bash-window-title.patch (bash-window-title.patch,1.56 KB, patch)
2015-07-06 17:00 UTC, kfm
Details | Diff
bash-window-title.patch (amended) (bash-window-title.patch,1.56 KB, patch)
2015-07-06 17:09 UTC, kfm
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description kfm 2015-07-06 17:00:28 UTC
Created attachment 406262 [details, diff]
bash-window-title.patch

Currently, /etc/bash/bashrc primes PS1 so as to define the window title before appending portion that defines the actual prompt. This makes it impossible to customize without modifying bashrc itself. It might not be immediately obvious as to why this may be awkward so I shall explain.

I manage a heterogenous environment, typically using a jump box with tmux. I do not like the default title convention in Gentoo. Instead, I just want the title to be the hostname. Therefore, I constructed a file to be sourced from /etc/bash/bashrc.d in Gentoo, and from /etc/profile.d in Ubuntu then proceeded to deploy it en masse. My method was to set PROMPT_COMMAND, like so:

  # In Ubuntu, the executing shell isn't always bash so discern the shell
  shell="$(/bin/ps -p $$ -o ucomm=)"
  if [ "$shell" = "bash" ] && [ "$TERM" = screen ]; then
    PROMPT_COMMAND="printf '\033k$(hostname -s)\033\\'"
  fi

So there's my first concern. While this worked fine in Ubuntu, the default bashrc in Gentoo prevents this particular use case because PS1 is primed with a sequence to set the title and PROMPT_COMMAND is executed before the prompt is shown.

My second concern is that, while I could just set PS1 from under /etc/bash/bashrc.d - overriding whatever it was before - I would rather not. The reason is that I am happy for the distro to construct the prompt according to its system-wide norms. I only want to override the system-wide behaviour that sets the title. Gentoo does not disambiguate these concerns.

Hence, the only way for me to go about it in the exact way that I want is to have my Puppet recipe push out a replacement /etc/bash/bashrc specifically for my Gentoo machines (removing the bits that deal with titles). This is what bothers me. I would rather be able to stick to a pattern where configuration snippets can be deployed and supplement the standard distro behaviour.

My idea is that the opinionated parts of bashrc could be migrated to default bashrc.d snippets. The advantages would be:

* Behaviour unwanted by the user can be disabled by removing the snippet
* Behaviour the user wants to override can be done by replacing the snippet (not the whole of bashrc)
* It makes for a nicer pattern in terms of cross-distro configuration management

The attached patch demonstrates the concept. It maintains the current default behaviour but does so by prepending to PS1 in "/etc/bash/bashrc.d/00-window-title.sh". Such an approach would provide for a range of methods to override or disable the behaviour. In my case, I would then deploy my replacement "00-window-title.sh" (which sets PROMPT_COMMAND instead).
Comment 1 kfm 2015-07-06 17:09:43 UTC
Created attachment 406264 [details, diff]
bash-window-title.patch (amended)

Same thing but prepends to PS1 as described, as opposed to appending.
Comment 2 Larry the Git Cow gentoo-dev 2024-04-20 07:30:06 UTC
The bug has been referenced in the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=268b2e7c07d97bd9e833d239d786a0314c3b09ec

commit 268b2e7c07d97bd9e833d239d786a0314c3b09ec
Author:     Kerin Millar <kfm@plushkava.net>
AuthorDate: 2024-04-18 04:17:37 +0000
Commit:     Sam James <sam@gentoo.org>
CommitDate: 2024-04-20 07:29:29 +0000

    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>

 app-shells/bash/bash-5.1_p16-r8.ebuild             | 353 ++++++++++++++++++
 app-shells/bash/bash-5.2_p26-r1.ebuild             | 400 +++++++++++++++++++++
 app-shells/bash/files/bashrc-r1                    |  21 ++
 .../bash/files/bashrc.d/10-gentoo-color.bash       |  67 ++++
 .../bash/files/bashrc.d/10-gentoo-title.bash       |  55 +++
 5 files changed, 896 insertions(+)