Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't override TERM #23

Closed
baod-rate opened this issue Jun 1, 2022 · 9 comments
Closed

Don't override TERM #23

baod-rate opened this issue Jun 1, 2022 · 9 comments
Labels
bug Something isn't working

Comments

@baod-rate
Copy link

Exporting TERM here is unexpected and causes issues when there's a mismatch between the capabilities of xterm-256color and your actual terminal. e.g. I expected $terminfo[fsl] and $terminfo[tsl] to be available for iTerm/Alacritty, but it's not available because my environment thinks TERM=xterm-256color

export TERM=xterm-256color

@zthxxx
Copy link
Owner

zthxxx commented Jun 17, 2022

@qubidt Please let me known your issue details with cases,maybe some screenshots to show?

For my case, if it don't set TERM, it will be colorless in screen / tmux

image

@baod-rate
Copy link
Author

simply put, TERM is supposed to reflect the terminal the user is using. Programs will read that variable in their env to see what capabilities the terminal provides (e.g. true-color, blinking, setting the title, etc) as well as to get the actual escape codes the terminal parses to do various things (e.g. set bg/fg color, ring the bell, grab focus, etc.). This is very important for properly processing the keys a user inputs.

setting TERM=xterm-256color in your whole environment basically tells any program that your terminal is xterm, which is inappropriate unless you're sure that's what the user is using (and even then it's unnecessary because the terminal does that on its own). the only reason it seems to work is because most terminals are relatively compatible with xterm

For example, when I'm running in the framebuffer console this propmt would set my TERM to something completely incompatible.

as for screenshots, here is my case with tsl/fsl, I use it to set the window title:
image

but when xterm-256color's terminfo doesn't contain tsl or fsl so that operation fails when TERM is overridden:
image

That's a relatively small issue, there are other visual bugs when the terminal capabilities aren't properly read.

For my case, if it don't set TERM, it will be colorless in screen / tmux

Not sure what your environment's issue is. It might be missing/invalid/outdated terminfo db. Or maybe something is setting TERM incorrectly elsewhere. Tmux's FAQ explains how it interacts with TERM. What terminal emulator are you using and what ist he value of TERM before you start tmux?

Tmux specifically recommends:

PLEASE NOTE: most display problems are due to incorrect TERM! Before
reporting problems make SURE that TERM settings are correct inside and
outside tmux.

Inside tmux TERM must be "screen", "tmux" or similar (such as "tmux-256color").
Don't bother reporting problems where it isn't!

Outside, it should match your terminal: particularly, use "rxvt" for rxvt
and derivatives.

(emphasis mine)

If you want to set TERM inside your tmux environment, the appropriate place to do that is not in your global zshrc but instead in your .tmux.conf:

set -g default-terminal "tmux-256color"

And if you really need to override TERM outside of tmux, you could alias the tmux command:

alias tmux='TERM=xterm-256color tmux`

Or, at the very least, your overriding TERM should be done in your local shell profile, not in this repo, it's not universally applicable at all. The user might very well be using a terminal that is much less compatible with xterm

@zthxxx
Copy link
Owner

zthxxx commented Jun 21, 2022

@qubidt Thanks a lot for explain. I see it maybe cause some problem, but those details seems not a "case" ?

Honestly, I still don't know what really usage scenarios problem that set TERM will cause?

Such as the jovial theme would be break what style when you use it? And what $terminfo[fsl] $terminfo[tsl] used for?

@baod-rate
Copy link
Author

Sorry, I'm not explaining myself well.

tsl and fsl are used to set the status line. This is the window title in tmux but in a regular terminal window it sets the title of the window.

But to give a more straightforward example.

Consider: I want to bind the F1 key.

When you press F1 the terminal emulator (xterm/rxvt/konsole/iterm/etc.) handles the input, translates it to some sequence of escape codes, and sends that to the program (e.g. zsh). You can see what code your terminal outputs by typing Ctrl+v + the key in the terminal. So Ctrl-v + F1 in xterm shows:

image

You can see that matches zsh's terminfo array ($'\033'==^[):

$ printf '%q\n' TERM=$TERM F1=
TERM=xterm-256color
F1=$'\033'OP

xterm will send ^[OP to zsh, and zsh will parse that using $terminfo, and you can use use bindkey with $terminfo:

$ bindkey -s ${terminfo[kf1]} 'echo working!\n'
$ ###press F1 here
$ echo working!
working!

no matter which terminal you are using, the keybind will work because it is specified against $terminfo. for example:

rxvt-unicode uses a different escape code for F1: ^[[11~ instead of ^[OP

$ env | grep TERM; printf '%q\n' F1=${terminfo[kf1]}
COLORTERM=rxvt-xpm
TERM=rxvt-unicode-256color
F1=$'\033'\[11\~
$ bindkey -s ${terminfo[kf1]} 'echo working!\n'
$ ###press F1
$ echo working!
working!

everything still works because zsh can read the escape sequence from $terminfo[kf1]:


However, when zsh sees TERM=xterm-256color, this breaks, because zsh expects ^[OP even though the terminal (rxvt) is sending ^[[11~:

$ env | grep TERM; printf '%q\n' F1=${terminfo[kf1]}
COLORTERM=rxvt-xpm
TERM=xterm-256color
F1=$'\033'OP
$ bindkey -s ${terminfo[kf1]} 'echo working!\n'
$ ###press F1
$ 

this is just one symptom. even if TERM=xterm-256color works most of the
time, there will always be issues like this because every terminal has its own
set of features and escape codes. That's why the OS includes a database with
the capabilities of every terminal:

$ infocmp xterm-256color
#	Reconstructed via infocmp from file: /usr/share/terminfo/x/xterm-256color
xterm-256color|xterm with 256 colors,
	am, bce, ccc, km, mc5i, mir, msgr, npc, xenl,
	colors#0x100, cols#80, it#8, lines#24, pairs#0x10000,
...

$ infocmp rxvt-unicode-256color                                                                  :(
#	Reconstructed via infocmp from file: /usr/share/terminfo/r/rxvt-unicode-256color
rxvt-unicode-256color|rxvt-unicode terminal with 256 colors (X Window System),
	am, bce, bw, ccc, eo, hs, km, mc5i, mir, msgr, npc, xenl, xon,
	btns#5, colors#0x100, cols#80, it#8, lines#24, lm#0, ncv#0,
	pairs#0x7fff,
...

$ ls -lah /usr/share/terminfo/*/* | wc -l
2825

let me know if that makes sense, or if I misunderstood your question

@zthxxx
Copy link
Owner

zthxxx commented Jun 21, 2022

@qubidt Thanks for explaining so many details~ Maybe I need some to understand all about terminfo, in this time, will just set unset TERM; after load the theme file in your .zshrc could solve your issue temporarily?

@baod-rate
Copy link
Author

Happy to help! Unfortunately, doing unset TERM would remove TERM completely, which would be even worse than having TERM=xterm-256color; the value the terminal originally set would be gone. A workaround would be to save TERM before sourcing this script and to restore it afterwards.

Don't worry about me, though, I already patched this script in my local environment. I just wanted to make you and any other users aware, in case anyone else ran into similar issues.

@zthxxx
Copy link
Owner

zthxxx commented Jun 22, 2022

@qubidt I'm sorry about this. Could you show some fragments of .zshrc configuration which you used for me, to reproduce the problem?

@baod-rate
Copy link
Author

Is this what you mean?

cerebro% echo TERM=$TERM
TERM=alacritty
cerebro% source ~/.config/jovial.zsh-theme

╭─[cerebro] as xxx in ~                                                                            06:42:44
╰──➤ echo TERM=$TERM
TERM=xterm-256color

╭─[cerebro] as xxx in ~                                                                            06:42:50
╰──➤ unset TERM

╭─[cerebro] as xxx in ~                                                                            06:43:34
╰──➤ echo TERM=$TERM
TERM=

╭─[cerebro] as xxx in ~                                                                            06:43:41
╰──➤ clear
TERM environment variable not set.                                                                   exit:1

Sourcing jovial.zsh-theme is the only step required to reproduce this

@zthxxx
Copy link
Owner

zthxxx commented Jun 22, 2022

@qubidt Is this appropriate?

# the default `TERM`` in `screen` command is 'linux' which will cause colorless in terminal,
# so set it with a compatible colorful value,
# otherwise shouldn't override TERM because it maybe a specific user setting.
if [[ ${TERM} == 'linux' ]]; then
  export TERM=xterm-256color
fi

Update in v2.3.1.

@zthxxx zthxxx closed this as completed in 248611d Jun 22, 2022
@zthxxx zthxxx added the bug Something isn't working label Jun 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants