diff --git a/doc/profile.conf.example b/doc/profile.conf.example index 2610cb7..a771d02 100755 --- a/doc/profile.conf.example +++ b/doc/profile.conf.example @@ -21,8 +21,10 @@ HISTIGNORE="&:[bf]g:exit" # Default pager PAGER=less -# Terminal colour capability -TERM=xterm-256color +# Terminal type. +# smart — auto-detect the best available capability at startup (default) +# — force a specific terminfo entry, e.g. xterm-256color or vt100 +#TERM=smart # ============================================================================== [compress] diff --git a/profile.d/conf.sh b/profile.d/conf.sh index 9cfac55..1cf7691 100755 --- a/profile.d/conf.sh +++ b/profile.d/conf.sh @@ -256,4 +256,97 @@ export -f conf_dump # ------------------------------------------------------------------------------ +# ------------------------------------------------------------------------------ +# Set TERM to the best terminal capability available on this system. +# Called automatically at sourcing time. +# +# If TERM is already set to a specific value (not empty, not the sentinel +# "smart"), it is honoured as-is — standard Unix behaviour. +# If TERM is empty or set to "smart", terminal emulator hints are checked first +# (COLORTERM, TERM_PROGRAM, VTE_VERSION, WT_SESSION, TMUX, STY), then terminfo +# is probed in preference order: xterm-256color → xterm-color → xterm → vt100. +# +# Usage: term_set +term_set() +{ + local _current="${TERM:-}" + + # Specific value already set — nothing to do. + if [[ -n "$_current" && "$_current" != "smart" ]]; then + export TERM="$_current" + return 0 + fi + + # Return true when terminfo has an entry for the given terminal type. + local _term_has + _term_has() { tput -T "$1" longname >/dev/null 2>&1; } + + local _candidate="" + + # 1. Explicit truecolor hint set by modern terminal emulators. + if [[ "${COLORTERM:-}" == "truecolor" || "${COLORTERM:-}" == "24bit" ]]; then + local _t + for _t in xterm-direct xterm-256color; do + _term_has "$_t" && { _candidate="$_t"; break; } + done + fi + + # 2. Terminal programme name hints. + if [[ -z "$_candidate" ]]; then + case "${TERM_PROGRAM:-}" in + iTerm.app) + _term_has "xterm-256color" && _candidate="xterm-256color" + ;; + WezTerm) + _term_has "wezterm" && _candidate="wezterm" + [[ -z "$_candidate" ]] && _term_has "xterm-256color" && _candidate="xterm-256color" + ;; + Hyper|vscode) + _term_has "xterm-256color" && _candidate="xterm-256color" + ;; + esac + fi + + # 3. VTE-based terminals (GNOME Terminal, Tilix, Xfce Terminal, …). + if [[ -z "$_candidate" && -n "${VTE_VERSION:-}" ]]; then + _term_has "vte-256color" && _candidate="vte-256color" + [[ -z "$_candidate" ]] && _term_has "xterm-256color" && _candidate="xterm-256color" + fi + + # 4. Windows Terminal. + if [[ -z "$_candidate" && -n "${WT_SESSION:-}" ]]; then + _term_has "xterm-256color" && _candidate="xterm-256color" + fi + + # 5. tmux — prefer tmux-256color, fall back to screen-256color. + if [[ -z "$_candidate" && -n "${TMUX:-}" ]]; then + _term_has "tmux-256color" && _candidate="tmux-256color" + [[ -z "$_candidate" ]] && _term_has "screen-256color" && _candidate="screen-256color" + fi + + # 6. GNU screen — prefer screen-256color, fall back to screen. + if [[ -z "$_candidate" && -n "${STY:-}" ]]; then + _term_has "screen-256color" && _candidate="screen-256color" + [[ -z "$_candidate" ]] && _term_has "screen" && _candidate="screen" + fi + + # 7. Generic terminfo probe in preference order. + if [[ -z "$_candidate" ]]; then + local _t + for _t in xterm-256color xterm-color xterm vt100; do + _term_has "$_t" && { _candidate="$_t"; break; } + done + fi + + unset -f _term_has + + export TERM="${_candidate:-vt100}" +} +export -f term_set +# ------------------------------------------------------------------------------ + + +term_set + + # EOF diff --git a/profile.d/help.sh b/profile.d/help.sh index dc80753..df559de 100644 --- a/profile.d/help.sh +++ b/profile.d/help.sh @@ -104,6 +104,7 @@ help() printf "showinfo\tDisplay welcome banner and system information (figlet + neofetch/fastfetch)\n" printf "ssr\t\tSSH into a server as root, forwarding extra ssh options\n" printf "taz\t\tCompress files and directories into a chosen archive format\n" + printf "term_set\tSet TERM to the best available terminal capability (auto-detect or honour config)\n" printf "urlencode\tURL-encode a string\n" printf "utaz\t\tSmartly uncompress archives (zip, tar.gz/bz2/xz/lz, rar, arj, lha, ace, 7z, zst, cpio, cab, deb, rpm)\n" printf "ver\t\tDisplay the installed profile version\n\n"