无论输出bash printf保持文本在适当位置 [英] Keep text in place regardless output bash printf
问题描述
当数字变大时就会发生这种情况...只是从这个烂摊子开始.文本被更大的输出推动...我如何包含这个烂摊子?
This happens when numbers gets bigger...Just started with this mess. Text being pushed around by bigger outputs...how do I contain this mess?
Accounts......: 5 Mail..........: 7
Banned........: 0 Pets..........: 1
Online........: 0 Tickets.......: 0
Guilds........: 1 Corpses.......: 0 PvP.......: 0
Members.......: 1 Characters....: 10 Gifts.....: 4 <-----HOWTO Reserve/Preserve spaces ?? ??
应如下所示:
Accounts......: 5 Mail..........: 7
Banned........: 0 Pets..........: 1
Online........: 0 Tickets.......: 0
Guilds........: 1 Corpses.......: 0 PvP.......: 0
Members.......: 1 Characters....: 10 Gifts.....: 4
现在这个烂摊子看起来像这样:
Now this mess looks like this:
ch_count=$(mysql --defaults-extra-file="$sql_mycnf" -N --execute="SELECT count(*) FROM $db_characters.characters;"); &> /dev/null
cm_char="\033[0mCharacters\e[0;32m....:\033[0m $ch_count\e[31m"
line=" "
$cm_acco$line$cm_mail
$cm_bann$line$cm_pets
$cm_onli$line$cm_tick
$cm_guil$line$cm_corp$line$cm_pvps
$cm_memb$line$cm_char$line$cm_gifts
在另一台服务器上有相同的输出,但是因为它们较小,所以看起来不错:
On another server there is same outputs but because they are smaller it looks fine:
Accounts......: 4 Mail..........: 0
Banned........: 0 Pets..........: 0
Online........: 0 Tickets.......: 0
Guilds........: 0 Corpses.......: 0 PvP.......: 0
Members.......: 0 Characters....: 2 Gifts.....: 0
编辑此行使其起作用?这是一个正确的起点吗?
Edit this line to make it work? Is this correct place to begin?
cm_char="\033[0mCharacters\e[0;32m....:\033[0m $ch_count\e[31m"
杀了我.
推荐答案
使2个函数格式化字段并使用它们:
Make 2 functions that will format the fields and use them:
dot_field() {
# todo Change implementation when field can be 2 words with a space in between
printf "%-14.14s:" "$1" | tr ' ' '.'
}
space_number() {
printf "%-7.7s" "$1"
}
printline() {
# Todo: add logic when only 4 parameters are given
echo " $(dot_field $1) $(space_number $2)$(dot_field $3) $(space_number $4)$(dot_field $5) $(space_number $6)"
}
printline "Guilds" 1 "Corpses" 0 "PvP" 0
printline "Members" 1 "Characters" 10 "Gifts" 4
printline "LongFieldName" 1 "High" 999999 "X" 2
添加颜色.
您不想让代码中充满各种颜色的转义代码.这取决于整个上下文,您希望如何构造颜色代码,下面给出一个有关问题的有限上下文的示例.它应该让您知道如何自己制作类似的东西.
You don't want to have your code full of escape codes for the colors. It depends on the full context how you would like to structure your color codes, I give an example for the limited context of the question. It should give you an idea how you can make something like this for yourself.
init_colors() {
reset=$(tput sgr0)
bold=$(tput bold)
black=$(tput setaf 0)
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
magenta=$(tput setaf 5)
cyan=$(tput setaf 6)
white=$(tput setaf 7)
user_color=$bold
}
# colorstring reads from stdin and uses parameter 1 as an escape sequence
# with more parameters the first is used as a color, the other as the string to be modified
# It will set colors until the last space sequence
colorstring() {
case $# in
0) # invalid
echo "colorstring called without parameters"
;;
1)
sed -r "s/^.*[^ ]/$1&${reset}/"
;;
*)
color="$1"
shift
sed -r "s/^.*[^ ]/${color}&${reset}/" <<< "$@"
;;
esac
}
dot_field() {
# todo Change implementation when field can be 2 words with a space in between
printf "%-14.14s" "$1" | colorstring ${cyan} | tr ' ' '.'
# The : must be printed in a second statement when you don't want cyan dots.
printf ':'
}
space_number() {
printf "%-7.7s" "$1" | colorstring ${red}
}
printline() {
echo " $(dot_field $1) $(space_number $2)$(dot_field $3) $(space_number $4)$(dot_field $5) $(space_number $6)"
}
# init the color variables
init_colors
# Next echo not needed, just testing the new colorstring function
echo "$(colorstring ${blue} blue string) $(colorstring ${red} red car) $(colorstring ${white} white snow) $(colorstring ${yellow} yellow marker) $(colorstring ${cyan} cyan) "
printline "Guilds" 1 "Corpses" 0 "PvP" 0
printline "Members" 1 "Characters" 10 "Gifts" 4
printline "LongFieldName" 1 "High" 999999 "X" 2
这篇关于无论输出bash printf保持文本在适当位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!