-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
205 additions
and
4 deletions.
There are no files selected for viewing
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,124 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
"github.com/mitchellh/colorstring" | ||
) | ||
|
||
const ( | ||
defaultColorsKey = 0 | ||
defaultColorsValue = 1 | ||
prefix = "logcatf_" | ||
) | ||
|
||
var defaultColors = [][]string{ | ||
// Default foreground/background colors | ||
{"default", "39"}, | ||
{"_default_", "49"}, | ||
|
||
// Foreground colors | ||
{"black", "30"}, | ||
{"red", "31"}, | ||
{"green", "32"}, | ||
{"yellow", "33"}, | ||
{"blue", "34"}, | ||
{"magenta", "35"}, | ||
{"cyan", "36"}, | ||
{"light_gray", "37"}, | ||
{"dark_gray", "90"}, | ||
{"light_red", "91"}, | ||
{"light_green", "92"}, | ||
{"light_yellow", "93"}, | ||
{"light_blue", "94"}, | ||
{"light_magenta", "95"}, | ||
{"light_cyan", "96"}, | ||
{"white", "97"}, | ||
|
||
// Background colors | ||
{"_black_", "40"}, | ||
{"_red_", "41"}, | ||
{"_green_", "42"}, | ||
{"_yellow_", "43"}, | ||
{"_blue_", "44"}, | ||
{"_magenta_", "45"}, | ||
{"_cyan_", "46"}, | ||
{"_light_gray_", "47"}, | ||
{"_dark_gray_", "100"}, | ||
{"_light_red_", "101"}, | ||
{"_light_green_", "102"}, | ||
{"_light_yellow_", "103"}, | ||
{"_light_blue_", "104"}, | ||
{"_light_magenta_", "105"}, | ||
{"_light_cyan_", "106"}, | ||
{"_white_", "107"}, | ||
|
||
// Attributes | ||
{"bold", "1"}, | ||
{"dim", "2"}, | ||
{"underline", "4"}, | ||
{"blink_slow", "5"}, | ||
{"blink_fast", "6"}, | ||
{"invert", "7"}, | ||
{"hidden", "8"}, | ||
|
||
// Reset to reset everything to their defaults | ||
{"reset", "0"}, | ||
{"reset_bold", "21"}, | ||
} | ||
|
||
// priorityColors represents default color of priority. | ||
var priorityColors = map[string]string{ | ||
"V": "default", | ||
"D": "default", | ||
"I": "default", | ||
"W": "yellow", | ||
"E": "red", | ||
"F": "red", | ||
} | ||
|
||
// Fmtc enables to print strings colored. | ||
type Fmtc struct { | ||
context colorstring.Colorize | ||
} | ||
|
||
// Fprintln prints string according to color settings. | ||
func (f *Fmtc) Fprintln(writer io.Writer, str string, item LogcatItem) { | ||
color, ok := priorityColors[item["priority"]] | ||
if ok { | ||
str = fmt.Sprintf("[%s%s]%s", prefix, color, str) | ||
} | ||
fmt.Fprintln(writer, f.context.Color(str)) | ||
} | ||
|
||
// SetUp color settings. | ||
func (f *Fmtc) SetUp(colorEnable bool) { | ||
c := colorstring.Colorize{ | ||
Colors: f.generateColorMap(), | ||
Disable: !colorEnable, | ||
Reset: true, | ||
} | ||
f.context = c | ||
} | ||
|
||
// generate a safety color code. | ||
func (f *Fmtc) generateColorMap() map[string]string { | ||
colorMap := map[string]string{} | ||
for _, pair := range defaultColors { | ||
newKey := prefix + pair[defaultColorsKey] | ||
colorMap[newKey] = pair[defaultColorsValue] | ||
} | ||
return colorMap | ||
} | ||
|
||
// ReplaceColorCode replaces colorCode in format, to be safety. | ||
func (f *Fmtc) ReplaceColorCode(format string) string { | ||
for _, pair := range defaultColors { | ||
oldKey := pair[defaultColorsKey] | ||
newKey := prefix + pair[defaultColorsKey] | ||
format = strings.Replace(format, oldKey, newKey, flagAll) | ||
} | ||
return format | ||
} |
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,68 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestRun_ReplaceColorCode(t *testing.T) { | ||
|
||
before := "%t [blue] %m" | ||
expect := "%t [logcatf_blue] %m" | ||
|
||
f := Fmtc{} | ||
after := f.ReplaceColorCode(before) | ||
|
||
if after != expect { | ||
t.Errorf("\nexpect: %s\nresult: %s", expect, after) | ||
} | ||
} | ||
|
||
func TestRun_Fprintln_enable(t *testing.T) { | ||
|
||
format := "%t %p [blue]%m" | ||
expect := "\033[31m%t %p \033[34m%m" | ||
|
||
item := LogcatItem{ | ||
"time": "12-28 19:01:14.073", | ||
"priority": "F", | ||
"message": "logcat_message", | ||
} | ||
w := new(bytes.Buffer) | ||
|
||
f := Fmtc{} | ||
f.SetUp(true) | ||
format = f.ReplaceColorCode(format) | ||
f.Fprintln(w, format, item) | ||
|
||
res := w.String() | ||
|
||
if !strings.Contains(res, expect) { | ||
t.Errorf("\nexpect: %s\nresult: %s", expect, res) | ||
} | ||
} | ||
|
||
func TestRun_Fprintln_disable(t *testing.T) { | ||
|
||
format := "%t %p [blue]%m" | ||
expect := "%t %p %m" | ||
|
||
item := LogcatItem{ | ||
"time": "12-28 19:01:14.073", | ||
"priority": "F", | ||
"message": "logcat_message", | ||
} | ||
w := new(bytes.Buffer) | ||
|
||
f := Fmtc{} | ||
f.SetUp(false) | ||
format = f.ReplaceColorCode(format) | ||
f.Fprintln(w, format, item) | ||
|
||
res := w.String() | ||
|
||
if !strings.Contains(res, expect) { | ||
t.Errorf("\nexpect: %s\nresult: %s", expect, res) | ||
} | ||
} |
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