AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 690355
Accepted
Vlastimil Burián
Vlastimil Burián
Asked: 2022-02-13 04:09:16 +0800 CST2022-02-13 04:09:16 +0800 CST 2022-02-13 04:09:16 +0800 CST

terminfo/termcap `tput bold`/`tput md`:粗体文本的可移植性

  • 772

假设我的一个便携式 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是否可移植,如果不是,粗体文本的限制是什么?

感谢您的时间。

colors portability
  • 1 1 个回答
  • 118 Views

1 个回答

  • Voted
  1. Best Answer
    Thomas Dickey
    2022-02-13T04:48:33+08:002022-02-13T04:48:33+08:00

    基本限制是关闭粗体 时。一些终端支持 ECMA-48 控制SGR 22(既不加粗也不加淡,不影响颜色)。然而

    • 在 terminfo 或 termcap 中没有预定义的加粗功能(参见手册页)。
    • 关闭粗体与关闭颜色之间也没有区别。

    为了便携性,您必须考虑到这一点(如果您关闭粗体而不打算影响颜色,则重新打开颜色)。

    • 1

相关问题

  • 如何 grep MATCH 彩色输入?

  • 如何以可移植的方式在文件之间更改 AWK 中的 RS 变量

  • 如何为 shell 脚本的某些输出着色?

  • BASH - 自定义 PS1 以运行命令

  • 使用 ImageMagick 创建具有多行的调色板图像

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve