下面的POSIX代码的目标是使用 shell tput颜色来处理越多的平台越好。使用这段代码,我现在启动了我的所有脚本,所以是时候回顾一下我忽略了的、不太好的东西了,诸如此类。注意:我使用set -u启动脚本,这就是为什么在未指定的情况下设置所有空脚本的原因。谢谢。
#!/bin/sh
set -u
if tput setaf > /dev/null 2>&1; then
# Linux-like
tput_number_of_colors=$(tput colors 2> /dev/null)
tput_bold=$(tput bold 2> /dev/null)
tput_reset=$(tput sgr0 2> /dev/null)
tput_cmd_set_fg_color='tput setaf'
elif tput AF > /dev/null 2>&1; then
# BSD-like
tput_number_of_colors=$(tput Co 2> /dev/null)
tput_bold=$(tput md 2> /dev/null)
tput_reset=$(tput me 2> /dev/null)
tput_cmd_set_fg_color='tput AF'
else
# Console-like
tput_number_of_colors=2
tput_cmd_set_fg_color=
tput_bold=
tput_reset=
fi
tput_test ()
{
[ -n "$tput_number_of_colors" ] && [ -n "$tput_bold" ] && [ -n "$tput_reset" ] &&
{ [ "$tput_number_of_colors" -ge 8 ] && printf '%s' "$tput_bold" && $tput_cmd_set_fg_color 1; } > /dev/null 2>&1
}
if tput_test; then
color_red=$tput_bold$($tput_cmd_set_fg_color 1)
color_green=$tput_bold$($tput_cmd_set_fg_color 2)
color_yellow=$tput_bold$($tput_cmd_set_fg_color 3)
color_blue=$tput_bold$($tput_cmd_set_fg_color 4)
color_magenta=$tput_bold$($tput_cmd_set_fg_color 5)
color_cyan=$tput_bold$($tput_cmd_set_fg_color 6)
color_white=$tput_bold$($tput_cmd_set_fg_color 7)
else
color_red=; color_green=; color_yellow=; color_blue=; color_magenta=; color_cyan=; color_white=
fi发布于 2020-10-22 05:40:07
由于到目前为止还没有人回复,所以我决定今天早上自己重新考虑并重新编写代码。
terminfo (*nix)诉termcap (*bsd)我从来没有真正想过把这件事记下来有多重要。要在网上搜索信息,你需要知道这些关键词。所以,我添加了它们作为评论。
将
我认为这段代码应该在可用的情况下被构造成适当的函数。
需要简单地链接(&&)像tput_bold=$(tput bold 2> /dev/null) && tput_reset=$(tput sgr0 2> /dev/null)这样的命令,这样才能使想象中的函数返回可靠的退出代码。
的最基本的测试
command -v tput在我的代码中丢失了,现在已经修复了。
因为我在脚本中使用了set -u,所以有必要在失败时将变量设置为空。这一特性已得到加强。
#!/bin/sh
set -u
tput_setup_nix ()
{
# terminfo
tput_cmd_set_fg_color='tput setaf'
tput_number_of_colors=$(tput colors 2> /dev/null) &&
tput_bold=$(tput bold 2> /dev/null) &&
tput_reset=$(tput sgr0 2> /dev/null)
}
tput_setup_bsd ()
{
# termcap
tput_cmd_set_fg_color='tput AF'
tput_number_of_colors=$(tput Co 2> /dev/null) &&
tput_bold=$(tput md 2> /dev/null) &&
tput_reset=$(tput me 2> /dev/null)
}
tput_setup_none ()
{
# no unset variables
tput_cmd_set_fg_color=
tput_number_of_colors=
tput_bold=
tput_reset=
}
if command -v tput > /dev/null 2>&1; then
if tput setaf > /dev/null 2>&1; then
if ! tput_setup_nix; then tput_setup_none; fi
elif tput AF > /dev/null 2>&1; then
if ! tput_setup_bsd; then tput_setup_none; fi
else
tput_setup_none
fi
else
tput_setup_none
fi
tput_capability_test ()
{
[ -n "$tput_cmd_set_fg_color" ] && [ -n "$tput_number_of_colors" ] && [ -n "$tput_bold" ] && [ -n "$tput_reset" ] &&
[ "$tput_number_of_colors" -ge 8 ] && { $tput_cmd_set_fg_color 1 && printf '%s' "$tput_bold$tput_reset"; } > /dev/null 2>&1
}
if tput_capability_test; then
color_red=$tput_bold$($tput_cmd_set_fg_color 1)
color_green=$tput_bold$($tput_cmd_set_fg_color 2)
color_yellow=$tput_bold$($tput_cmd_set_fg_color 3)
color_blue=$tput_bold$($tput_cmd_set_fg_color 4)
color_magenta=$tput_bold$($tput_cmd_set_fg_color 5)
color_cyan=$tput_bold$($tput_cmd_set_fg_color 6)
color_white=$tput_bold$($tput_cmd_set_fg_color 7)
else
color_red=
color_green=
color_yellow=
color_blue=
color_magenta=
color_cyan=
color_white=
fi发布于 2022-10-06 03:03:43
两年后,我发现自己有时间再做一次自我检讨。
# Linux uses terminfo; BSD uses termcap; the null command (:) ignores everything after
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=; }
# This routine prepares `tput` on Linux and BSD and no color console
if tput setaf >/dev/null 2>&1; then tput_init_linux || tput_init_none;
elif tput AF >/dev/null 2>&1; then tput_init_bsd || tput_init_none;
else tput_init_none; fi
no_color () { printf '%s' "$reset_color"; }
colorize ()
{
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' "This color ('$1') is not supported. Quitting!" >&2; exit 1 ;;
esac
}
color_red=$(colorize red)
color_green=$(colorize green)
color_yellow=$(colorize yellow)
color_blue=$(colorize blue)
color_magenta=$(colorize magenta)
color_cyan=$(colorize cyan)
color_white=$(colorize white)
color_none=$(no_color)https://codereview.stackexchange.com/questions/250396
复制相似问题