From 288e0481aca4d2e19d1ccd667b88728b71645fe5 Mon Sep 17 00:00:00 2001 From: Tw Date: Tue, 8 Aug 2023 11:40:24 +0800 Subject: [PATCH] support monochrome mode Use NO_COLOR environment variable to enable monochrome mode in which foreground and background colors will be stripped. Signed-off-by: Tw --- pkg/shell/shell.go | 2 ++ pkg/ui/style.go | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/shell/shell.go b/pkg/shell/shell.go index fa1a9f4b..87386248 100644 --- a/pkg/shell/shell.go +++ b/pkg/shell/shell.go @@ -19,6 +19,7 @@ import ( "src.elv.sh/pkg/parse" "src.elv.sh/pkg/prog" "src.elv.sh/pkg/sys" + "src.elv.sh/pkg/ui" ) var logger = logutil.GetLogger("[shell] ") @@ -61,6 +62,7 @@ func (p *Program) Run(fds [3]*os.File, args []string) error { cleanup2 := initSignal(fds) defer cleanup2() + ui.NoColor = os.Getenv("NO_COLOR") != "" interactive := len(args) == 0 ev := p.makeEvaler(fds[2], interactive) defer ev.PreExit() diff --git a/pkg/ui/style.go b/pkg/ui/style.go index 7cc87e12..249da0e9 100644 --- a/pkg/ui/style.go +++ b/pkg/ui/style.go @@ -5,6 +5,8 @@ import ( "strings" ) +var NoColor bool = false + // Style specifies how something (mostly a string) shall be displayed. type Style struct { Fg Color @@ -32,10 +34,10 @@ func (s Style) SGRValues() []string { addIf(s.Underlined, "4") addIf(s.Blink, "5") addIf(s.Inverse, "7") - if s.Fg != nil { + if s.Fg != nil && !NoColor { sgr = append(sgr, s.Fg.fgSGR()) } - if s.Bg != nil { + if s.Bg != nil && !NoColor { sgr = append(sgr, s.Bg.bgSGR()) } return sgr