首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >术语/术语“`tput bold‘/`tput’:粗体文本的可移植性

术语/术语“`tput bold‘/`tput’:粗体文本的可移植性
EN

Unix & Linux用户
提问于 2022-02-12 12:09:16
回答 1查看 221关注 0票数 1
代码语言:javascript
复制
#!/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; }

下面是一个相当愚蠢的使用示例:

代码语言:javascript
复制
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

现在,my问题是关于粗体文本的。

具体来说,我不确定tput bold/tput md是否是可移植的,如果不是,粗体文本的限制是什么?

谢谢您抽时间见我。

EN

回答 1

Unix & Linux用户

回答已采纳

发布于 2022-02-12 12:48:33

一些终端支持ECMA-48控制SGR 22 (不粗体/模糊,不影响颜色)。然而,

  • 在terminfo或termcap中没有预定义的粗体关闭功能(请参阅手册页)。

为了便于移植,您必须考虑到这一点(如果您将粗体关闭而不打算影响颜色,则将颜色返回<#>上<#>)。

票数 2
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/690355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档