git bash - 在 printf 输出中使用 git 颜色

问题描述 投票:0回答:1

我写了一个 git status 的变体。如何使用 git status 用于分阶段和未分阶段更改的绿色和红色来为我的输出着色?最好是找到红色和绿色的运行时 git 定义,但这并不那么重要:硬编码的解决方案就可以了。

colors printf git-bash
1个回答
0
投票

Git 可能会使用 ANSI 转义码:https://en.wikipedia.org/wiki/ANSI_escape_code

这通常意味着您在

-e
中使用
echo
或在 printf 中使用 %b (使用适用于 Windows 2.43.0 的 Git 进行测试):

declare color_red="\\e[1;31m"
declare color_none="\\e[0m"
declare color_green="\\e[1;32m"
echo -e "${color_red}RED${color_none}"
printf "%bRED%b" "${color_red}" "${color_none}"

默认的 git 颜色由

color.status.<slot>
和布尔值
color.status
处理:我没有使用
git config
命令找到默认值。

© www.soinside.com 2019 - 2024. All rights reserved.