diff --git a/internal/clifmt/colors_test.go b/internal/clifmt/colors_test.go index db12e88..7c9a964 100644 --- a/internal/clifmt/colors_test.go +++ b/internal/clifmt/colors_test.go @@ -9,7 +9,9 @@ func escape(in string) string { out := make([]rune, 0) for _, char := range in { - if char == '\n' { + if char == '\\' { + out = append(out, []rune("\\\\")...) + } else if char == '\n' { out = append(out, []rune("\\n")...) } else if char == '\r' { out = append(out, []rune("\\r")...) diff --git a/internal/clifmt/symbols.go b/internal/clifmt/symbols.go index 8bd429b..4642edf 100644 --- a/internal/clifmt/symbols.go +++ b/internal/clifmt/symbols.go @@ -8,6 +8,7 @@ import ( var titleIndex = 0 var alignOffset = 40 +var defaultPrinter = fmt.Printf // Warn returns a red warning ASCII sign. If a string is given // as argument, it will print it after the warning sign @@ -32,7 +33,7 @@ func Info(s ...string) string { // Title prints a formatted title (auto-indexed from local counted) func Title(s string) { titleIndex++ - fmt.Printf("\n%s |%d| %s %s\n", Color(33, ">>", false), titleIndex, s, Color(33, "<<", false)) + defaultPrinter("\n%s |%d| %s %s\n", Color(33, ">>", false), titleIndex, s, Color(33, "<<", false)) } @@ -68,11 +69,11 @@ func Align(s string) { } // 3. print string - fmt.Printf("%s", s) + defaultPrinter("%s", s) // 4. print trailing spaces for i := size; i < offset; i++ { - fmt.Printf(" ") + defaultPrinter(" ") } } diff --git a/internal/clifmt/symbols_test.go b/internal/clifmt/symbols_test.go new file mode 100644 index 0000000..d8b14c6 --- /dev/null +++ b/internal/clifmt/symbols_test.go @@ -0,0 +1,45 @@ +package clifmt + +import ( + "fmt" + "testing" +) + +var lastPrint = "" + +func mockupPrinter(format string, args ...interface{}) (int, error) { + lastPrint = fmt.Sprintf(format, args...) + return 0, nil +} + +func TestSpecial(t *testing.T) { + defaultPrinter = mockupPrinter + + tests := []struct { + Pre func() + Processed string + Expect string + }{ + {nil, Warn(), "\033[0;31m/!\\\033[0m"}, + {nil, Warn("any text"), "\033[0;31m/!\\\033[0m any text"}, + + {nil, Info(), "\033[0;34m(!)\033[0m"}, + {nil, Info("any text"), "\033[0;34m(!)\033[0m any text"}, + + {func() { Title("any text") }, "", "\n\033[0;33m>>\033[0m |1| any text \033[0;33m<<\033[0m\n"}, + {func() { Title("any text") }, "", "\n\033[0;33m>>\033[0m |2| any text \033[0;33m<<\033[0m\n"}, + } + + for i, test := range tests { + + if test.Pre != nil { + test.Pre() + test.Processed = lastPrint + } + + if test.Processed != test.Expect { + t.Errorf("[%d] expected '%s', got '%s'", i, escape(test.Expect), escape(test.Processed)) + } + + } +}