From 5c386aa52d52db1b96c6e8d2e7fd0745e0c924cb Mon Sep 17 00:00:00 2001 From: Loic Nageleisen Date: Thu, 4 Dec 2014 16:08:08 +0100 Subject: [PATCH] bash: set_color helper (bash 3+4) --- bash/ansi | 25 +++++++++++ bash/ansi3 | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++ bash/ansi4 | 92 +++++++++++++++++++++++++++++++++++++++ bash/colors | 115 ------------------------------------------------ bash/rc | 2 +- 5 files changed, 241 insertions(+), 116 deletions(-) create mode 100644 bash/ansi create mode 100644 bash/ansi3 create mode 100644 bash/ansi4 delete mode 100644 bash/colors diff --git a/bash/ansi b/bash/ansi new file mode 100644 index 0000000..8ed3e2c --- /dev/null +++ b/bash/ansi @@ -0,0 +1,25 @@ +if [[ $BASH_VERSINFO -gt 3 ]]; then + source "$DOTFILES_BASH_DIR"/ansi4 +else + source "$DOTFILES_BASH_DIR"/ansi3 +fi + +setup_ansi_sgr_index + +# set_color [-p] color +# -p: wraps output for bash prompt size match +set_color() { + if [[ $1 == "-p" ]]; then + local prompt=1 + shift + fi + local code=$1 + + [[ $prompt -eq 1 ]] && echo -n "\[" + echo -n "\033[" + ansi_sgr "$code" + echo -n "m" + [[ $prompt -eq 1 ]] && echo -n "\]" +} + +# vim: ft=sh diff --git a/bash/ansi3 b/bash/ansi3 new file mode 100644 index 0000000..e230b1e --- /dev/null +++ b/bash/ansi3 @@ -0,0 +1,123 @@ +# hack a dictionary with case and return codes +ansi_sgr_index() { + case "$1" in + none) return 00;; + bold) return 01;; + faint) return 02;; + standout) return 03;; + underline) return 04;; + blink) return 05;; + fast-blink) return 06;; + reverse) return 07;; + conceal) return 08;; + strikethrough) return 09;; + + font-default) return 10;; + font-first) return 11;; + font-second) return 12;; + font-third) return 13;; + font-fourth) return 14;; + font-fifth) return 15;; + font-sixth) return 16;; + font-seventh) return 17;; + font-eighth) return 18;; + font-ninth) return 19;; + + gothic) return 20;; + double-underline) return 21;; + normal) return 22;; + no-standout) return 23;; + no-underline) return 24;; + no-blink) return 25;; + proportional) return 26;; + no-reverse) return 27;; + no-conceal) return 28;; + no-strikethrough) return 29;; + + black) return 30;; + red) return 31;; + green) return 32;; + yellow) return 33;; + blue) return 34;; + magenta) return 35;; + cyan) return 36;; + white) return 37;; + iso-8316-6) return 38;; + default) return 39;; + + fg-black) return 30;; + fg-red) return 31;; + fg-green) return 32;; + fg-yellow) return 33;; + fg-blue) return 34;; + fg-magenta) return 35;; + fg-cyan) return 36;; + fg-white) return 37;; + fg-iso-8316-6) return 38;; + fg-default) return 39;; + + bg-black) return 40;; + bg-red) return 41;; + bg-green) return 42;; + bg-yellow) return 43;; + bg-blue) return 44;; + bg-magenta) return 45;; + bg-cyan) return 46;; + bg-white) return 47;; + bg-iso-8316-6) return 48;; + bg-default) return 49;; + + no-proportional) return 50;; + border-rectangle) return 51;; + border-circle) return 52;; + overline) return 53;; + no-border) return 54;; + no-overline) return 55;; + #through 59 reserved) return 56;; + + underline-or-right) return 60;; + double-underline-or-right) return 61;; + overline-or-left) return 62;; + double-overline-or-left) return 63;; + stress) return 64;; + no-ideogram-marking) return 65;; + esac + + return 255 +} + +# create caching key-value vars dynamically +setup_ansi_sgr_index() { + local keys=" + none bold faint standout underline blink fast-blink reverse conceal strikethrough + font-default font-first font-second font-third font-fourth font-fifth font-sixth font-seventh font-eighth font-ninth + gothic double-underline normal no-standout no-underline no-blink proportional no-reverse no-conceal no-strikethrough + black red green yellow blue magenta cyan white iso-8316-6 default + fg-black fg-red fg-green fg-yellow fg-blue fg-magenta fg-cyan fg-white fg-iso-8316-6 fg-default + bg-black bg-red bg-green bg-yellow bg-blue bg-magenta bg-cyan bg-white bg-iso-8316-6 bg-default + no-proportional border-rectangle border-circle overline no-border no-overline + underline-or-right double-underline-or-right overline-or-left double-overline-or-left stress no-ideogram-marking" + + for key in $keys; do + cache_ansi_sgr "$key" + done +} + +# resolve to cache var +cache_ansi_sgr() { + local key=$1; key=${key//-/_} + + eval "ansi_sgr_index ${key}" + local code=$? + [[ $code -ne 255 ]] && eval "ANSI_SGR_INDEX__${key}__=\$code" +} + +# memoized fetch +ansi_sgr() { + local key=$1; key=${key//-/_} + + eval "[[ -z \$ANSI_SGR_INDEX__${key}__ ]]" && cache_ansi_sgr "$key" + eval "echo -n \$ANSI_SGR_INDEX__${key}__" +} + +# vim: ft=sh diff --git a/bash/ansi4 b/bash/ansi4 new file mode 100644 index 0000000..75de5d9 --- /dev/null +++ b/bash/ansi4 @@ -0,0 +1,92 @@ +declare -A ANSI_SGR_INDEX + +# build index +setup_ansi_sgr_index() { + ANSI_SGR_INDEX["none"]=00 + ANSI_SGR_INDEX["bold"]=01 + ANSI_SGR_INDEX["faint"]=02 + ANSI_SGR_INDEX["standout"]=03 + ANSI_SGR_INDEX["underline"]=04 + ANSI_SGR_INDEX["blink"]=05 + ANSI_SGR_INDEX["fast-blink"]=06 + ANSI_SGR_INDEX["reverse"]=07 + ANSI_SGR_INDEX["conceal"]=08 + ANSI_SGR_INDEX["strikethrough"]=09 + + ANSI_SGR_INDEX["font-default"]=10 + ANSI_SGR_INDEX["font-first"]=11 + ANSI_SGR_INDEX["font-second"]=12 + ANSI_SGR_INDEX["font-third"]=13 + ANSI_SGR_INDEX["font-fourth"]=14 + ANSI_SGR_INDEX["font-fifth"]=15 + ANSI_SGR_INDEX["font-sixth"]=16 + ANSI_SGR_INDEX["font-seventh"]=17 + ANSI_SGR_INDEX["font-eighth"]=18 + ANSI_SGR_INDEX["font-ninth"]=19 + + ANSI_SGR_INDEX["gothic"]=20 + ANSI_SGR_INDEX["double-underline"]=21 + ANSI_SGR_INDEX["normal"]=22 + ANSI_SGR_INDEX["no-standout"]=23 + ANSI_SGR_INDEX["no-underline"]=24 + ANSI_SGR_INDEX["no-blink"]=25 + ANSI_SGR_INDEX["proportional"]=26 + ANSI_SGR_INDEX["no-reverse"]=27 + ANSI_SGR_INDEX["no-conceal"]=28 + ANSI_SGR_INDEX["no-strikethrough"]=29 + + ANSI_SGR_INDEX["black"]=30 + ANSI_SGR_INDEX["red"]=31 + ANSI_SGR_INDEX["green"]=32 + ANSI_SGR_INDEX["yellow"]=33 + ANSI_SGR_INDEX["blue"]=34 + ANSI_SGR_INDEX["magenta"]=35 + ANSI_SGR_INDEX["cyan"]=36 + ANSI_SGR_INDEX["white"]=37 + ANSI_SGR_INDEX["iso-8316-6"]=38 + ANSI_SGR_INDEX["default"]=39 + + ANSI_SGR_INDEX["fg-black"]=30 + ANSI_SGR_INDEX["fg-red"]=31 + ANSI_SGR_INDEX["fg-green"]=32 + ANSI_SGR_INDEX["fg-yellow"]=33 + ANSI_SGR_INDEX["fg-blue"]=34 + ANSI_SGR_INDEX["fg-magenta"]=35 + ANSI_SGR_INDEX["fg-cyan"]=36 + ANSI_SGR_INDEX["fg-white"]=37 + ANSI_SGR_INDEX["fg-iso-8316-6"]=38 + ANSI_SGR_INDEX["fg-default"]=39 + + ANSI_SGR_INDEX["bg-black"]=40 + ANSI_SGR_INDEX["bg-red"]=41 + ANSI_SGR_INDEX["bg-green"]=42 + ANSI_SGR_INDEX["bg-yellow"]=43 + ANSI_SGR_INDEX["bg-blue"]=44 + ANSI_SGR_INDEX["bg-magenta"]=45 + ANSI_SGR_INDEX["bg-cyan"]=46 + ANSI_SGR_INDEX["bg-white"]=47 + ANSI_SGR_INDEX["bg-iso-8316-6"]=48 + ANSI_SGR_INDEX["bg-default"]=49 + + ANSI_SGR_INDEX["no-proportional"]=50 + ANSI_SGR_INDEX["border-rectangle"]=51 + ANSI_SGR_INDEX["border-circle"]=52 + ANSI_SGR_INDEX["overline"]=53 + ANSI_SGR_INDEX["no-border"]=54 + ANSI_SGR_INDEX["no-overline"]=55 + #ANSI_SGR_INDEX["through 59 reserved"]=56 + + ANSI_SGR_INDEX["underline-or-right"]=60 + ANSI_SGR_INDEX["double-underline-or-right"]=61 + ANSI_SGR_INDEX["overline-or-left"]=62 + ANSI_SGR_INDEX["double-overline-or-left"]=63 + ANSI_SGR_INDEX["stress"]=64 + ANSI_SGR_INDEX["no-ideogram-marking"]=65 +} + +# fetch +ansi_sgr() { + echo -n ${ANSI_SGR_INDEX[$1]} +} + +# vim: ft=sh diff --git a/bash/colors b/bash/colors deleted file mode 100644 index 51328ba..0000000 --- a/bash/colors +++ /dev/null @@ -1,115 +0,0 @@ -ansi_sgr_index() { - case "$1" in - none) return 00;; - bold) return 01;; - faint) return 02;; - standout) return 03;; - underline) return 04;; - blink) return 05;; - #fast-blink) return 06;; - reverse) return 07;; - conceal) return 08;; - #strikethrough) return 09;; - - #font-default) return 10;; - #font-first) return 11;; - #font-second) return 12;; - #font-third) return 13;; - #font-fourth) return 14;; - #font-fifth) return 15;; - #font-sixth) return 16;; - #font-seventh) return 17;; - #font-eighth) return 18;; - #font-ninth) return 19;; - - #gothic) return 20;; - #double-underline) return 21;; - normal) return 22;; - no-standout) return 23;; - no-underline) return 24;; - no-blink) return 25;; - #proportional) return 26;; - no-reverse) return 27;; - no-conceal) return 28;; - #no-strikethrough) return 29;; - - black) return 30;; - red) return 31;; - green) return 32;; - yellow) return 33;; - blue) return 34;; - magenta) return 35;; - cyan) return 36;; - white) return 37;; - #iso-8316-6) return 38;; - default) return 39;; - - fg-black) return 30;; - fg-red) return 31;; - fg-green) return 32;; - fg-yellow) return 33;; - fg-blue) return 34;; - fg-magenta) return 35;; - fg-cyan) return 36;; - fg-white) return 37;; - #fg-iso-8316-6) return 38;; - fg-default) return 39;; - - bg-black) return 40;; - bg-red) return 41;; - bg-green) return 42;; - bg-yellow) return 43;; - bg-blue) return 44;; - bg-magenta) return 45;; - bg-cyan) return 46;; - bg-white) return 47;; - #bg-iso-8316-6) return 48;; - bg-default) return 49;; - - #no-proportional) return 50;; - #border-rectangle) return 51;; - #border-circle) return 52;; - #overline) return 53;; - #no-border) return 54;; - #no-overline) return 55;; - #through 59 reserved) return 56;; - - #underline-or-right) return 60;; - #double-underline-or-right) return 61;; - #overline-or-left) return 62;; - #double-overline-or-left) return 63;; - #stress) return 64;; - #no-ideogram-marking) return 65;; - - *) return 255;; - esac -} - -ansi_csi_l="\033[" -ansi_csi_sgr_r="m" - -color=(); -fg=(); -fg_bold=(); -fg_no_bold=(); -bg=(); -bg_bold=(); -bg_no_bold=(); - -for v in {30..39}; do - fg[$v]="${ansi_csi_l}${v}${ansi_csi_sgr_r}" - ansi_sgr_index 'bold' - fg_bold[$v]="${ansi_csi_l}${?};${v}${ansi_csi_sgr_r}" - ansi_sgr_index 'normal' - fg_no_bold[$v]="${ansi_csi_l}${?};${v}${ansi_csi_sgr_r}" -done - -for v in {40..49}; do - bg[$v]="${ansi_csi_l}${v}${ansi_csi_sgr_r}" - ansi_sgr_index 'bold' - bg_bold[$v]="${ansi_csi_l}${?};${v}${ansi_csi_sgr_r}" - ansi_sgr_index 'normal' - bg_no_bold[$v]="${ansi_csi_l}${?};${v}${ansi_csi_sgr_r}" -done - -# vim: ft=sh diff --git a/bash/rc b/bash/rc index 8d79e4e..85e02a8 100644 --- a/bash/rc +++ b/bash/rc @@ -4,7 +4,7 @@ source $DOTFILES_SHELL_DIR/aliases source $DOTFILES_BASH_DIR/ext -source $DOTFILES_BASH_DIR/colors +source $DOTFILES_BASH_DIR/ansi source $DOTFILES_BASH_DIR/prompt source $DOTFILES_BASH_DIR/history source $DOTFILES_BASH_DIR/term_title