Created
May 30, 2011 16:40
-
-
Save eatnumber1/999135 to your computer and use it in GitHub Desktop.
Koluring: Coloring text for your terminal.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
#setopt xtrace | |
setopt err_exit | |
[[ "$TERM" == "tty" ]] && unset TERM | |
if (( ${+TERM} )); then | |
typeset term_defined | |
else | |
export TERM="rxvt" | |
fi | |
emulate -L zsh | |
zmodload -F zsh/terminfo +b:echoti | |
typeset -A echoti_cache | |
function echoti_retval { | |
if [[ $# -gt 1 ]]; then | |
! (( ${+echoti_cache[$*]} )) && echoti_cache[$*]="$(builtin echoti "$@")" | |
retval="$echoti_cache[$*]" | |
else | |
retval="$terminfo[$1]" | |
fi | |
} | |
typeset retval | |
function echoti { | |
echoti_retval "$@" | |
printf "%s" "$retval" | |
} | |
typeset -i ncolors | |
echoti_retval colors | |
ncolors="$retval" | |
let ncolors-=1 | |
zmodload -F zsh/zutil +b:zparseopts | |
zparseopts -K -D -E -- -bold=bold b=bold f:=frequency -bold-frequency:=frequency h=help -help=help r=random -random=random n=nonewline -nonewline=nonewline | |
readonly usage="Usage: $0 [-hrbn] [-f frequency] [--random] [--bold] [--bold-frequency=frequency] [--help] [--nonewline] text" | |
if (( ${+help} )); then | |
echo "$usage" | |
exit | |
fi | |
if [[ $# -eq 0 ]]; then | |
echo "$usage" >&2 | |
exit 1 | |
fi | |
if ! (( ${+frequency} )); then | |
typeset -a frequency | |
frequency=( -f 2 ) | |
fi | |
if (( ${+bold} )); then | |
if (( ${+random} )); then | |
function bold { | |
typeset -i do_bold | |
let do_bold="$RANDOM % ${frequency[2]}" | |
if [[ $do_bold -eq 0 ]]; then | |
if ! (( ${+did_bold} )); then | |
typeset -g did_bold | |
echoti bold | |
fi | |
else | |
unbold | |
unset did_bold | |
fi | |
} | |
function print_attributes { | |
bold | |
print_next_color | |
} | |
else | |
typeset did_bold | |
function bold { | |
if (( ${+did_bold} )); then | |
unbold | |
unset did_bold | |
return 1 | |
else | |
echoti bold | |
typeset -g did_bold | |
return 0 | |
fi | |
} | |
function print_attributes { | |
bold || print_next_color | |
} | |
fi | |
function unbold { | |
(( ${+did_bold} )) && echoti sgr0 | |
} | |
else | |
function print_attributes { | |
print_next_color | |
} | |
function unbold {} | |
fi | |
if (( ${+random} )); then | |
function next_color_get_num { | |
num="$RANDOM" | |
} | |
else | |
function next_color_get_num { | |
num="$color" | |
} | |
fi | |
function TRAPEXIT { | |
echoti sgr0 | |
} | |
function next_color { | |
typeset -i num | |
next_color_get_num | |
let color="( $num % $ncolors ) + 1" | |
} | |
function print_next_color { | |
next_color | |
[[ $last_color -ne $next_color ]] && echoti setaf $color | |
} | |
typeset -i i color=0 last_color=-1 | |
typeset text="$*" | |
for (( i=1; i<=${#text}; i++ )); do | |
print_attributes | |
printf "%c" "$text[$i]" | |
done | |
unbold | |
(( ${+nonewline} )) || printf "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment