Skip to content

Commit

Permalink
Merge a7585cf into f8aced0
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed Apr 11, 2019
2 parents f8aced0 + a7585cf commit 1d3d295
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 135 deletions.
119 changes: 79 additions & 40 deletions common/colorable_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,101 @@ import (
"github.com/gookit/color"
)

var labelColorFunc, repoColorFunc, groupColorFunc func(r string) string
type colorSettings map[string]string
type colorFuncs map[string](func(r string) string)

var labelColor, repoColor, groupColor string
/*
Color struct shows the color settings of RRH.
*/
type Color struct {
settings colorSettings
funcs colorFuncs
}

var colorLabels = []string{
"repository", "group", "label", "boolTrue", "boolFalse", "configValue",
}

/*
ColorizedRepositoryID returns the colorized repository id string from configuration.
*/
func ColorizedRepositoryID(repoID string) string {
return repoColorFunc(repoID)
func (c *Color) ColorizedRepositoryID(repoID string) string {
return c.executeColorFunc("repository", repoID)
}

/*
ColorizedGroupName returns the colorized group name string from configuration.
*/
func ColorizedGroupName(groupName string) string {
return groupColorFunc(groupName)
func (c *Color) ColorizedGroupName(groupName string) string {
return c.executeColorFunc("group", groupName)
}

/*
ColorizedBool returns the colorized bool value from configuration.
*/
func (c *Color) ColorizedBool(value string) string {
if value == "true" {
return c.executeColorFunc("boolTrue", "true")
}
return c.executeColorFunc("boolFalse", "false")
}

/*
ColorizeConfigValue returns the coloried config value from configuration.
*/
func (c *Color) ColorizeConfigValue(value string) string {
return c.executeColorFunc("configValue", value)
}

func (c *Color) executeColorFunc(label string, value string) string {
var f, ok = c.funcs[label]
if ok {
return f(value)
}
return value
}

/*
ColorizedLabel returns the colorized label string from configuration.
*/
func ColorizedLabel(label string) string {
return labelColorFunc(label)
func (c *Color) ColorizedLabel(label string) string {
return c.executeColorFunc("label", label)
}

/*
ClearColorize clears the color settings.
*/
func ClearColorize() {
parse("")
func (c *Color) ClearColorize() {
c.parse("")
}

func parse(colorSettings string) {
func (c *Color) parse(colorSettings string) {
for _, label := range colorLabels {
c.settings[label] = ""
}
var colors = strings.Split(colorSettings, "+")
repoColor = ""
groupColor = ""
labelColor = ""
for _, c := range colors {
parseEach(c)
for _, eachColor := range colors {
c.parseEach(eachColor)
}
updateFuncs()
c.updateFuncs()
}

func parseEach(c string) {
var colors = strings.Split(c, ":")
switch colors[0] {
case "repository":
repoColor = color.ParseCodeFromAttr(strings.Replace(c, "repository:", "", -1))
case "group":
groupColor = color.ParseCodeFromAttr(strings.Replace(c, "group:", "", -1))
case "label":
labelColor = color.ParseCodeFromAttr(strings.Replace(c, "label:", "", -1))
func (c *Color) parseEach(eachColor string) {
var typeAndValue = strings.Split(eachColor, ":")
if contains(colorLabels, typeAndValue[0]) {
c.settings[typeAndValue[0]] = color.ParseCodeFromAttr(typeAndValue[1])
}
}

func updateFuncs() {
repoColorFunc = generateColorFunc(repoColor)
groupColorFunc = generateColorFunc(groupColor)
labelColorFunc = generateColorFunc(labelColor)
func (c *Color) updateFuncs() {
for _, label := range colorLabels {
var targetColor, ok = c.settings[label]
if ok {
c.funcs[label] = generateColorFunc(targetColor)
} else {
c.funcs[label] = generateColorFunc("")
}
}
}

func generateColorFunc(targetColor string) func(s string) string {
Expand All @@ -82,23 +118,26 @@ func generateColorFunc(targetColor string) func(s string) string {
/*
SetColorize sets to enable colorization.
*/
func SetColorize(enable bool) {
func (c *Color) SetColorize(enable bool) {
if !enable {
labelColor = ""
groupColor = ""
repoColor = ""
for _, label := range colorLabels {
c.funcs[label] = generateColorFunc("")
}
} else {
c.updateFuncs()
}
updateFuncs()
}

/*
InitializeColor is the initialization function of the colorized output.
The function is automatically called on loading the config file.
*/
func InitializeColor(config *Config) {
var colorSetting = config.GetValue(RrhColor)
if config.IsSet(RrhEnableColorized) && colorSetting != "" {
parse(colorSetting)
func InitializeColor(config *Config) *Color {
var color = Color{colorSettings{}, colorFuncs{}}
var settingString = config.GetValue(RrhColor)
if config.IsSet(RrhEnableColorized) && settingString != "" {
color.parse(settingString)
}
updateFuncs()
color.updateFuncs()
return &color
}
77 changes: 53 additions & 24 deletions common/colorable_output_test.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,78 @@
package common

import (
"os"
"testing"

c "github.com/gookit/color"
)

func TestEnableColorize(t *testing.T) {
os.Setenv(RrhConfigPath, "../testdata/config.json")
var config = OpenConfig()
config.Update(RrhEnableColorized, "true")
var cs = InitializeColor(config)

var boolTrue1 = cs.ColorizedBool("true")
var boolTrueWont = c.FgGreen.Render("true")
if boolTrue1 != boolTrueWont {
t.Errorf("bool true did not match: wont: %s, got: %s", boolTrue1, boolTrueWont)
}

cs.SetColorize(false)
var boolTrue2 = cs.ColorizedBool("true")
if boolTrue2 != "true" {
t.Errorf("bool true did not match: wont: %s, got: %s", boolTrue2, "true")
}

cs.SetColorize(true)
var boolTrue3 = cs.ColorizedBool("true")
if boolTrue3 != boolTrueWont {
t.Errorf("bool true did not match: wont: %s, got: %s", boolTrue3, boolTrueWont)
}
cs.ClearColorize()
}

func TestParse(t *testing.T) {
var testcases = []struct {
givenString string
repoColor string
groupColor string
labelColor string
wontRepo string
wontGroup string
wontLabel string
givenString string
repoColor string
groupColor string
labelColor string
boolTrueColor string
boolFalseColor string
wontRepo string
wontGroup string
wontLabel string
wontBoolTrue string
wontBoolFalse string
}{
{"repository:fg=white;op=bold,underscore", "37;1;4", "", "", c.Style{c.FgWhite, c.Bold, c.OpUnderscore}.Render("repository"), "groupName", "label"},
{"group: fg=red+repository:fg=white;op=bold,underscore", "37;1;4", "31", "", c.Style{c.FgWhite, c.Bold, c.OpUnderscore}.Render("repository"), c.FgRed.Render("groupName"), "label"},
{"group: fg=red+group: fg=blue+label:op=bold", "", "34", "1", "repository", c.FgBlue.Render("groupName"), c.Bold.Render("label")},
{"repository:fg=white;op=bold,underscore", "37;1;4", "", "", "", "", c.Style{c.FgWhite, c.Bold, c.OpUnderscore}.Render("repository"), "groupName", "label", "true", "false"},
{"group: fg=red+repository:fg=white;op=bold,underscore", "37;1;4", "31", "", "", "", c.Style{c.FgWhite, c.Bold, c.OpUnderscore}.Render("repository"), c.FgRed.Render("groupName"), "label", "true", "false"},
{"group: fg=red+group: fg=blue+label:op=bold", "", "34", "1", "", "", "repository", c.FgBlue.Render("groupName"), c.Bold.Render("label"), "true", "false"},
{"boolTrue: fg=green+boolFalse: fg=blue", "", "", "", "32", "34", "repository", "groupName", "label", c.FgGreen.Render("true"), c.FgBlue.Render("false")},
}

for _, tc := range testcases {
parse(tc.givenString)
if repoColor != tc.repoColor {
t.Errorf("%v: repo color did not match, wont: %s, got: %s", tc.givenString, tc.repoColor, repoColor)
}
if groupColor != tc.groupColor {
t.Errorf("%v: group color did not match, wont: %s, got: %s", tc.givenString, tc.groupColor, groupColor)
var cs = Color{settings: colorSettings{}, funcs: colorFuncs{}}
cs.parse(tc.givenString)
if v, ok := cs.settings["repository"]; !ok || v != tc.repoColor {
t.Errorf("%v: repo color did not match, wont: %s, got: %s", tc.givenString, tc.repoColor, v)
}
if groupColor != tc.groupColor {
t.Errorf("%v: group color did not match, wont: %s, got: %s", tc.givenString, tc.groupColor, groupColor)
if v, ok := cs.settings["group"]; !ok || v != tc.groupColor {
t.Errorf("%v: group color did not match, wont: %s, got: %s", tc.givenString, tc.groupColor, v)
}
if labelColor != tc.labelColor {
t.Errorf("%v: label color did not match, wont: %s, got: %s", tc.givenString, tc.labelColor, labelColor)
if v, ok := cs.settings["label"]; !ok || v != tc.labelColor {
t.Errorf("%v: label color did not match, wont: %s, got: %s", tc.givenString, tc.labelColor, v)
}
if name := ColorizedRepositoryID("repository"); name != tc.wontRepo {
if name := cs.ColorizedRepositoryID("repository"); name != tc.wontRepo {
t.Errorf("repository id did not match: wont: %s, got: %s", tc.wontRepo, name)
}
if name := ColorizedGroupName("groupName"); name != tc.wontGroup {
if name := cs.ColorizedGroupName("groupName"); name != tc.wontGroup {
t.Errorf("group name did not match: wont: %s, got: %s", tc.wontGroup, name)
}
if name := ColorizedLabel("label"); name != tc.wontLabel {
if name := cs.ColorizedLabel("label"); name != tc.wontLabel {
t.Errorf("label did not match: wont: %s, got: %s", tc.wontLabel, name)
}
ClearColorize()
}
}
Loading

0 comments on commit 1d3d295

Please sign in to comment.