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 3, 2023
1 parent a1bfc0e commit 4166449
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
5 changes: 5 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 All @@ -33,6 +34,7 @@ type Program struct {
rc string
json *bool
daemonPaths *prog.DaemonPaths
noColor bool
}

func (p *Program) RegisterFlags(fs *prog.FlagSet) {
Expand All @@ -48,6 +50,8 @@ func (p *Program) RegisterFlags(fs *prog.FlagSet) {
"Don't read the RC file when running interactively")
fs.StringVar(&p.rc, "rc", "",
"Path to the RC file when running interactively")
fs.BoolVar(&p.noColor, "nocolor", false,
"Monochrome mode")

p.json = fs.JSON()
if p.ActivateDaemon != nil {
Expand All @@ -61,6 +65,7 @@ func (p *Program) Run(fds [3]*os.File, args []string) error {
cleanup2 := initSignal(fds)
defer cleanup2()

ui.NoColor = p.noColor
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 4166449

Please sign in to comment.