Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions libs/cmdio/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ func render(ctx context.Context, code, msg string) string {
return code + msg + ansiReset
}

// Bold renders msg with increased intensity (bold).
func Bold(ctx context.Context, msg string) string { return render(ctx, ansiBold, msg) }

// Faint renders msg with decreased intensity (faint, dim).
func Faint(ctx context.Context, msg string) string { return render(ctx, ansiFaint, msg) }

// Italic renders msg in italic.
func Italic(ctx context.Context, msg string) string { return render(ctx, ansiItalic, msg) }

// Underline renders msg underlined.
func Underline(ctx context.Context, msg string) string { return render(ctx, ansiUnderline, msg) }

// Red renders msg in red.
func Red(ctx context.Context, msg string) string { return render(ctx, ansiRed, msg) }

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

// Magenta renders msg in magenta.
func Magenta(ctx context.Context, msg string) string { return render(ctx, ansiMagenta, msg) }

// Cyan renders msg in cyan.
func Cyan(ctx context.Context, msg string) string { return render(ctx, ansiCyan, msg) }

Expand All @@ -70,14 +85,16 @@ func HiBlue(ctx context.Context, msg string) string { return render(ctx, ansiHiB
// helpers accept a format string + args.
func RenderFuncMap(ctx context.Context) template.FuncMap {
return template.FuncMap{
"red": templateColor(ctx, ansiRed),
"green": templateColor(ctx, ansiGreen),
"blue": templateColor(ctx, ansiBlue),
"yellow": templateColor(ctx, ansiYellow),
"magenta": templateColor(ctx, ansiMagenta),
"cyan": templateColor(ctx, ansiCyan),
"bold": templateColor(ctx, ansiBold),
"italic": templateColor(ctx, ansiItalic),
"red": templateColor(ctx, ansiRed),
"green": templateColor(ctx, ansiGreen),
"blue": templateColor(ctx, ansiBlue),
"yellow": templateColor(ctx, ansiYellow),
"magenta": templateColor(ctx, ansiMagenta),
"cyan": templateColor(ctx, ansiCyan),
"bold": templateColor(ctx, ansiBold),
"faint": templateColor(ctx, ansiFaint),
"italic": templateColor(ctx, ansiItalic),
"underline": templateColor(ctx, ansiUnderline),
}
}

Expand Down
7 changes: 6 additions & 1 deletion libs/cmdio/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ func TestColorHelpersEmitSGRWhenEnabled(t *testing.T) {
got string
want string
}{
{"Bold", cmdio.Bold(ctx, "id"), "\x1b[1mid\x1b[0m"},
{"Faint", cmdio.Faint(ctx, "hint"), "\x1b[2mhint\x1b[0m"},
{"Italic", cmdio.Italic(ctx, "em"), "\x1b[3mem\x1b[0m"},
{"Underline", cmdio.Underline(ctx, "link"), "\x1b[4mlink\x1b[0m"},
{"Red", cmdio.Red(ctx, "hello"), "\x1b[31mhello\x1b[0m"},
{"Green", cmdio.Green(ctx, "ok"), "\x1b[32mok\x1b[0m"},
{"Yellow", cmdio.Yellow(ctx, "warn"), "\x1b[33mwarn\x1b[0m"},
{"Blue", cmdio.Blue(ctx, "info"), "\x1b[34minfo\x1b[0m"},
{"Magenta", cmdio.Magenta(ctx, "trace"), "\x1b[35mtrace\x1b[0m"},
{"Cyan", cmdio.Cyan(ctx, "debug"), "\x1b[36mdebug\x1b[0m"},
{"HiBlack", cmdio.HiBlack(ctx, "dim"), "\x1b[90mdim\x1b[0m"},
{"HiBlue", cmdio.HiBlue(ctx, "APP"), "\x1b[94mAPP\x1b[0m"},
Expand Down Expand Up @@ -64,7 +69,7 @@ func TestRenderFuncMap(t *testing.T) {
ctx := ttyContext(t)
fm := cmdio.RenderFuncMap(ctx)

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