diff options
author | Philip Wittamore <philip@wittamore.com> | 2025-05-18 15:08:02 +0200 |
---|---|---|
committer | Philip Wittamore <philip@wittamore.com> | 2025-05-18 15:08:02 +0200 |
commit | 61026973046b4afa6b57d01f33df7910695f3300 (patch) | |
tree | c6ca4fff635ccb6feb74badcdb690ee96c54bc7f /pb_colour | |
parent | 27577da287d57be67f3dc9be5e7d88430618893c (diff) | |
download | bashlib-61026973046b4afa6b57d01f33df7910695f3300.tar.gz bashlib-61026973046b4afa6b57d01f33df7910695f3300.tar.bz2 bashlib-61026973046b4afa6b57d01f33df7910695f3300.zip |
update
Diffstat (limited to 'pb_colour')
-rwxr-xr-x | pb_colour | 61 |
1 files changed, 38 insertions, 23 deletions
@@ -1,49 +1,64 @@ #!/usr/bin/env bash -#--- 256 colour functions -# requires pb_number +#--- 88/256 colour functions # usage: # source ./pb # printf %b "$(pb_bg 196)$(pb_fg 254) RED $(pb_nc)\n" -pb_nc() { +pb_ctest() { - if [ -z "$TERM" ] || (($(tput colors) != 256)); then - return 1 + if [ -z "$TERM" ] ; then + return 0 + fi + + case $1 in '' | *[!0-9]* ) return 0;; esac + + local x + x=$(tput colors) + + if [ -z "$1" ] || (("$1" > "$x")) ; then + return 0 fi - printf '\033[0;0m'; - return 0 + return 1 } -pb_fg() { +pb_terminfo() { + local x + x=$(tput colors) + echo "TERM = $TERM : $x colours supported" +} + +pb_nc() { - if [ -z "$TERM" ] || (($(tput colors) != 256)); then + if eval pb_ctest ; then + printf '\033[0;0m' + return 0 + else return 1 - fi + fi +} - if [ -z "$1" ] || (("$1" > 255)) || ! pb_isuint "$1" ; then +pb_fg() { + + if eval pb_ctest ; then + printf %s "\033[38;5;$1m" + return 0 + else return 1 fi - printf %s "\033[38;5;$1m" - return 0 - } pb_bg() { - if [ -z "$TERM" ] || (($(tput colors) != 256)); then + if eval pb_ctest ; then + printf %s "\033[48;5;$1m" + return 0 + else return 1 fi - if [ -z "$1" ] || (("$1" > 255)) || ! pb_isuint "$1" ; then - return 1 - fi - - printf %s "\033[48;5;$1m" - return 0 - } pb_ef() { @@ -65,7 +80,7 @@ pb_ef() { return 0 ;; - "underscore") + "underline") printf %s "\033[4m" return 0 ;; |