最近,我发现一件奇怪的事情。如果我运行(左)和(右)sudo journalctl -f -u ModemManager
中的命令,黄色的警告信息并没有出现。rxvt-unicode
xterm
rxvt-unicode
两个终端都有TERM=xterm-256color
,而且我只在黄色中注意到了这个问题,因为两个终端上都出现了红色。
我尝试了不同的转义序列,但无法检测到问题。
我有一个多行字符串,用于在 bash 脚本中打印。
docstring="
Headings
-H, -H CNT, -H=CNT, -HCNT, --heading CNT, --heading=CNT
Warnings
-W, -W CNT, -W=CNT, -WCNT, --warning CNT, --warning=CNT
Errors
-E, -E CNT, -E=CNT, -ECNT, --error CNT, --error=CNT"
echo "$docstring"
我想创建一个函数,它接受这个字符串并以彩色打印以-
or开头的行{-
。
因此,以下将被着色
-H, -H CNT, -H=CNT, -HCNT, --heading CNT, --heading=CNT
-W, -W CNT, -W=CNT, -WCNT, --warning CNT, --warning=CNT
-E, -E CNT, -E=CNT, -ECNT, --error CNT, --error=CNT
我的控制台背景很暗,所以有相当多的 journalctl 输出不可读。
我看到很多关于如何添加颜色的信息!但是我如何完全禁用它呢?
我想在控制台的标准输出中看到颜色输出,但我想在tee
命令输出的捕获副本中将其删除。就我而言,
command_that_writes_color_to_stdout | 三通文件
我希望文件中没有 ANSI 颜色序列等,因为它使grep
以后的日志文件变得有趣:
echo -e "color \033[1;31mRED\033[0m output" | tee test.log
在这种情况下,颜色被写入控制台并写入文件“test.log”。
color ^[[1;31mRED^[[0m output
有没有办法只为tee
文件的输出去除 ANSI 序列?
试图让 tee 看到我的终端不知道颜色(环境变量、子外壳),但很tee
高兴只写它给出的内容。我想要控制台输出的颜色(供人类消费,这很棒),但不希望输出的日志文件副本中的颜色。
echo -e "color \033[1;31mRED\033[0m output" | TERM=dumb tee test.log ; od -c test.log
我发现很多人想要在他们的“piped to tee”输出中使用颜色代码(通常是当第一个程序知道可以显示颜色的东西时),但我没有找到相反的问题/答案。
粗体字体 (ANSI: CSI 1 m
) 的效果似乎取决于终端仿真器。例如,在不同的终端模拟器上运行以下脚本
#!/bin/sh
echo "TERM = $TERM"
for mode in 0 2 1 '1;2'; do
printf '\033[%s;38;5;%dm\033[48;5;%dm%s\033[0m\n' "$mode" 0 15 "testing ($mode)"
done
给出以下输出
在测试的终端仿真器中,只能xterm
正确呈现粗体文本(模式=1)。其他终端仿真器似乎为粗体字体选择了更亮的颜色(通常也将其与粗体字体结合使用)。奇怪的是,st
当给定参数时,会产生正确着色的粗体文本1;2
,对应于bold;faint
.
认为也许这些终端仿真器期望粗体字体的不同控制序列,我检查了terminfo
,但发现一致
$ for term in xterm-256color st-256color rxvt-unicode-256color tmux-256color; do printf "%-24s" "$term"; TERM=$term tput bold | cat -v; echo; done
xterm-256color ^[[1m
st-256color ^[[1m
rxvt-unicode-256color ^[[1m
tmux-256color ^[[1m
这就引出了一个问题,哪些终端仿真器参数控制粗体字体的效果?如何防止转向更亮的颜色?这可以通过Xresources
或terminfo
定制来解决吗?(顺便说一句,是否有对应的参数vim
?它表现出类似的行为,不一定对应于运行它的终端仿真器的行为。)
假设我的一个便携式 shell 脚本中有以下颜色支持:
#!/bin/sh
set -o nounset
tput_init_linux () { set_fg_color='tput setaf'; reset_color=$(tput sgr0 2>/dev/null); }
tput_init_bsd () { set_fg_color='tput AF'; reset_color=$(tput me 2>/dev/null); }
tput_init_none () { set_fg_color=':'; reset_color=; }
if tput setaf 1 >/dev/null 2>&1; then tput_init_linux || tput_init_none;
elif tput AF 1 >/dev/null 2>&1; then tput_init_bsd || tput_init_none;
else tput_init_none; fi
no_color () { printf '%s' "$reset_color"; }
colorize ()
{
#tput bold
case "$1" in
(red) $set_fg_color 1 ;;
(green) $set_fg_color 2 ;;
(yellow) $set_fg_color 3 ;;
(blue) $set_fg_color 4 ;;
(magenta) $set_fg_color 5 ;;
(cyan) $set_fg_color 6 ;;
(white) $set_fg_color 7 ;;
(*) printf '%s\n' "[ERROR] This color ('$1') is not supported by the colorize() function. Quiting!" >&2; exit 1 ;;
esac
}
print_ok () { colorize green; printf '%s' '[OK] '; no_color; }
print_notice () { colorize cyan; printf '%s' '[NOTICE] '; no_color; }
print_debug () { colorize yellow; printf '%s' '[DEBUG] ' >&2; no_color; }
print_error () { colorize red; printf '%s' '[ERROR] ' >&2; no_color; }
一个相当愚蠢的使用示例如下:
grub_config_file=/boot/grub/grub.cfg
readonly grub_config_file
if [ ! -f "$grub_config_file" ]; then
print_error; printf '%s\n' "GRUB config file not found at $grub_config_file. Aborting!" >&2
exit 1
else
print_ok; printf '%s\n' "GRUB config file was found at $grub_config_file. Searching for Windows..."
fi
现在,我的问题是关于粗体文本。
具体来说,我不确定 terminfo/termcap tput bold
/tput md
是否可移植,如果不是,粗体文本的限制是什么?
感谢您的时间。