Skip to content

Commit

Permalink
Add locking to color settings
Browse files Browse the repository at this point in the history
Add sync lock to the color settings.

Introduce convenience function to set color settings.
  • Loading branch information
HeavyWombat committed Mar 18, 2021
1 parent 29da5e4 commit 414b7de
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 62 deletions.
68 changes: 51 additions & 17 deletions bunt.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package bunt
import (
"fmt"
"strings"
"sync"

"github.com/gonvenience/term"
)
Expand All @@ -37,22 +38,27 @@ const (
)

// ColorSetting defines the coloring setting to be used
var ColorSetting = AUTO
var ColorSetting SwitchState = SwitchState{value: AUTO}

// TrueColorSetting defines the true color usage setting to be used
var TrueColorSetting = AUTO
var TrueColorSetting SwitchState = SwitchState{value: AUTO}

// switchState is the type to cover different preferences/settings like: on, off, or auto
type switchState int
type state int

// Supported setting states
const (
ON = switchState(iota)
AUTO = state(iota)
ON
OFF
AUTO
)

func (s switchState) String() string {
// SwitchState is the type to cover different preferences/settings like: on, off, or auto
type SwitchState struct {
sync.Mutex
value state
}

func (s state) String() string {
switch s {
case ON:
return "on"
Expand All @@ -64,19 +70,28 @@ func (s switchState) String() string {
return "auto"
}

panic("unsupported switch state")
panic("unsupported state")
}

func (s *switchState) Set(setting string) error {
func (s *SwitchState) String() string {
return s.value.String()
}

// Set updates the switch state based on the provided setting, or fails with an
// error in case the setting cannot be parsed
func (s *SwitchState) Set(setting string) error {
s.Lock()
defer s.Unlock()

switch strings.ToLower(setting) {
case "auto":
*s = AUTO
s.value = AUTO

case "off", "no", "false":
*s = OFF
s.value = OFF

case "on", "yes", "true":
*s = ON
s.value = ON

default:
return fmt.Errorf("invalid state '%s' used, supported modes are: auto, on, or off", setting)
Expand All @@ -85,20 +100,39 @@ func (s *switchState) Set(setting string) error {
return nil
}

func (s switchState) Type() string {
// Type returns the type name of switch state, which is an empty string for now
func (s *SwitchState) Type() string {
return ""
}

// UseColors return whether colors are used or not based on the configured color
// setting or terminal capabilities
func UseColors() bool {
return (ColorSetting == ON) ||
(ColorSetting == AUTO && term.IsTerminal() && !term.IsDumbTerminal())
ColorSetting.Lock()
defer ColorSetting.Unlock()

return (ColorSetting.value == ON) ||
(ColorSetting.value == AUTO && term.IsTerminal() && !term.IsDumbTerminal())
}

// UseTrueColor returns whether true color colors should be used or not based on
// the configured true color usage setting or terminal capabilities
func UseTrueColor() bool {
return (TrueColorSetting == ON) ||
(TrueColorSetting == AUTO && term.IsTrueColor())
TrueColorSetting.Lock()
defer TrueColorSetting.Unlock()

return (TrueColorSetting.value == ON) ||
(TrueColorSetting.value == AUTO && term.IsTrueColor())
}

// SetColorSettings is a convenience function to set both color settings at the
// same time using the internal locks
func SetColorSettings(color state, trueColor state) {
ColorSetting.Lock()
defer ColorSetting.Unlock()
ColorSetting.value = color

TrueColorSetting.Lock()
defer TrueColorSetting.Unlock()
TrueColorSetting.value = trueColor
}
2 changes: 1 addition & 1 deletion bunt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

var _ = Describe("settings tests", func() {
var parse = func(setting string) (string, error) {
tmp := AUTO
var tmp = &SwitchState{}
err := tmp.Set(setting)
return tmp.String(), err
}
Expand Down
18 changes: 6 additions & 12 deletions colors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ import (
var _ = Describe("color specific tests", func() {
Context("fallback to 4 bit colors for non true color terminals", func() {
BeforeEach(func() {
ColorSetting = ON
TrueColorSetting = OFF
SetColorSettings(ON, OFF)
})

AfterEach(func() {
ColorSetting = AUTO
TrueColorSetting = AUTO
SetColorSettings(AUTO, AUTO)
})

var (
Expand Down Expand Up @@ -209,13 +207,11 @@ var _ = Describe("color specific tests", func() {

Context("custom colors in text annotation", func() {
BeforeEach(func() {
ColorSetting = ON
TrueColorSetting = ON
SetColorSettings(ON, ON)
})

AfterEach(func() {
ColorSetting = AUTO
TrueColorSetting = AUTO
SetColorSettings(AUTO, AUTO)
})

It("should parse hexcolors in text annotations", func() {
Expand All @@ -226,13 +222,11 @@ var _ = Describe("color specific tests", func() {

Context("random colors", func() {
BeforeEach(func() {
ColorSetting = ON
TrueColorSetting = OFF
SetColorSettings(ON, OFF)
})

AfterEach(func() {
ColorSetting = AUTO
TrueColorSetting = AUTO
SetColorSettings(AUTO, AUTO)
})

It("should create a list of random terminal friendly colors", func() {
Expand Down
6 changes: 2 additions & 4 deletions convenience_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ import (

var _ = Describe("convenience functions", func() {
BeforeEach(func() {
ColorSetting = ON
TrueColorSetting = ON
SetColorSettings(ON, ON)
})

AfterEach(func() {
ColorSetting = AUTO
TrueColorSetting = AUTO
SetColorSettings(AUTO, AUTO)
})

Context("substring function", func() {
Expand Down
4 changes: 2 additions & 2 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import (
var _ = Describe("error functions", func() {
Context("process markdown style in Errorf function", func() {
BeforeEach(func() {
ColorSetting = ON
SetColorSettings(ON, OFF)
})

AfterEach(func() {
ColorSetting = AUTO
SetColorSettings(AUTO, AUTO)
})

It("should parse and process markdown style in Errorf", func() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ require (
github.com/gonvenience/term v1.0.1
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3
github.com/onsi/ginkgo v1.15.0
github.com/onsi/ginkgo v1.15.2
github.com/onsi/gomega v1.11.0
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.15.0 h1:1V1NfVQR87RtWAgp1lv9JZJ5Jap+XFGKPi00andXGi4=
github.com/onsi/ginkgo v1.15.0/go.mod h1:hF8qUzuuC8DJGygJH3726JnCZX4MYbRB8yFfISqnKUg=
github.com/onsi/ginkgo v1.15.2 h1:l77YT15o814C2qVL47NOyjV/6RbaP7kKdrvZnxQ3Org=
github.com/onsi/ginkgo v1.15.2/go.mod h1:Dd6YFfwBW84ETqqtL0CPyPXillHgY6XhQH3uuCCTr/o=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48=
Expand Down
4 changes: 2 additions & 2 deletions model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import (
var _ = Describe("working with colored strings", func() {
Context("cut a substring from a colored string", func() {
BeforeEach(func() {
ColorSetting = ON
SetColorSettings(ON, OFF)
})

AfterEach(func() {
ColorSetting = AUTO
SetColorSettings(AUTO, AUTO)
})

It("should be possible to cut out a substring from a colored string", func() {
Expand Down
6 changes: 2 additions & 4 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ import (

var _ = Describe("parse input string", func() {
BeforeEach(func() {
ColorSetting = ON
TrueColorSetting = ON
SetColorSettings(ON, ON)
})

AfterEach(func() {
ColorSetting = AUTO
TrueColorSetting = AUTO
SetColorSettings(AUTO, AUTO)
})

Context("parse supported ANSI sequences from an input reader", func() {
Expand Down
18 changes: 10 additions & 8 deletions print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ var _ = Describe("print functions", func() {
}

BeforeEach(func() {
ColorSetting = ON
SetColorSettings(ON, AUTO)
})

AfterEach(func() {
ColorSetting = AUTO
SetColorSettings(AUTO, AUTO)
})

It("should parse and process markdown style in Print", func() {
Expand Down Expand Up @@ -87,13 +87,15 @@ var _ = Describe("print functions", func() {
)

BeforeEach(func() {
ColorSetting = ON
SetColorSettings(ON, AUTO)
buf = bytes.Buffer{}
out = bufio.NewWriter(&buf)
})

AfterEach(func() {
ColorSetting = AUTO
buf.Reset()
out = nil
SetColorSettings(AUTO, AUTO)
})

It("should parse and process markdown style in Fprint", func() {
Expand All @@ -117,11 +119,11 @@ var _ = Describe("print functions", func() {

Context("process markdown style in Sprint functions", func() {
BeforeEach(func() {
ColorSetting = ON
SetColorSettings(ON, AUTO)
})

AfterEach(func() {
ColorSetting = AUTO
SetColorSettings(AUTO, AUTO)
})

It("should parse and process markdown style in Sprint", func() {
Expand All @@ -139,11 +141,11 @@ var _ = Describe("print functions", func() {

Context("weird use cases and issues", func() {
BeforeEach(func() {
ColorSetting = ON
SetColorSettings(ON, AUTO)
})

AfterEach(func() {
ColorSetting = AUTO
SetColorSettings(AUTO, AUTO)
})

It("should ignore escape sequences that cannot be processed", func() {
Expand Down
16 changes: 5 additions & 11 deletions render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,11 @@ import (
)

var _ = Describe("render colored strings", func() {
BeforeEach(func() {
ColorSetting = ON
TrueColorSetting = ON
})

AfterEach(func() {
ColorSetting = AUTO
TrueColorSetting = AUTO
})

Context("verify that rendering of colored strings returns correct results", func() {
It("should render colored output when colors are enabled", func() {
SetColorSettings(ON, ON)
defer SetColorSettings(AUTO, AUTO)

input := "Example: \x1b[1mbold\x1b[0m, \x1b[3mitalic\x1b[0m, \x1b[4munderline\x1b[0m, \x1b[38;2;133;247;7mforeground\x1b[0m, and \x1b[48;2;133;247;7mbackground\x1b[0m."
result, err := ParseString(input)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -49,7 +42,8 @@ var _ = Describe("render colored strings", func() {
})

It("should render plain output when colors are not enabled", func() {
ColorSetting = OFF
SetColorSettings(OFF, OFF)
defer SetColorSettings(AUTO, AUTO)

input := "Example: \x1b[1mbold\x1b[0m, \x1b[3mitalic\x1b[0m, \x1b[4munderline\x1b[0m, \x1b[38;2;133;247;7mforeground\x1b[0m, and \x1b[48;2;133;247;7mbackground\x1b[0m."
result, err := ParseString(input)
Expand Down

0 comments on commit 414b7de

Please sign in to comment.