Skip to content

Commit

Permalink
internal: v1.3.0 (#6)
Browse files Browse the repository at this point in the history
Added the DisableColors and EnableColors functions
to disable and enable colors globally.
  • Loading branch information
i582 authored May 8, 2021
1 parent 7812ced commit cde156d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
16 changes: 15 additions & 1 deletion cmd/cfmt/cfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 7 additions & 3 deletions internal/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions tests/disable_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
2 changes: 1 addition & 1 deletion tests/style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cde156d

Please sign in to comment.