How to get ANSI escape codes (colors, text formatting) into my commit messages in the `git log` output

huangapple go评论63阅读模式
英文:

How to get ANSI escape codes (colors, text formatting) into my commit messages in the `git log` output

问题

在你的Bash终端中运行以下命令:

f="\e[;1;4;5;94m"   # 起始ANSI格式字符串
F="\e[m"            # 结束ANSI格式字符串
echo -e "${f}This string is bold, underlined, blinking, bright blue.${F} This is not."

你会看到字符串以粗体、下划线、闪烁和亮蓝色的格式显示,这是使用我添加的ANSI转义码来实现的。

我想要将这种格式应用于我的git log消息中。我该如何做?

我目前的尝试,比如这样,只是插入了原始字符,而没有进行格式化:

# 无法工作
git commit -m "${f}This string is bold, underlined, blinking, bright blue.${F} This is not."

我还尝试将上面第一个echo的输出粘贴到我的git编辑器中(当前使用Sublime Text,如我在这里所示),无论是手动包含转义码还是不包含转义码,都不起作用。

请注意,这个问题的存在似乎表明我正在尝试的事情至少是可能的:git中的ANSI颜色未正确显示-- 除非我误解了,也许他们只是在谈论git grep显示受git控制的文件。更新:我认为我误解了:我认为他们在谈论git log中的ANSI转义码,比如提交哈希本身的着色和粗体。

为什么我想这样做?

我认为在我的git log输出中使一些自定义提交消息闪烁并以蓝色粗体显示会很有趣,以突出某些特殊的提交。我刚刚编写了一个Bash库(ansi_text_format_lib.sh),以便轻松进行文本格式化,并希望在命令行中使用它来实现这一目标。

英文:

Run this in your Bash terminal:

f="\e[;1;4;5;94m"   # beginning ANSI format string
F="\e[m"            # ending ANSI format string
echo -e "${f}This string is bold, underlined, blinking, bright blue.${F} This is not."

You'll see the string formatted bold, underlined, blinking, and bright blue, per the ANSI escape codes I put in it.

I'd like to get formatting text like that into my git log messages. How can I do that?

My current attempts, such as this, just put in the raw chars instead of doing the formatting:

# doesn't work
git commit -m "${f}This string is bold, underlined, blinking, bright blue.${F} This is not."

I also tried pasting the output from the first echo above, both with and without the escaped codes manually in it as chars, into my git editor (currently Sublime Text, as I show how to set here), and that didn't work either.

Note that the very existence of this question seems to indicate that what I am trying to do is at least possible: ANSI color in git is not displayed correctly--unless I'm misunderstanding and hey are just talking about git grep showing git-controlled files. Update: I think I'm misunderstanding: I think they are talking about the ANSI escape codes in git log, such as the coloring and bold of the commit hashes themselves.

Why do I want to do this?

I think it would be neat to make some of my custom commit messages in my git log output blink and be blue bold or something, to make certain really special commits stand out. I just wrote a Bash library (ansi_text_format_lib.sh) to allow easy text formatting and thought I'd use it at the command-line to do this.

答案1

得分: 0

git commit -m "${f}This string is bold, underlined, blinking, bright blue.${F} This is not."

在这里发生的是,您在提交消息中使用了 ANSI 颜色控制序列的转义序列。

您想要的是将 ANSI 颜色控制序列本身放入提交消息中。我知道在变量替换期间获得它的最简单方法是使用 prompt-escape 扩展:

# (首先设置您的变量,就像您已经做的那样):
f='\e[;1;4;5;94m'
F='\e[m'

# 然后
git commit -m "${f@P}This string is bold, underlined, blinking, bright blue.${F@P} This is not."

运行 man bash 并查找参数转换和提示以了解您可以使用它做什么其他操作。


<details>
<summary>英文:</summary>

```bash
git commit -m &quot;${f}This string is bold, underlined, blinking, bright blue.${F} This is not.&quot;

What's happening there is, you're putting escape sequences for the ansi color control sequences into the commit message.

What you want is to put the ansi color control sequences themselves into the commit message. Easiest way I know to get that during variable substitution is to use prompt-escape expansion:

# (First set your variables like you already do):
f=&#39;\e[;1;4;5;94m&#39;
F=&#39;\e[m&#39;

# then
git commit -m &quot;${f@P}This string is bold, underlined, blinking, bright blue.${F@P} This is not.&quot;

Run man bash and hunt up parameter transformation and prompting to see what else you can do with that.

huangapple
  • 本文由 发表于 2023年2月19日 03:21:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定