Skip to content

Instantly share code, notes, and snippets.

@rawiriblundell
Last active May 1, 2021 23:55
Show Gist options
  • Save rawiriblundell/cd54153066486ac7a32802eb772ebef3 to your computer and use it in GitHub Desktop.
Save rawiriblundell/cd54153066486ac7a32802eb772ebef3 to your computer and use it in GitHub Desktop.
#!/bin/bash
# A merge of ideas from the following sources
# https://askubuntu.com/q/476641
# https://github.com/dylanaraps/neofetch/blob/master/neofetch
# https://raw.githubusercontent.com/mintty/utils/master/terminal
# If TERM_PROGRAM is set, give it a name that we recognise
# Otherwise, poll the terminal and interpret what it feeds back
# Otherwise, try to glean the information from the PPID
case "${TERM_PROGRAM}" in
("iTerm.app") : iTerm2 ;;
("Terminal.app") : "Apple Terminal" ;;
("Hyper") : HyperTerm ;;
('')
if read -t 1 -r -s -dc -p $'\E[>c' da < /dev/tty; then
da=${da##$'\E'[>}
elif uname -r | grep -qi microsoft; then
da=95
fi
# We want the following word splitting
# shellcheck disable=SC2086
set - ${da//;/ }
case "${1}" in
(0)
: VT100
case "${2}" in
(95) : tmux ;;
(115) : KDE-konsole ;;
(136) : PuTTY ;;
(304) : VT125 ;;
esac
;;
(1)
: VT220
case "${2}" in
(2) : openwin-xterm ;;
(95) : iTerm2 ;;
(96) : mlterm ;;
(304) : VT241/VT382 ;;
(1115) : gnome-terminal ;;
(4000) : kitty ;;
(*)
# This is where gnome-terminal and derivitives usually exist
# This first test would incorrectly identify e.g. lxterminal
#(( $2 >= 2000 )) && : gnome-terminal
(( $2 >= 2000 )) && : "$(ps -p "$(ps -p $$ -o ppid=)" -o args=)"
;;
esac
;;
(2) : VT240 ;;
(24) : VT320 ;;
(18) : VT330 ;;
(19) : VT340 ;;
(41) : VT420 ;;
(61) : VT510 ;;
(64) : VT520 ;;
(65) : VT525 ;;
(28) : DECterm ;;
(67) : cygwin ;;
(77) : mintty ;;
(82) : rxvt ;;
(83) : screen ;;
(84) : tmux ;;
(85) : rxvt-unicode ;;
(95) : WSL ;;
(*)
# Alternative command
#printf -- '%s\n' "$(tr '\0' ' ' </proc/"$(awk '{print $4}' /proc/$$/stat)"/cmdline)"
: "$(ps -p "$(ps -p $$ -o ppid=)" -o args=)"
;;
esac
;;
(*) : "${TERM_PROGRAM}" ;;
esac
export _termtype="$_"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment