-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from tamada/issue/colorable_output
Issue/colorable output
- Loading branch information
Showing
25 changed files
with
472 additions
and
244 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package common | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/gookit/color" | ||
) | ||
|
||
var repoColorFunc func(r string) string | ||
var groupColorFunc func(r string) string | ||
|
||
var repoColor = "" | ||
var groupColor = "" | ||
|
||
/* | ||
ColorizedRepositoryID returns the colorrized repository id string from configuration. | ||
*/ | ||
func ColorizedRepositoryID(repoID string) string { | ||
return repoColorFunc(repoID) | ||
} | ||
|
||
/* | ||
ColorizedGroupName returns the colorrized group name string from configuration. | ||
*/ | ||
func ColorizedGroupName(groupName string) string { | ||
return groupColorFunc(groupName) | ||
} | ||
|
||
/* | ||
ClearColorize clears the color settings. | ||
*/ | ||
func ClearColorize() { | ||
parse("") | ||
} | ||
|
||
func parse(colorSettings string) { | ||
var colors = strings.Split(colorSettings, "+") | ||
repoColor = "" | ||
groupColor = "" | ||
for _, c := range colors { | ||
parseEach(c) | ||
} | ||
updateFuncs() | ||
} | ||
|
||
func parseEach(c string) { | ||
if strings.HasPrefix(c, "repository:") { | ||
repoColor = color.ParseCodeFromAttr(strings.Replace(c, "repository:", "", -1)) | ||
} else if strings.HasPrefix(c, "group:") { | ||
groupColor = color.ParseCodeFromAttr(strings.Replace(c, "group:", "", -1)) | ||
} | ||
} | ||
|
||
func updateFuncs() { | ||
updateRepoFunc(repoColor) | ||
updateGroupFunc(groupColor) | ||
} | ||
|
||
func updateRepoFunc(repoColor string) { | ||
if repoColor != "" { | ||
var printer = color.NewPrinter(repoColor) | ||
repoColorFunc = func(r string) string { | ||
return printer.Sprint(r) | ||
} | ||
} else { | ||
repoColorFunc = func(r string) string { | ||
return r | ||
} | ||
} | ||
} | ||
|
||
func updateGroupFunc(groupColor string) { | ||
if groupColor != "" { | ||
var printer = color.NewPrinter(groupColor) | ||
groupColorFunc = func(r string) string { | ||
return printer.Sprint(r) | ||
} | ||
} else { | ||
groupColorFunc = func(r string) string { | ||
return r | ||
} | ||
} | ||
} | ||
|
||
/* | ||
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) | ||
} | ||
updateFuncs() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package common | ||
|
||
import ( | ||
"testing" | ||
|
||
c "github.com/gookit/color" | ||
) | ||
|
||
func TestParse(t *testing.T) { | ||
var testcases = []struct { | ||
givenString string | ||
repoColor string | ||
groupColor string | ||
wontRepo string | ||
wontGroup string | ||
}{ | ||
{"repository:fg=white;op=bold,underscore", "37;1;4", "", c.Style{c.FgWhite, c.Bold, c.OpUnderscore}.Render("repository"), "groupName"}, | ||
{"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")}, | ||
{"group: fg=red+group: fg=blue", "", "34", "repository", c.FgBlue.Render("groupName")}, | ||
} | ||
|
||
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) | ||
} | ||
if name := 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 { | ||
t.Errorf("group name did not match: wont: %s, got: %s", tc.wontGroup, name) | ||
} | ||
ClearColorize() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.