带宽度的shell颜色脚本

带宽度的shell颜色脚本

使用printf实现的颜色脚本,带输出自定义宽度格式化

#!/bin/bash
desc(){
printf '# Author: royoy
# Fst_v : v0.0.1
# C_Ver : v0.1.6
# MDat  : 2015/06
# Lmod  : 10/2019
# Mail  : royoy@royoy.com
# Desc  : font [-f|-d|-b|-t|-n|-v|-black|-red|-green|-yellow|-blue|-purple|
#       :       -cyan|-gray|-bblack|-bred|-bgreen|-byellow|-bblue|-bpurple|
#       :       -bcyan|-bgray|-highlight|-underline|-flicker|-reverse|-hide|
#       :       -reset|-slw|-srw|-dlw|-drw|-flw|-frw|-glw|-grw]
# Usage : font -color1 message1 -reset -color2 message2 -reset ...
#  eg   :   font -red  message1 -reset -n -green message2 -reset -n
#  eg   :   font -f 3 \= -n
#  eg   :   font -srw 30 "hello world" -slw 20 "java" -n
#  eg   :   font -red  -slw 30 "hellow world" -r -cyan -slw 20 "java" -r -n'
printf "\n"
}

format(){
for i in $(seq $1)
do
        printf "$2"
        sleep 0.001
done
}

width_set(){
if [ "$1" = "-l" ];then
        printf "%-${2}${4}" "$3"
else
        printf "%${2}${4}" "$3"
fi
}

function font {
while [ $# -ne 0 ]
do
   case $1 in
        -f )   format "$2" "$3";shift 2;;
        -d )   printf "`date`  ";;
        -b )   printf " ";;
        -t )   printf "\t";;
        -n )   printf "\n";;
        -v|-V|-h|-help|--help|-version|--version )   desc;;
        -bla|-black)	printf  "\033[30m";;
        -red )          printf  "\033[31m";;
        -gre|-green)   	printf 	"\033[32m";;
        -yel|-yellow)  	printf 	"\033[33m";;
        -blu|-blue)   	printf 	"\033[34m";;
        -pur|-purple)  	printf 	"\033[35m";;
        -cya|-cyan)   	printf 	"\033[36m";;
        -gra|-gray)   	printf 	"\033[37m";;
        -bbla|-bblack) 	printf  "\033[40m";;
        -bred )         printf  "\033[41m";;
        -bgre|-bgreen) 	printf 	"\033[42m";;
        -byel|-byellow)	printf 	"\033[43m";;
        -bblu|-bblue)  	printf 	"\033[44m";;
        -bpur|-bpurple)	printf 	"\033[45m";;
        -bcya|-bcyan)  	printf 	"\033[46m";;
        -bgra|-bgray)  	printf 	"\033[47m";;
        -hl|-highlight)	printf 	"\033[1m";;
       	-ul|-underline)	printf 	"\033[4m";;
        -fli|-flicker) 	printf 	"\033[5m";;
        -rev|-reverse) 	printf 	"\033[7m";;
        -hide )         printf 	"\033[8m";;
        -r|-reset )   	printf 	"\033[0m";;
        -slw)	width_set -l "$2" "$3" s;shift 2;;
        -srw)	width_set -r "$2" "$3" s;shift 2;;
        -dlw)	width_set -l "$2" "$3" d;shift 2;;
        -drw)	width_set -r "$2" "$3" d;shift 2;;
        -flw)	width_set -l "$2" "$3" f;shift 2;;
        -frw)	width_set -r "$2" "$3" f;shift 2;;
        -glw)	width_set -l "$2" "$3" g;shift 2;;
        -grw)	width_set -r "$2" "$3" g;shift 2;;
           *)
                printf "$1\n";;
    esac
    shift
done
}

font "$@"
##Script End##