Skip to content

Commit

Permalink
all: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
i582 committed Nov 15, 2020
1 parent 35bd757 commit 52f4054
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 101 deletions.
4 changes: 1 addition & 3 deletions internal/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"github.com/gookit/color"
)

type styleCallback func(func(string) string) func(string) string

// CustomMap is the custom styles map
var CustomMap = map[string]styleCallback{
var CustomMap = map[string]func(string) string{
"overline": overline,
"faint": faint,
"reverse": reverse,
Expand Down
4 changes: 2 additions & 2 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ func Parse(format string) string {
}

func groupStyle(format string, token string, style string) string {
styler, err := StyleBuilder(style)
styleText, err := StyleBuilder(style, token)
if err != nil {
log.Fatalf("Error parse style string in '%s' format string: %v", format, err)
}

return styler(token)
return styleText
}
49 changes: 11 additions & 38 deletions internal/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,50 @@ import (
"github.com/gookit/color"
)

// StyleBuilder is a function that returns a callback function that will style
// the supplied string according to the supplied format string.
func StyleBuilder(styleString string) (func(string) string, error) {
// StyleBuilder is a function that returns a styled string based on the supplied style.
func StyleBuilder(styleString string, text string) (string, error) {
if styleString == "" {
return nil, fmt.Errorf("style string is empty")
return "", fmt.Errorf("style string is empty")
}

var styleParts = strings.Split(styleString, "|")

var hexColorFuncs []func(string, func(string) string) func(string) string
var hexColors []string

var customStyles []func(func(string) string) func(string) string

var colors = make([]color.Color, 0, len(styleParts))
color.New()

for _, style := range styleParts {
if isHex(style) {
err := checkHex(style)
if err != nil {
return nil, err
return "", err
}

hexColorFuncs = append(hexColorFuncs, hexForegroundColor)
hexColors = append(hexColors, style)
text = hexForegroundColorFunc(style, text)
continue
}

if isBackgroundHex(style) {
rawHex := strings.TrimPrefix(style, "bg")
err := checkHex(rawHex)
if err != nil {
return nil, err
return "", err
}

hexColorFuncs = append(hexColorFuncs, hexBackgroundColor)
hexColors = append(hexColors, rawHex)
text = hexBackgroundColorFunc(style, text)
continue
}

clr, ok := Map[style]
if !ok {
customStyle, ok := CustomMap[style]
customStyleFunc, ok := CustomMap[style]
if !ok {
return nil, fmt.Errorf("unknown style %s", style)
return "", fmt.Errorf("unknown style %s", style)
}

customStyles = append(customStyles, customStyle)
text = customStyleFunc(text)
continue
}
colors = append(colors, clr)
}

outFun := func(text string) string { return text }

for index, fun := range hexColorFuncs {
clr := hexColors[index]
outFun = fun(clr, outFun)
}

for _, style := range customStyles {
outFun = style(outFun)
}

colorStyle := color.New(colors...)

return func(s string) string {
return outFun(colorStyle.Sprint(s))
}, nil
return colorStyle.Sprint(text), nil
}

func isHex(val string) bool {
Expand All @@ -87,7 +61,6 @@ func checkHex(val string) error {
if len(val) != 7 {
return fmt.Errorf("invalid hex: length of hex color must be 6")
}

return nil
}

Expand Down
80 changes: 30 additions & 50 deletions internal/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,44 @@ import (
"github.com/muesli/termenv"
)

func overline(f func(text string) string) func(text string) string {
return func(text string) string {
t := f(text)
out := termenv.String(t)
out = out.Overline()
t = out.String()
return t
}
var colorProfile termenv.Profile

func init() {
colorProfile = termenv.ColorProfile()
}

func overline(text string) string {
out := termenv.String(text)
out = out.Overline()
return out.String()
}

func reverse(f func(text string) string) func(text string) string {
return func(text string) string {
t := f(text)
out := termenv.String(t)
out = out.Reverse()
t = out.String()
return t
}
func reverse(text string) string {
out := termenv.String(text)
out = out.Reverse()
return out.String()
}

func blink(f func(text string) string) func(text string) string {
return func(text string) string {
t := f(text)
out := termenv.String(t)
out = out.Blink()
t = out.String()
return t
}
func blink(text string) string {
out := termenv.String(text)
out = out.Blink()
return out.String()
}

func faint(f func(text string) string) func(text string) string {
return func(text string) string {
t := f(text)
out := termenv.String(t)
out = out.Faint()
t = out.String()
return t
}
func faint(text string) string {
out := termenv.String(text)
out = out.Faint()
return out.String()
}

func hexForegroundColor(cl string, f func(text string) string) func(text string) string {
return func(text string) string {
t := f(text)
out := termenv.String(t)
p := termenv.ColorProfile()
out = out.Foreground(p.Color(cl))
t = out.String()
return t
}
func hexBackgroundColorFunc(cl string, text string) string {
out := termenv.String(text)
out = out.Background(colorProfile.Color(cl))
return out.String()
}

func hexBackgroundColor(cl string, f func(text string) string) func(text string) string {
return func(text string) string {
t := f(text)
out := termenv.String(t)
p := termenv.ColorProfile()
out = out.Background(p.Color(cl))
t = out.String()
return t
}
func hexForegroundColorFunc(cl string, text string) string {
out := termenv.String(text)
out = out.Foreground(colorProfile.Color(cl))
return out.String()
}
8 changes: 1 addition & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ import (
// The first argument is the name by which this style will be used,
// and the second is the styling function itself.
func RegisterStyle(name string, fn func(string) string) {
internal.CustomMap[name] = func(f func(text string) string) func(text string) string {
return func(text string) string {
t := f(text)
t = fn(t)
return t
}
}
internal.CustomMap[name] = fn
}

// Sprint is the same as fmt.
Expand Down
2 changes: 1 addition & 1 deletion tests/style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestStyleBuilder(t *testing.T) {
}

for _, suite := range suites {
_, err := internal.StyleBuilder(suite.Value)
_, err := internal.StyleBuilder(suite.Value, "")
if err == nil && suite.Error != "" {
t.Error(cmp.Diff("", suite.Error))
continue
Expand Down

0 comments on commit 52f4054

Please sign in to comment.