Skip to content

Commit c8ea3a8

Browse files
authored
cmdio: add Bold, Faint, Italic, Underline, Magenta helpers (#5360)
Expose Go-callable wrappers for SGR styles already present as constants in `libs/cmdio/color.go`: `Bold` (1), `Faint` (2), `Italic` (3), `Underline` (4), and `Magenta` (35). Helpers are named after the SGR descriptor of their constant — so `ansiFaint` → `Faint`, matching the rest of the file. Doc comments use the SGR table phrasing so `Bold` and `Faint` read as paired intensities. Also adds `faint` and `underline` bindings to `RenderFuncMap`. Related to #5223. This pull request and its description were written by Isaac.
1 parent 813b754 commit c8ea3a8

2 files changed

Lines changed: 31 additions & 9 deletions

File tree

libs/cmdio/color.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ func render(ctx context.Context, code, msg string) string {
4444
return code + msg + ansiReset
4545
}
4646

47+
// Bold renders msg with increased intensity (bold).
48+
func Bold(ctx context.Context, msg string) string { return render(ctx, ansiBold, msg) }
49+
50+
// Faint renders msg with decreased intensity (faint, dim).
51+
func Faint(ctx context.Context, msg string) string { return render(ctx, ansiFaint, msg) }
52+
53+
// Italic renders msg in italic.
54+
func Italic(ctx context.Context, msg string) string { return render(ctx, ansiItalic, msg) }
55+
56+
// Underline renders msg underlined.
57+
func Underline(ctx context.Context, msg string) string { return render(ctx, ansiUnderline, msg) }
58+
4759
// Red renders msg in red.
4860
func Red(ctx context.Context, msg string) string { return render(ctx, ansiRed, msg) }
4961

@@ -56,6 +68,9 @@ func Yellow(ctx context.Context, msg string) string { return render(ctx, ansiYel
5668
// Blue renders msg in blue.
5769
func Blue(ctx context.Context, msg string) string { return render(ctx, ansiBlue, msg) }
5870

71+
// Magenta renders msg in magenta.
72+
func Magenta(ctx context.Context, msg string) string { return render(ctx, ansiMagenta, msg) }
73+
5974
// Cyan renders msg in cyan.
6075
func Cyan(ctx context.Context, msg string) string { return render(ctx, ansiCyan, msg) }
6176

@@ -70,14 +85,16 @@ func HiBlue(ctx context.Context, msg string) string { return render(ctx, ansiHiB
7085
// helpers accept a format string + args.
7186
func RenderFuncMap(ctx context.Context) template.FuncMap {
7287
return template.FuncMap{
73-
"red": templateColor(ctx, ansiRed),
74-
"green": templateColor(ctx, ansiGreen),
75-
"blue": templateColor(ctx, ansiBlue),
76-
"yellow": templateColor(ctx, ansiYellow),
77-
"magenta": templateColor(ctx, ansiMagenta),
78-
"cyan": templateColor(ctx, ansiCyan),
79-
"bold": templateColor(ctx, ansiBold),
80-
"italic": templateColor(ctx, ansiItalic),
88+
"red": templateColor(ctx, ansiRed),
89+
"green": templateColor(ctx, ansiGreen),
90+
"blue": templateColor(ctx, ansiBlue),
91+
"yellow": templateColor(ctx, ansiYellow),
92+
"magenta": templateColor(ctx, ansiMagenta),
93+
"cyan": templateColor(ctx, ansiCyan),
94+
"bold": templateColor(ctx, ansiBold),
95+
"faint": templateColor(ctx, ansiFaint),
96+
"italic": templateColor(ctx, ansiItalic),
97+
"underline": templateColor(ctx, ansiUnderline),
8198
}
8299
}
83100

libs/cmdio/color_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ func TestColorHelpersEmitSGRWhenEnabled(t *testing.T) {
2727
got string
2828
want string
2929
}{
30+
{"Bold", cmdio.Bold(ctx, "id"), "\x1b[1mid\x1b[0m"},
31+
{"Faint", cmdio.Faint(ctx, "hint"), "\x1b[2mhint\x1b[0m"},
32+
{"Italic", cmdio.Italic(ctx, "em"), "\x1b[3mem\x1b[0m"},
33+
{"Underline", cmdio.Underline(ctx, "link"), "\x1b[4mlink\x1b[0m"},
3034
{"Red", cmdio.Red(ctx, "hello"), "\x1b[31mhello\x1b[0m"},
3135
{"Green", cmdio.Green(ctx, "ok"), "\x1b[32mok\x1b[0m"},
3236
{"Yellow", cmdio.Yellow(ctx, "warn"), "\x1b[33mwarn\x1b[0m"},
3337
{"Blue", cmdio.Blue(ctx, "info"), "\x1b[34minfo\x1b[0m"},
38+
{"Magenta", cmdio.Magenta(ctx, "trace"), "\x1b[35mtrace\x1b[0m"},
3439
{"Cyan", cmdio.Cyan(ctx, "debug"), "\x1b[36mdebug\x1b[0m"},
3540
{"HiBlack", cmdio.HiBlack(ctx, "dim"), "\x1b[90mdim\x1b[0m"},
3641
{"HiBlue", cmdio.HiBlue(ctx, "APP"), "\x1b[94mAPP\x1b[0m"},
@@ -64,7 +69,7 @@ func TestRenderFuncMap(t *testing.T) {
6469
ctx := ttyContext(t)
6570
fm := cmdio.RenderFuncMap(ctx)
6671

67-
for _, name := range []string{"red", "green", "blue", "yellow", "magenta", "cyan", "bold", "italic"} {
72+
for _, name := range []string{"red", "green", "blue", "yellow", "magenta", "cyan", "bold", "faint", "italic", "underline"} {
6873
_, ok := fm[name].(func(string, ...any) string)
6974
assert.True(t, ok, "FuncMap missing %q or wrong signature", name)
7075
}

0 commit comments

Comments
 (0)