Skip to content

Instantly share code, notes, and snippets.

@rawiriblundell
Last active November 13, 2019 04:06
Show Gist options
  • Save rawiriblundell/83ed9408a7e3032c780ed56b7c9026f2 to your computer and use it in GitHub Desktop.
Save rawiriblundell/83ed9408a7e3032c780ed56b7c9026f2 to your computer and use it in GitHub Desktop.
Overlay tput to make it work portably
# Detect if our version of 'tput' is so old that it uses termcap syntax
# If this is the case, overlay it so that newer terminfo style syntax works
# Inspired by 'bashlib' and 'liquidprompt'
# For performance we only implement if 'tput ce' (a harmless test) works
if tput ce 2>/dev/null; then
tput() {
ctput-null() { command tput "${@}" 2>/dev/null; }
ctput() { command tput "${@}"; }
case "${1}" in
(blink) ctput-null blink || ctput mb;;
(bold) ctput-null bold || ctput md;;
(civis) ctput-null civis || ctput vi;;
(cnorm) ctput-null cnorm || ctput ve;;
(cols) ctput-null cols || ctput co;;
(dim) ctput-null dim || ctput mh;;
(ed) ctput-null ed || ctput cd;;
(el) ctput-null el || ctput ce;;
(el1) ctput-null el1 || ctput cb;;
(lines) ctput-null lines || ctput li;;
(ritm) ctput-null ritm || ctput ZR;;
(rmcup) ctput-null rmcup || ctput te;;
(rmso) ctput-null rmso || ctput se;;
(rmul) ctput-null rmul || ctput ue;;
(setaf)
case $(uname) in
(FreeBSD) ctput AF "${2}";;
(OpenBSD) ctput AF "${2}" 0 0;;
(*) ctput setaf "${2}";;
esac
;;
(setab)
case $(uname) in
(FreeBSD) ctput AB "${2}";;
(OpenBSD) ctput AB "${2}" 0 0;;
(*) ctput setab "${2}";;
esac
;;
(sgr0) ctput-null sgr0 || ctput me;;
(sitm) ctput-null sitm || ctput ZH;;
(smcup) ctput-null smcup || ctput ti;;
(smso) ctput-null smso || ctput so;;
(smul) ctput-null smul || ctput us;;
(*) ctput "${@}";;
esac
}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment