aboutsummaryrefslogtreecommitdiff
path: root/bin/color
diff options
context:
space:
mode:
Diffstat (limited to 'bin/color')
-rwxr-xr-xbin/color204
1 files changed, 204 insertions, 0 deletions
diff --git a/bin/color b/bin/color
new file mode 100755
index 0000000..0597278
--- /dev/null
+++ b/bin/color
@@ -0,0 +1,204 @@
+#!/bin/sh
+set -eu
+
+usage() {
+ cat <<-'EOF'
+ Usage:
+ color -c COLOR TEXT
+ color -h
+ EOF
+}
+
+help() {
+ cat <<-'EOF'
+
+
+ Options:
+ -c COLOR
+ -h, --help show this message
+
+
+ The available colors are:
+ EOF
+ list_colors | sed 's/^/ /'
+}
+
+
+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
+}
+
+
+for flag in "$@"; do
+ case "$flag" in
+ --)
+ break
+ ;;
+ --help)
+ usage
+ help
+ exit
+ ;;
+ *)
+ ;;
+ esac
+done
+
+while getopts 'c:h' 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"
+ ;;
+ h)
+ usage
+ help
+ exit
+ ;;
+ *)
+ usage >&2
+ exit 2
+ ;;
+ esac
+done
+shift $((OPTIND - 1))
+
+assert_arg() {
+ if [ -z "$1" ]; then
+ printf 'Missing %s\n\n' "$2" >&2
+ usage >&2
+ exit 2
+ fi
+}
+
+TEXT="${1:-}"
+assert_arg "$COLOR_FN" '-c COLOR'
+assert_arg "$TEXT" 'TEXT'
+
+
+"$COLOR_FN" "$TEXT"