gpt4 book ai didi

makefile - 从 Makefile 规则打印粗体或彩色文本

转载 作者:行者123 更新时间:2023-12-03 13:54:30 24 4
gpt4 key购买 nike

我正在尝试从以下 Makefile 打印粗体文本:

printf-bold-1:
@printf "normal text - \e[1mbold text\e[0m"

但是,转义序列按原样打印,因此在运行 make printf-bold-1 时, 我有 :

normal text - \e[1mbold text\e[0m



而不是预期:

normal text - bold text



这很奇怪,因为我可以从终端打印粗体文本:直接运行命令 printf "normal text - \e[1mbold text\e[0m"产生,如预期的那样:

normal text - bold text



Makefile ,我尝试使用 @echoecho而不是 @printf ,或打印 \x1b而不是 \e ,但没有成功。

以下是一些描述我的环境(带有标准 Gnome 终端的 Linux)的变量,如果可以的话:
COLORTERM=gnome-terminal
TERM=xterm-256color

另请注意,在某些同事的笔记本电脑 (Mac) 上,可以正确打印粗体文本。

什么是可移植的方式,适用于各种环境,从 Makefile 打印粗体或彩色文本规则?

最佳答案

您应该使用通常的 tput用于为实际终端生成正确转义序列的程序,而不是硬编码特定字符串(例如,在 Emacs 编译缓冲区中看起来很难看):

printf-bold-1:
@printf "normal text - `tput bold`bold text`tput sgr0`"

当然,您可以将结果存储到 Make 变量中,以减少子 shell 的数量:
bold := $(shell tput bold)
sgr0 := $(shell tput sgr0)

printf-bold-1:
@printf 'normal text - $(bold)bold text$(sgr0)'

关于makefile - 从 Makefile 规则打印粗体或彩色文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54039983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com