Skip to content

Instantly share code, notes, and snippets.

@faho
Created December 14, 2024 09:42
Show Gist options
  • Save faho/db2d7630a6d25492245c2340858c22de to your computer and use it in GitHub Desktop.
Save faho/db2d7630a6d25492245c2340858c22de to your computer and use it in GitHub Desktop.
# Determine how useful terminfo is to us.
#
# We ignore a bunch of useless entries:
# - those invented by ncurses that the terminals don't actually use
# - weird variants that nobody actually uses
# - entries under 256 colors
#
# That last part is meant as a proxy for "modern" to filter out hardware terminals from the 1980s.
# If there is a modern terminal that restricts color on purpose, we can add it.
# In general, terminfo's assumption is that the user would care to configure $TERM. This is wrong.
# And it is unhelpful to ask people to do that, things should work out of the box.
#
# Even after that, we are *still* left with garbage data:
# - terminfo claims "tmux-256color" uses '\E[m^O' for sgr0 and '\EM' for cursor_up.
# this is demonstrably wrong, the regular xterm sequences work.
# - terminfo often claims clear_screen: '\E[H\E[J', '\E[H\E[2J'.
# this is because the xterm entry tells you to clear screen and scrollback,
# which is an extremely spicy and weird decision.
#
set -l ignores \
# Entries dreamed up by ncurses
^nsterm ^Apple_ ^gnome- ^iterm ^iTerm ^konsole- \
^kitty ^vscode ^stterm ^vte- ^ms-terminal ^scrt ^mrxvt- ^terminator \
# Various variants nobody uses (including -direct - everyone sets -256color)
^terminology-\\d ^screen\\. ^screen-256color- ^dvtm- ^xterm\\+ -direct \
# "Absolute Telnet", some awkward windows ssh client
^absolute \
# redundant, "rxvt-unicode" exists
rxvt-256color \
# some proprietary ssh client
securecrt
for f in (path filter -rf /usr/share/terminfo/** | path basename |
string match -rv -- (string join '|' -- $ignores))
set -l colors (tput -T $f colors); and test "$colors" -ge 256 2>/dev/null
and echo -- $f $colors
end >/tmp/plausible-term
# All our used capabilities
printf '%s:\n' enter_bold_mode enter_italics_mode exit_italics_mode enter_dim_mode enter_underline_mode exit_underline_mode enter_reverse_mode \
enter_standout_mode exit_standout_mode exit_attribute_mode clear_screen \
cursor_up cursor_down cursor_left cursor_right parm_left_cursor parm_right_cursor clr_eol clr_eos \
init_tabs eat_newline_glitch auto_right_margin \
key_backspace key_btab key_dc key_down \
# f1 as a proxy for all the f-keys
key_f1 key_home key_ic key_left key_npage key_ppage key_right key_sdc \
key_send key_sf key_shome key_sic key_sleft key_snext key_sprevious key_sr key_sright key_up Ss \
cursor_normal \
>/tmp/used
## only for removing "visual" escapes, never used by us
# enter_blink_mode enter_secure_mode enter_alt_charset_mode exit_alt_charset_mode \
## I have no idea how to trigger these
# key_a1 key_a3 key_b2 key_c1 key_c3 \
## We don't use these because some entries list RGB, some list 256 color palette entries, and max_colors is at least 256 (we filter above)
# set_a_foreground set_a_background max_colors \
while read -l term colors
echo $term
infocmp -x1L $term xterm-256color |
# We ignore keys that this entry doesn't have. That means we just won't get them.
grep -Pv 'key_[^:]+: NULL' |
# Filter it down to caps we use
grep -Ff /tmp/used
end < /tmp/plausible-term
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment