snipplets:add_color_to_your_scripts

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
snipplets:add_color_to_your_scripts [2010/07/31 08:58]
127.0.0.1 external edit
snipplets:add_color_to_your_scripts [2013/03/23 16:48] (current)
ormaaj color array setup function
Line 2: Line 2:
  
 ---- dataentry snipplet ---- ---- dataentry snipplet ----
-snipplet_tags:​ terminal, color +snipplet_tags : terminal, color 
-LastUpdate_dt: ​2010-07-31 +LastUpdate_dt : 2013-03-23 
-Contributors:​ Frank Lazzarini+Contributors ​ : Frank Lazzarini, Dan Douglas 
 +type          : snipplet
 ---- ----
- 
-^ Keywords: | color,echo | 
-^ Contributor:​ | Frank Lazzarini | 
  
 Make your scripts output more readable using bash colors. Simply add these variables to your script, and you will be able to echo in color. (I haven'​t added all the colors available, just some basics) Make your scripts output more readable using bash colors. Simply add these variables to your script, and you will be able to echo in color. (I haven'​t added all the colors available, just some basics)
Line 35: Line 33:
  
 But also see the notes in [[scripting:​terminalcodes | the article about using terminalcodes]] about generating codes and hardwiring codes. But also see the notes in [[scripting:​terminalcodes | the article about using terminalcodes]] about generating codes and hardwiring codes.
 +
 +This snipplet sets up associative arrays for basic color codes using ''​tput''​ for Bash, ksh93 or zsh. You can pass it variable names to correspond with a collection of codes. There'​s a ''​main''​ function with example usage.
 +<code bash>
 +#​!/​usr/​bin/​env bash
 +
 +${ZSH_VERSION+false} || emulate ksh
 +${BASH_VERSION+shopt -s lastpipe extglob}
 +
 +# colorSet [ --setaf | --setab | --misc ] var
 +# Assigns the selected set of escape mappings to the given associative array names.
 +function colorSet {
 + typeset -a clrs msc
 + typeset x
 + clrs=(black red green orange blue magenta cyan grey darkgrey ltred ltgreen yellow ltblue ltmagenta ltcyan white)
 + msc=(sgr0 bold dim smul blink rev invis)
 +
 + while ! ${2:​+false};​ do
 + ${KSH_VERSION:​+eval typeset -n "​$2"​=\$2}
 + case ${1#--} in
 + setaf|setab)
 + for x in "​${!clrs[@]}";​ do
 + eval "​$2"'​[${clrs[x]}]=$(tput "​${1#​--}"​ "​$x"​)'​
 + done
 + ;;
 + misc)
 + for x in "​${msc[@]}";​ do
 + eval "​$2"'​[$x]=$(tput "​$x"​)'​
 + done
 + ;;
 + *)
 + return 1
 + esac
 + shift 2
 + done
 +}
 +
 +# Example code
 +function main {
 + typeset -A fgColors bgColors miscEscapes
 + if colorSet --setaf fgColors --setab bgColors --misc miscEscapes;​ then
 + if ! ${1:​+${fgColors[$1]:​+false}};​ then
 + printf '​%s%s%s\n'​ "​${fgColors[$1]}"​ "this text is ${1}" "​${miscEscapes[sgr0]}"​ >&3
 + else
 + printf '%s, %s\n' "​${1:​-Empty}"​ 'no such color.'​
 + typeset x y
 + for x in fgColors bgColors miscEscapes;​ do
 + typeset -a keys
 + eval '​keys=("​${!'"​$x"'​[@]}"​)'​
 + printf '%s=( ' "​$x"​
 + for y in "​${keys[@]}";​ do
 + eval '​printf "​[%q]=%q " "​$y"​ "​${'"​$x"'​[$y]}"'​
 + done
 + printf '​)\n'​
 + done
 + return 1
 + fi
 + else
 + echo '​Failed setting color arrays.'​
 + return 1
 + fi 3>&1 >&2
 +}
 +
 +main "​$@"​
 +
 +# vim: set fenc=utf-8 ff=unix ft=sh :
 +</​code>​
  • snipplets/add_color_to_your_scripts.1280566691.txt
  • Last modified: 2011/06/24 05:09
  • (external edit)