diff --git a/cmd/cfmt/cfmt.go b/cmd/cfmt/cfmt.go index ecb2813..ff0fdc6 100644 --- a/cmd/cfmt/cfmt.go +++ b/cmd/cfmt/cfmt.go @@ -4,10 +4,24 @@ import ( "fmt" "io" "os" + "sync/atomic" "github.com/i582/cfmt/internal" ) +var disableColors int32 + +// DisableColors globally disables colors. +// After parsing, no styles will be applied and the output will be clean text. +func DisableColors() { + atomic.AddInt32(&disableColors, 1) +} + +// EnableColors globally enables colors. +func EnableColors() { + atomic.AddInt32(&disableColors, -1) +} + // RegisterStyle registers a new custom style. // // It will look like this: @@ -24,7 +38,7 @@ func RegisterStyle(name string, fn func(string) string) { // Sprint is the same as fmt.Sprint. func Sprint(a ...interface{}) string { - return internal.ParseAndApply(fmt.Sprint(a...)) + return internal.ParseAndApply(fmt.Sprint(a...), disableColors == 1) } // Fprint is the same as fmt.Fprint. diff --git a/internal/parser.go b/internal/parser.go index 81777bf..f62df77 100644 --- a/internal/parser.go +++ b/internal/parser.go @@ -6,7 +6,7 @@ import ( ) // ParseAndApply parses the passed format string and applies styles, returning a styled string. -func ParseAndApply(text string) string { +func ParseAndApply(text string, disable bool) string { var resParts = make([]string, 0, 50) var err error @@ -149,7 +149,7 @@ func ParseAndApply(text string) string { // text from :: or previous | to current | singleFormat := text[tempTokenStartIndex:tempTokenEndIndex] - lastToken, err = applyStyle(lastToken, singleFormat) + lastToken, err = applyStyle(lastToken, singleFormat, disable) if err != nil { log.Fatalf("Error parse style string in '%s' text string: %v", strings.ReplaceAll(text, "\n", "\\n"), err) } @@ -164,7 +164,7 @@ func ParseAndApply(text string) string { // last format singleFormat := text[tempTokenStartIndex:tempTokenEndIndex] - lastToken, err = applyStyle(lastToken, singleFormat) + lastToken, err = applyStyle(lastToken, singleFormat, disable) if err != nil { log.Fatalf("Error parse style string in '%s' text string: %v", strings.ReplaceAll(text, "\n", "\\n"), err) } @@ -192,7 +192,7 @@ func ParseAndApply(text string) string { if lastIsFormatGroup { singleFormat := text[tempTokenStartIndex:] - lastToken, err = applyStyle(lastToken, singleFormat) + lastToken, err = applyStyle(lastToken, singleFormat, disable) if err != nil { log.Fatalf("Error parse style string in '%s' text string: %v", strings.ReplaceAll(text, "\n", "\\n"), err) } diff --git a/internal/style.go b/internal/style.go index 37bcc6c..796ad5e 100644 --- a/internal/style.go +++ b/internal/style.go @@ -8,14 +8,14 @@ import ( ) // StyleBuilder is a function that returns a styled string based on the supplied style. -func StyleBuilder(styles []string, text string) (string, error) { +func StyleBuilder(styles []string, text string, disable bool) (string, error) { if len(styles) == 0 { return "", fmt.Errorf("style string is empty") } var err error for _, style := range styles { - text, err = applyStyle(text, style) + text, err = applyStyle(text, style, disable) if err != nil { return "", err } @@ -25,7 +25,11 @@ func StyleBuilder(styles []string, text string) (string, error) { } // applyStyle is a function that apply a style for string. -func applyStyle(text, style string) (string, error) { +func applyStyle(text, style string, disable bool) (string, error) { + if disable { + return text, nil + } + if isHex(style) { err := checkHex(style) if err != nil { diff --git a/tests/disable_test.go b/tests/disable_test.go new file mode 100644 index 0000000..a8448cd --- /dev/null +++ b/tests/disable_test.go @@ -0,0 +1,15 @@ +package tests + +import ( + "testing" + + "github.com/i582/cfmt/cmd/cfmt" +) + +func TestDisableColors(t *testing.T) { + cfmt.DisableColors() + cfmt.Println("{{привет, correct group}}::red|underline and {{other}}::red") + + cfmt.EnableColors() + cfmt.Println("{{привет, correct group}}::red|underline and {{other}}::red") +} diff --git a/tests/style_test.go b/tests/style_test.go index 8b54054..0be9970 100644 --- a/tests/style_test.go +++ b/tests/style_test.go @@ -69,7 +69,7 @@ func TestStyleBuilder(t *testing.T) { } for _, suite := range suites { - _, err := internal.StyleBuilder(suite.Value, "") + _, err := internal.StyleBuilder(suite.Value, "", false) if err == nil && suite.Error != "" { t.Errorf("mismatch\nwant: ''\nhave: %s", suite.Error) continue