Skip to content

Commit

Permalink
support monochrome mode
Browse files Browse the repository at this point in the history
Signed-off-by: Tw <[email protected]>
  • Loading branch information
tw4452852 committed Aug 7, 2023
1 parent a1bfc0e commit 77ec605
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
2 changes: 2 additions & 0 deletions pkg/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] ")
Expand Down Expand Up @@ -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()
Expand Down
27 changes: 20 additions & 7 deletions pkg/ui/styling.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func Stylings(ts ...Styling) Styling { return jointStyling(ts) }

// Common stylings.
var (
Reset Styling = reset{}
NoColor bool = false
Reset Styling = reset{}

FgDefault Styling = setForeground{nil}

Expand Down Expand Up @@ -116,12 +117,24 @@ type boolOn struct{ f boolField }
type boolOff struct{ f boolField }
type boolToggle struct{ f boolField }

func (reset) transform(s *Style) { *s = Style{} }
func (t setForeground) transform(s *Style) { s.Fg = t.c }
func (t setBackground) transform(s *Style) { s.Bg = t.c }
func (t boolOn) transform(s *Style) { *t.f.get(s) = true }
func (t boolOff) transform(s *Style) { *t.f.get(s) = false }
func (t boolToggle) transform(s *Style) { p := t.f.get(s); *p = !*p }
func (reset) transform(s *Style) { *s = Style{} }
func (t setForeground) transform(s *Style) {
if NoColor {
s.Fg = nil
} else {
s.Fg = t.c
}
}
func (t setBackground) transform(s *Style) {
if NoColor {
s.Bg = nil
} else {
s.Bg = t.c
}
}
func (t boolOn) transform(s *Style) { *t.f.get(s) = true }
func (t boolOff) transform(s *Style) { *t.f.get(s) = false }
func (t boolToggle) transform(s *Style) { p := t.f.get(s); *p = !*p }

type boolField interface{ get(*Style) *bool }

Expand Down

0 comments on commit 77ec605

Please sign in to comment.