Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 680022 - [PATCH] sys-apps/portage: correct terminal window title escape codes for screen or tmux
Summary: [PATCH] sys-apps/portage: correct terminal window title escape codes for scre...
Status: RESOLVED WONTFIX
Alias: None
Product: Portage Development
Classification: Unclassified
Component: Core - External Interaction (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: Portage team
URL:
Whiteboard:
Keywords: PATCH
Depends on:
Blocks:
 
Reported: 2019-03-11 09:56 UTC by Heui-mun Park
Modified: 2024-04-21 23:43 UTC (History)
1 user (show)

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


Attachments
correct escaped codes to change window title in screen/tmux (screen-tmux-title.patch,1.38 KB, patch)
2019-03-11 09:59 UTC, Heui-mun Park
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Heui-mun Park 2019-03-11 09:56:40 UTC
lib/portage/output.py provides xtermTitle() and xtermTitleReset() to set window
titles for various xterm-like terminal emulators by wrapping the string with
escape codes, namely '\x1b]0;%s\x07', which does not work as expected for
terminal multiplexers like screen or tmux, setting their pane titles instead
window titles especially in tmux.  The correct escape codes to change window
title in screen/tmux are '\033k%s\033\\' in octet or '\x1bk%s\x1b\\' in hex
according to their manuals.  But the current emerge outputs '\x1b]0;%s\x07' in
those multiplexers anyway, not changing the window title but overriding the
pane title, especially in tmux with default config showing truncated emerge
status info on the right of its status line.  Although tmux can be configured to
show its active pane title on the window title, this is not a solution and gives
inconsistent user experience as almost no command line program tries to change
its multiplexer's pane title which are left for user's setting but now blindly
overridden and gone by every emerge run.

Since the terminal emulator/multiplexer handles the actual terminal's window
title, the program running under it should follow the escape codes specific to
the emulator/multiplexer in order to change the terminal's window title,
otherwise it's crippled support. The patch fixes this by conditioning the
escape codes to 'TERM' environ matching regex ^(screen|tmux).  Other
multiplexers besides tmux are not tested though.

p.s. For multiplexers, letting emerge set the pane title also could be an
option, but looks overbearing and xtermTitle()/xtermTitleReset() should be
modified to somehow save and restore previous pane title in those multiplexers.


Reproducible: Always

Steps to Reproduce:
1. Without FEATURES="notitles", emerge any package within one of those terminal
   multiplexers, screen or tmux inside xterm or the likes.
2. Watch whether the emerge status info shows up at the window title of the
   terminal emulator.
Actual Results:  
In tmux configured default except allow-rename, emerge's status info does not
show up on the terminal window title but unexpectedly on the right side of its
status line truncated very short.

Expected Results:  
Within a terminal multiplexer or not, emerge's status info shows up on xterm-like
terminal's window title.

Vim also uses wrong escape codes for screen/tmux, but it can be corrected by
changing its 't_ts' and 't_fs' variables. But emerge is hard-coded with the
escape codes, thus needs to be patched.
Comment 1 Heui-mun Park 2019-03-11 09:59:17 UTC
Created attachment 568556 [details, diff]
correct escaped codes to change window title in screen/tmux
Comment 2 Heui-mun Park 2019-03-11 13:33:41 UTC
Need one more setting for tmux: set-titles.

To reproduce the issue with tmux in xterm-like terminals:
1. # emerge tmux
2. $ echo "set -g set-titles on" > ~/.tmux.conf
3. $ echo "setw -g allow-rename on" >> ~/.tmux.conf
4. Fire up xterm or the like and open tmux session in that terminal:
   $ tmux
5. Within the tmux session, check out the terminal window title and the right
   side of tmux status line.  Emerge any package and observe the window title and
   the tmux status line change.  Compared to direct emerging in xterm, the emerge
   status info does not shows up on the xterm window title with the tmux session
   but the truncated and quoted string of it appears on the right side of tmux
   status line.  After the emerge, the title set by the shell prior to emerging
   takes the right side spot as emerge restores it with wrong escape codes.
Comment 3 Heui-mun Park 2019-03-20 17:25:36 UTC
After some testings, I find this can be considered not a portage bug.  In tmux,
letting pane titles be modified by applications running under it with XTerm's
window title escape codes(\033]0; and \007) and setting up tmux to show its
active pane title on the window title (plus, pane title on pane border status)
seems more intriguing.

But this needs somewhat unified setups between applications 'cause now they
have two different ways to influence the window title, disparities in those
configurations can lead to weird outcomes.  For example, if the shell is using
tmux's escape codes, VIM is using XTerm's and tmux is configured with
set-titles-string to show both window title(#W) and pane title(#T) on the
actual window title, recursively repeated title strings as many as times VIM
invocated will happen.  As VIM is saving/restoring actual window title to pane
title and the shell is changing only window title(#W) leaving pane title
untouched, a new VIM starts up again saving previous actual window title which
is still dirty with previous VIM's pane title and so on.  VIM expects saving and
restoring previous window title--current VIM saves the actual window title and
restores it with escape codes set in t_ts and t_fs--would be enough, but in
tmux it's not.  tmux intercepts those two sets of escape codes and handles
application requested window title strings by the code set, using XTerm's codes
to change pane title(#T) and tmux's codes to do window title(#W), leaving what
the actual window title string should be up to user's preference.  Also, tmux
won't allow any application to directly change the actual window title.  To fix
the problem, configuring VIM to save previous pane title in tmux would be too
special.  However, one of the simple workarounds is with the shell's
PROMPT_COMMAND or PS1 to purge dirty pane title VIM left.  Another way to fix is
to set VIM's t_ts and t_fs variable in accordance with the shell's preference
or vice versa.

So it is a problem of configuration mix-up.  Setting up the applications window
title escape codes to be only one set of them fixes the problem, i.e. use only
the window title escape codes(\033k and \033\\) to update window title even by
inactive pane's application or use only the pane title escape codes(\033]2; and
\033\\ from tmux manual or \033]0; and \007 from XTerm) to get more
distinguishable panes.  Portage needs patching to use tmux's window title
escape codes instead of XTerm's.  Bash's default bashrc sets PS1 to use tmux's
when TERM=screen* but user can set up PS1 either way.  VIM uses the pane title
escape codes regardless of TERM value but can configured by setting t_ts t_fs
variables to use either one.  To set PS1 in bashrc to use XTerm's escape codes
and leave others to default would be less of work and more of distinguishable
UX in tmux.