aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xcolour_test17
-rwxr-xr-xpb_colour61
2 files changed, 50 insertions, 28 deletions
diff --git a/colour_test b/colour_test
index 4d903a7..fa691ef 100755
--- a/colour_test
+++ b/colour_test
@@ -2,17 +2,24 @@
# 256 colour functions
# usage:
-source ~/.local/lib/pb_number
-source ./pb_colour
-
-printf %b "$(pb_bg 196)$(pb_fg 254) RED $(pb_nc)\n"
+source "/home/philip/.local/git-repos/bashlib/pb_colour"
+echo $(pb_terminfo)
printf %b "$(pb_ef bold) BOLD TEXT $(pb_nc)\n"
printf %b "$(pb_ef italic) ITALIC TEXT $(pb_nc)\n"
printf %b "$(pb_ef dim) DIM TEXT $(pb_nc)\n"
-printf %b "$(pb_ef underscore) UNDERSCORE TEXT $(pb_nc)\n"
+printf %b "$(pb_ef underline) UNDERLINE TEXT $(pb_nc)\n"
printf %b "$(pb_ef blink) BLINK TEXT $(pb_nc)\n"
printf %b "$(pb_ef reverse) REVERSE TEXT $(pb_nc)\n"
printf %b "$(pb_ef strikethrough) STRIKETHROUGH TEXT $(pb_nc)\n"
+x=$(tput colors)
+if ((x > 256)); then
+ x=256
+fi
+for ((i=0;i<x;i++))
+do
+ printf %b "$(pb_bg "$i") $(pb_fg 0)$i $(pb_nc)"
+done
+printf "\n"
diff --git a/pb_colour b/pb_colour
index 0012205..5fe6240 100755
--- a/pb_colour
+++ b/pb_colour
@@ -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
;;