Skip to content

Instantly share code, notes, and snippets.

@Summertime
Created October 8, 2019 01:21
Show Gist options
  • Save Summertime/6dae32a1aa8298d8878f420f7322ae6f to your computer and use it in GitHub Desktop.
Save Summertime/6dae32a1aa8298d8878f420f7322ae6f to your computer and use it in GitHub Desktop.
Detect color resolution of the terminal
#!/bin/sh
detect_color_resolution() {
# 24bit
if [ "$COLORTERM" = "truecolor" ]
then
printf '%s\n' 24
return
elif [ "$COLORTERM" = "24bit" ]
then
printf '%s\n' 24
return
fi
# 8bit
case $TERM in
*-256color)
printf '%s\n' 8
return
;;
esac
# lets ask tput, bleh!
local TPUTCOL=$(tput colors)
case $TPUTCOL in
256)
printf '%s\n' 8
return
;;
8)
printf '%s\n' 3
return
;;
*)
printf '%s\n' 0
return
;;
esac
}
printf '%s\n' $(detect_color_resolution)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment