summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-05-08 06:39:55 -0300
committerEuAndreh <eu@euandre.org>2024-05-08 08:24:03 -0300
commitbe706800a21556c21db90d01a50cc69ab510b2a7 (patch)
tree5fd94984c78e23249e1a899dc97846fc6d2ce466
parentsrc/assert-arg: Add working utility (diff)
downloadeut-be706800a21556c21db90d01a50cc69ab510b2a7.tar.gz
eut-be706800a21556c21db90d01a50cc69ab510b2a7.tar.xz
src/color: Add working utility
-rw-r--r--deps.mk1
-rwxr-xr-xsrc/color167
2 files changed, 168 insertions, 0 deletions
diff --git a/deps.mk b/deps.mk
index b88fd94..f8fcde1 100644
--- a/deps.mk
+++ b/deps.mk
@@ -1,5 +1,6 @@
sources.sh = \
src/assert-arg \
+ src/color \
src/now \
src/timestamp \
src/uuid \
diff --git a/src/color b/src/color
new file mode 100755
index 0000000..51c4f40
--- /dev/null
+++ b/src/color
@@ -0,0 +1,167 @@
+#!/bin/sh
+set -eu
+
+usage() {
+ cat <<-'EOF'
+ Usage:
+ color -c COLOR TEXT
+ color -l
+ EOF
+}
+
+END="\033[0m"
+
+black() {
+ BLACK="\033[0;30m"
+ printf "${BLACK}${1}${END}"
+}
+
+blackb() {
+ BLACK_B="\033[1;30m"
+ printf "${BLACK_B}${1}${END}"
+}
+
+blacki() {
+ BLACK_I="\033[0;90m"
+ printf "${BLACK_I}${1}${END}"
+}
+
+white() {
+ WHITE="\033[0;37m"
+ printf "${WHITE}${1}${END}"
+}
+
+whiteb() {
+ WHITE_B="\033[1;37m"
+ printf "${WHITE_B}${1}${END}"
+}
+
+red() {
+ RED="\033[0;31m"
+ printf "${RED}${1}${END}"
+}
+
+redb() {
+ RED_B="\033[1;31m"
+ printf "${RED_B}${1}${END}"
+}
+
+green() {
+ GREEN="\033[0;32m"
+ printf "${GREEN}${1}${END}"
+}
+
+greenb() {
+ GREEN_B="\033[1;32m"
+ printf "${GREEN_B}${1}${END}"
+}
+
+yellow() {
+ YELLOW="\033[0;33m"
+ printf "${YELLOW}${1}${END}"
+}
+
+yellowb() {
+ YELLOW_B="\033[1;33m"
+ printf "${YELLOW_B}${1}${END}"
+}
+
+blue() {
+ BLUE="\033[0;34m"
+ printf "${BLUE}${1}${END}"
+}
+
+blueb() {
+ BLUE_B="\033[1;34m"
+ printf "${BLUE_B}${1}${END}"
+}
+
+bluei() {
+ BLUE_I="\033[0;94m"
+ printf "${BLUE_I}${1}${END}"
+}
+
+purple() {
+ PURPLE="\033[0;35m"
+ printf "${PURPLE}${1}${END}"
+}
+
+
+purpleb() {
+ PURPLE_B="\033[1;35m"
+ printf "${PURPLE_B}${1}${END}"
+}
+
+lightblue() {
+ LIGHT_BLUE="\033[0;36m"
+ printf "${LIGHT_BLUE}${1}${END}"
+}
+
+lightblueb() {
+ LIGHT_BLUE_B="\033[1;36m"
+ printf "${LIGHT_BLUE_B}${1}${END}"
+}
+
+COLOR_LIST='
+black
+blackb
+white
+whiteb
+red
+redb
+green
+greenb
+yellow
+yellowb
+blue
+blueb
+purple
+purpleb
+lightblue
+lightblueb
+blacki
+bluei
+'
+list_colors() {
+ for c in $COLOR_LIST; do
+ printf '%s\n' "$("$c" "$c")"
+ done
+}
+
+
+COLOR_FN=''
+while getopts 'c:l' flag; do
+ case "$flag" in
+ (c)
+ EXISTS=false
+ for c in $COLOR_LIST; do
+ if [ "$OPTARG" = "$c" ]; then
+ EXISTS=true
+ break
+ fi
+ done
+ if [ "$EXISTS" = false ]; then
+ printf 'Invalid color: %s\n' "$OPTARG" >&2
+ exit 2
+ fi
+ COLOR_FN="$OPTARG"
+ ;;
+ (l)
+ list_colors
+ exit
+ ;;
+ (*)
+ usage >&2
+ exit 2
+ ;;
+ esac
+done
+shift $((OPTIND - 1))
+
+TEXT="${1:-}"
+
+eval "$(assert-arg -- "$COLOR_FN" '-c COLOR')"
+eval "$(assert-arg -- "$TEXT" 'TEXT')"
+
+
+"$COLOR_FN" "$TEXT"