Skip to content

Instantly share code, notes, and snippets.

@agriffis
Last active September 17, 2023 20:09
Show Gist options
  • Save agriffis/a96617fee0d45b3afc015509b9763d69 to your computer and use it in GitHub Desktop.
Save agriffis/a96617fee0d45b3afc015509b9763d69 to your computer and use it in GitHub Desktop.
TERM in bash and nushell
setup-TERM() {
# vte-256color messes up emacs with solarized, so use xterm-256color
# instead.
declare vte=xterm-256color
declare -a terms
case $TERM:$COLORTERM in
ansi:*)
# probably Windows telnet
TERM=vt100 ;;
screen*|tmux*:*)
# leave it alone! don't trip on the following vte checks
;;
*:roxterm|xterm:)
if [[ $ROXTERM_PID == "$PPID" ]]; then
TERM=$vte
elif [[ -e /etc/crouton ]]; then
# https://groups.google.com/a/chromium.org/forum/#!topic/chromium-hterm/2EmfR2Mac88
TERM=xterm-256color
elif [[ $(xargs -0 < /proc/$PPID/cmdline) == *gnome-terminal* ]] 2>/dev/null; then
# gnome-terminal stopped setting COLORTERM=gnome-terminal
# https://github.com/GNOME/gnome-terminal/commit/1d5c1b6ca6373c1301494edbc9e43c3e6a9c9aaf
TERM=$vte
fi
;;
*:Terminal|*:gnome-terminal)
TERM=$vte ;;
esac
case $TERM in
alacritty)
terms=("$TERM" xterm-256color xterm) ;;
rxvt-*)
terms=("$TERM" rxvt xterm) ;;
xterm-256color|xterm-kitty)
terms=("$TERM" xterm-256color xterm) ;;
screen*)
terms=(screen-256color "$TERM" screen) ;;
tmux*)
terms=(tmux-256color screen-256color "$TERM" tmux screen) ;;
esac
if [[ -n $terms ]]; then
# toe on gentoo searches only /usr/share/terminfo.
# toe on debian/unbuntu searches only ~/.terminfo and /etc/terminfo.
# both appear to be broken, so we search explicitly.
declare -a toes
toes=($(
for d in ~/.terminfo /etc/terminfo /lib/terminfo /usr/share/terminfo; do
[[ -d $d ]] && toe "$d"
done | awk '{print $1}'
))
for term in "${terms[@]}"; do
for toe in "${toes[@]}"; do
if [[ $term == "$toe" ]]; then
TERM=$term
return
fi
done
done
fi
}
setup-TERM
unset -f setup-TERM
$env.TERM = (
# Start with whatever is in TERM already.
($env | get -i 'TERM' | default-empty 'xterm') |
# Do some replacements, optimistically upgrading xterm to xterm-256color for
# the moment.
(
if $in == 'ansi' {'vt100'}
else if ($in == 'xterm') {'xterm-256color'}
else if ($in | find -r '^screen|^tmux' | is-not-empty) {$in}
else if ($env | get -i 'COLORTERM' | default '' |
find -r '^roxterm$|^Terminal$|^gnome-terminal$' | is-not-empty)
{'xterm-256color'}
else {$in}
) |
# Convert to list with fallbacks, for example:
# xterm-kitty -> [xterm-kitty xterm-256color xterm]
(
if (['alacritty' 'xterm-256color' 'xterm-kitty'] | any {|it| str contains $it}) {
[$in 'xterm-256color' 'xterm']
} else if ($in | str starts-with 'rxvt-') {
[$in 'rxvt' 'xterm']
} else if ($in | str starts-with 'screen') {
['screen-256color' $in 'screen']
} else if ($in | str starts-with 'tmux') {
['tmux-256color' 'screen-256color' $in 'tmux' 'screen']
} else {
[$in]
}
) |
# Now that we have a prioritized list with fallbacks, choose the first that
# is available on the current system.
(do {
let terms = $in
# Don't call external toe command. It's slow and doesn't work the same on
# all Linux distros.
let toes = (
['~/.terminfo' '/etc/terminfo' '/lib/terminfo' '/usr/share/terminfo'] |
reduce -f [] {|d, acc|
$acc ++ (
try {ls ([$d * *] | path join) | get name | path basename}
catch {[]}
)
}
)
for term in $terms {
if ($toes | any {|toe| $term == $toe}) {
return $term
}
}
return ($terms | last) # most conservative
})
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment