the*_*fog 7 colors bash terminal tput
我在 bash 脚本中有一个函数:message_offset它用于打印 bash 脚本的状态。
即你会调用它向它传递一条消息和一个状态,就像这样
message_offset "install font library" "[ OK ]"
Run Code Online (Sandbox Code Playgroud)
它会打印到终端,其中printf的%*s格式用于始终将最右边的字符设置[ OK ]为 80 列宽,例如输出将是
install font library [ OK ]
update configuration file on server [ ERR ]
^
|
always
at 80
Run Code Online (Sandbox Code Playgroud)
如果echo使用输出看起来像这样
install font library [ OK ]
update configuration file on server [ ERR ]
Run Code Online (Sandbox Code Playgroud)
代码:
#!/usr/bin/env bash
function message_offset() {
local message="$1"
local status="$2"
# compensate for the message length by reducing the offset
# by the length of the message,
(( offset = 80 - ${#message} ))
# add a $(tput sgr0) to the end to "exit attributes" whether a color was
# set or not
printf "%s%*s%s" "${message}" 80 "$status" "$(tput sgr0)"
}
Run Code Online (Sandbox Code Playgroud)
这一切正常,直到我尝试使用tput将一些颜色序列添加到字符串中,即使“[ERR]”变为红色。
似乎printf "%*s"格式化在设置偏移量时计算 tput 字符序列,所以如果我像这样调用函数
message_offset "update configuration file on server" "$(tput setaf 1)[ ERR ]"
Run Code Online (Sandbox Code Playgroud)
输出将类似于:
install font library [ OK ]
update configuration file on server [ ERR ]
Run Code Online (Sandbox Code Playgroud)
因为printf "%*s"是说嘿这个字符串已经得到了所有的"[ ERR ]"字符,加上"$(tput setaf 1)字符,但显然"$(tput setaf 1)字符没有打印,所以实际上不会影响填充。
有没有办法可以为“状态”消息添加颜色,并使用tput样式颜色序列?
你让这比它应该的复杂得多。您可以处理对齐$message而不关心 ANSI 序列的宽度:
#! /usr/bin/env bash
message() {
[ x"$2" = xOK ] && color=2 || color=1
let offset=$(tput cols)-4-${#2}
printf "%-*s[ %s%s%s ]\n" $offset "$1" "$(tput setaf "$color")" "$2" "$(tput sgr0)"
}
message "install font library" "OK"
message "update configuration file on server" "ERR"
Run Code Online (Sandbox Code Playgroud)
编辑:请注意,大多数printf(1)实现不能很好地处理多字节字符集的长度计算。因此,如果您想在 UTF-8 中打印带有重音字符的消息,您可能需要一种不同的方法。耸耸肩