您可能已经知道有一些特殊的系统序列可以控制到终端的输出。例如,这会将文本变为红色 '\e[31m',这将在特定列\行 '\e[${LINE};${COLUMN}H' 中打印文本。所以我们将使用它。首先,我将使用“名称值”对创建这个“数据”数组来模拟您的情况。
data=(
"Accounts 5"
"Banned 10"
"Online 40"
"Guilds 4"
"Members 1"
"Mail 71"
"Pets 43"
"Tickets 0"
"Corpses 101"
"Characters 10"
"PvP 0"
"Gifts 4"
)
我在处理文本输出时使用这个表,所以让我们用它来
#--------------------------------------------------------------------+
#Color picker, usage: printf ${BLD}${CUR}${RED}${BBLU}"Hello!)"${DEF}|
#-------------------------+--------------------------------+---------+
# Text color | Background color | |
#-----------+-------------+--------------+-----------------+ |
# Base color|Lighter shade| Base color | Lighter shade | |
#-----------+-------------+--------------+-----------------+ |
BLK='\e[30m'; blk='\e[90m'; BBLK='\e[40m'; bblk='\e[100m' #| Black |
RED='\e[31m'; red='\e[91m'; BRED='\e[41m'; bred='\e[101m' #| Red |
GRN='\e[32m'; grn='\e[92m'; BGRN='\e[42m'; bgrn='\e[102m' #| Green |
YLW='\e[33m'; ylw='\e[93m'; BYLW='\e[43m'; bylw='\e[103m' #| Yellow |
BLU='\e[34m'; blu='\e[94m'; BBLU='\e[44m'; bblu='\e[104m' #| Blue |
MGN='\e[35m'; mgn='\e[95m'; BMGN='\e[45m'; bmgn='\e[105m' #| Magenta |
CYN='\e[36m'; cyn='\e[96m'; BCYN='\e[46m'; bcyn='\e[106m' #| Cyan |
WHT='\e[37m'; wht='\e[97m'; BWHT='\e[47m'; bwht='\e[107m' #| White |
#----------------------------------------------------------+---------+
# Effects |
#--------------------------------------------------------------------+
DEF='\e[0m' #Default color and effects |
BLD='\e[1m' #Bold\brighter |
DIM='\e[2m' #Dim\darker |
CUR='\e[3m' #Italic font |
UND='\e[4m' #Underline |
INV='\e[7m' #Inverted |
COF='\e[?25l' #Cursor Off |
CON='\e[?25h' #Cursor On |
#--------------------------------------------------------------------+
# Text positioning, usage: XY 10 10 "Hello World!" |
XY () { printf "\e[${2};${1}H${3}"; } # |
#--------------------------------------------------------------------+
# Print line, usage: line - 10 | line -= 20 | line "Hello World!" 20 |
line () { printf -v LINE "%$2s"; printf -- "${LINE// /$1}"; } # |
# Create sequence like {0..X} |
cnt () { printf -v _N %$1s; _N=(${_N// / 1}); printf "${!_N[*]}"; } #|
#--------------------------------------------------------------------+
将所有基本颜色设置为 vars 以轻松插入文本和一些有用的功能,如 XY 我将使用它在 serrtain 位置打印文本。
让我们设置一些变量
space_betwen=7 # space betwen columns
X=$space_betwen # starting X(column) position
Y=10 # starting Y(line) position
dot_string='...............: ' # dot string to simulate your output
dot_length=${#dot_string} # this will calculate the length of the dot string
好的,我们准备好了,但首先让我们清除终端屏幕上的所有文本
clear
现在我们可以遍历数据并打印 3 列 4 行的文本
for item in "${data[@]}"; {
((counter++)) # lets count items to know when start next column
read name value <<< $item # get naame and value from current item
XY $X $Y "$dot_string$RED$value$DEF" # print dot string and red value
XY $X $Y "$YLW$name$DEF" # name will be printed ower dots in yelow color
((Y++)) # go to next line by increasing Y value
# chek if we print 4 lines than set Y to start poosition and inc X to space_betwen+dot_length
((counter%4)) || { Y=10; ((X+=space_betwen+dot_length)); }
}
最终的脚本会是这样的
#!/bin/bash
data=(
"Accounts 5"
"Banned 10"
"Online 40"
"Guilds 4"
"Members 1"
"Mail 71"
"Pets 43"
"Tickets 0"
"Corpses 101"
"Characters 10"
"PvP 0"
"Gifts 4"
)
. ~/SCR/color # include color table
space_betwen=7 # space betwen columns
X=$space_betwen # starting X(column) position
Y=10 # starting Y(line) position
dot_string='...............: ' # dot string to simulate your output
dot_length=${#dot_string} # this will calculate the length of the dot string
clear
for item in "${data[@]}"; {
((counter++)) # lets count items to know when start next column
read name value <<< $item # get naame and value from current item
XY $X $Y "$dot_string$RED$value$DEF" # print dot string and red value
XY $X $Y "$YLW$name$DEF" # name will be printed ower dots in yelow color
((Y++)) # go to next line by increasing Y value
# chek if we print 4 lines than set Y to start poosition and inc X to space_betwen+dot_length
((counter%4)) || { Y=10; ((X+=space_betwen+dot_length)); }
}
XY 1 20 "$DEF" # one more time to move cursor down in the end
输出将是这样的
