feat: NO_COLOR env to not use ansi colors in miro

This commit is contained in:
2026-03-21 06:48:23 +00:00
parent 7b61da612a
commit b3fc15d3ae
4 changed files with 71 additions and 8 deletions
+10 -4
View File
@@ -21,9 +21,6 @@ var (
pass: 0x7bf1a8,
fail: 0xff8fa3,
}
chevron = NewStyle().FG(palette.chevronTeal).Bold().Italic().Apply("")
prefix = NewStyle().FG(palette.miroGreen).Bold().Italic().Apply("miro") + " " + chevron + " "
)
func label(text string, color uint32) string {
@@ -42,10 +39,19 @@ func LabelInfo(text string) string {
return label(text, palette.info)
}
func noColor() bool {
return os.Getenv("NO_COLOR") != ""
}
func prefix() string {
chevron := NewStyle().FG(palette.chevronTeal).Bold().Italic().Apply("")
return NewStyle().FG(palette.miroGreen).Bold().Italic().Apply("miro") + " " + chevron + " "
}
func Format(msg string) string {
body := strings.TrimRight(msg, "\n")
suffix := msg[len(body):]
return prefix + body + suffix
return prefix() + body + suffix
}
func Println(msg string) {
+58
View File
@@ -0,0 +1,58 @@
package output
import (
"os"
"strings"
"testing"
)
func TestFormatIncludesANSIByDefault(t *testing.T) {
t.Setenv("NO_COLOR", "")
got := Format("hello\n")
if !strings.Contains(got, "\x1b[") {
t.Fatalf("Format() = %q, want ANSI styling", got)
}
if !strings.Contains(got, "hello\n") {
t.Fatalf("Format() = %q, want message body", got)
}
}
func TestFormatPlainWhenNoColorSet(t *testing.T) {
t.Setenv("NO_COLOR", "1")
if got := Format("hello\n"); got != "miro hello\n" {
t.Fatalf("Format() = %q, want %q", got, "miro hello\n")
}
}
func TestLabelsPlainWhenNoColorSet(t *testing.T) {
t.Setenv("NO_COLOR", "1")
if got := LabelInfo("RUN"); got != "RUN" {
t.Fatalf("LabelInfo() = %q, want %q", got, "RUN")
}
if got := LabelPass("PASS"); got != "PASS" {
t.Fatalf("LabelPass() = %q, want %q", got, "PASS")
}
if got := LabelFail("FAIL"); got != "FAIL" {
t.Fatalf("LabelFail() = %q, want %q", got, "FAIL")
}
}
func TestFormatReadsNoColorAtRuntime(t *testing.T) {
t.Setenv("NO_COLOR", "")
styled := Format("hello\n")
if err := os.Setenv("NO_COLOR", "1"); err != nil {
t.Fatalf("Setenv() error = %v", err)
}
plain := Format("hello\n")
if !strings.Contains(styled, "\x1b[") {
t.Fatalf("styled Format() = %q, want ANSI styling", styled)
}
if plain != "miro hello\n" {
t.Fatalf("plain Format() = %q, want %q", plain, "miro hello\n")
}
}
+1 -1
View File
@@ -40,7 +40,7 @@ func (s Style) BG(rgb uint32) Style {
}
func (s Style) Apply(text string) string {
if !s.bold && !s.italic && s.fg == nil && s.bg == nil {
if noColor() || (!s.bold && !s.italic && s.fg == nil && s.bg == nil) {
return text
}