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 <tw19881113@gmail.com>
This commit is contained in:
Tw 2023-08-08 11:40:24 +08:00
parent a1bfc0e231
commit 288e0481ac
2 changed files with 6 additions and 2 deletions

View File

@ -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()

View File

@ -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