全部博文(534)
分类:
2008-10-27 10:25:19
-(dearvoid@:Forum)-(~/void/bin/bash_functions.d)-
[$$=4035 $?=0] ; cat colorize
#!/bin/bash
# vi:set ts=8 sw=4 et sta:
#
# Author: Clark J. Wang
#
# $Date: 2008-10-18 09:53:40 +0800 (Sat, 18 Oct 2008) $
# $HeadURL: svn://svnserver/svn/trunk/bin/bash_functions.d/colorize $
# $Revision: 1470 $
#
#--------------------------------------------------------------------#
function colorize()
{
# the escape char
eval local "cEsc=\$'\\033'"
# foreground colors
local black=30
local red=31
local green=32
local yellow=33
local blue=34
local purple=35
local cyan=36
local white=37
# background colors
local bg_black=40
local bg_red=41
local bg_green=42
local bg_yellow=43
local bg_blue=44
local bg_purple=45
local bg_cyan=46
local bg_white=47
# other attributes
local none=0
local bold=1
local underscore=4
local blink=5
local reverse=7
local hide=8
# valid color names
local aValidClrs
aValidClrs=(black red green yellow blue purple cyan white)
local bUnderscore=0 bBold=0 bHelp=0 bBgClr=0
local bValidClr bListClr=0 bBlink=0 bForceClr=0
local sClr cOpt
local sFgClr sBgClr sNewLineOpt sText
OPTIND=1
OPTERR=0
while getopts ":b:Bc:Fhklnu" cOpt "$@"; do
case "$cOpt" in
b|c)
{
[ "$cOpt" = b ] && bBgClr=1
bValidClr=0
for sClr in ${aValidClrs[@]}; do
if [ "$sClr" = "$OPTARG" ]; then
bValidClr=1
break
fi
done
if ((bValidClr)); then
if ((bBgClr)); then
sBgClr=bg_$OPTARG
else
sFgClr=$OPTARG
fi
else
echo "Invalid color \`$OPTARG'" 1>&2
return 1
fi
};;
B) bBold=1 ;;
F) bForceClr=1 ;;
h) bHelp=1 ;;
k) bBlink=1 ;;
l) bListClr=1 ;;
n) sNewLineOpt='-n' ;;
u) bUnderscore=1 ;;
:|'?')
{
echo "Usage error. Run \`$FUNCNAME -h' for help." 1>&2
return 1
}
esac
done
shift $((OPTIND - 1))
if ((bListClr)); then
echo "${aValidClrs[@]}"
return 0
fi
if (($# == 0 || bHelp)); then
echo "Usage:"
echo " $FUNCNAME [OPTION ...] TEXT ..."
echo ""
echo "Options:"
echo " -b COLOR Background color"
echo " -B Bold"
echo " -c COLOR Foreground color"
echo " -F Use color even STDOUT is not a TTY"
echo " -h Help"
echo " -k Blink (Your terminal may not support this)"
echo " -l List valid colors"
echo " -n Don't output newline"
echo " -u Underscore"
echo ""
echo "Report bugs to $cEsc[1;35mClark J. Wang$cEsc[0m"
return 0
fi
# Text to colorize
sText="$*"
if [[ ! -t 1 && $bForceClr -eq 0 ]]; then
eval "echo $sNewLineOpt \"\$sText\""
return
fi
sClr=0
if ((bUnderscore)); then
sClr="$sClr;$underscore"
fi
if ((bBold)); then
sClr="$sClr;$bold"
fi
if ((bBlink)); then
sClr="$sClr;$blink"
fi
if [ -n "$sFgClr" ]; then
eval "sClr=\"\$sClr;\$$sFgClr\""
fi
if [ -n "$sBgClr" ]; then
eval "sClr=\"\$sClr;\$$sBgClr\""
fi
eval "echo $sNewLineOpt \"\$cEsc[${sClr}m\$sText\$cEsc[0m\""
}
#-- main ------------------------------------------------------------#
{
if [[ $- != *i* ]]; then
colorize "$@"
fi
}
-(dearvoid@:Forum)-(~/void/bin/bash_functions.d)-
[$$=4035 $?=0] ; ./colorize
Usage:
colorize [OPTION ...] TEXT ...
Options:
-b COLOR Background color
-B Bold
-c COLOR Foreground color
-F Use color even STDOUT is not a TTY
-h Help
-k Blink (Your terminal may not support this)
-l List valid colors
-n Don't output newline
-u Underscore
Report bugs to Clark J. Wang
-(dearvoid@:Forum)-(~/void/bin/bash_functions.d)-
[$$=4035 $?=0] ; ./colorize -c red -b blue hello world
hello world
-(dearvoid@:Forum)-(~/void/bin/bash_functions.d)-
[$$=4035 $?=0] ; bye