diff --git a/pkg/cprint/color.go b/pkg/cprint/color.go deleted file mode 100644 index e5c9d8d..0000000 --- a/pkg/cprint/color.go +++ /dev/null @@ -1,77 +0,0 @@ -package cprint - -import ( - "sync" - - "github.com/fatih/color" -) - -var ( - // mu is used to synchronize writes from multiple goroutines. - mu sync.Mutex - // DisableOutput disables all output. - DisableOutput bool -) - -func conditionalPrintf(fn func(string, ...interface{}), format string, a ...interface{}) { - if DisableOutput { - return - } - mu.Lock() - defer mu.Unlock() - fn(format, a...) -} - -func conditionalPrintln(fn func(...interface{}), a ...interface{}) { - if DisableOutput { - return - } - mu.Lock() - defer mu.Unlock() - fn(a...) -} - -var ( - createPrintf = color.New(color.FgGreen).PrintfFunc() - deletePrintf = color.New(color.FgRed).PrintfFunc() - updatePrintf = color.New(color.FgYellow).PrintfFunc() - - // CreatePrintf is fmt.Printf with red as foreground color. - CreatePrintf = func(format string, a ...interface{}) { - conditionalPrintf(createPrintf, format, a...) - } - - // DeletePrintf is fmt.Printf with green as foreground color. - DeletePrintf = func(format string, a ...interface{}) { - conditionalPrintf(deletePrintf, format, a...) - } - - // UpdatePrintf is fmt.Printf with yellow as foreground color. - UpdatePrintf = func(format string, a ...interface{}) { - conditionalPrintf(updatePrintf, format, a...) - } - - createPrintln = color.New(color.FgGreen).PrintlnFunc() - deletePrintln = color.New(color.FgRed).PrintlnFunc() - updatePrintln = color.New(color.FgYellow).PrintlnFunc() - bluePrintln = color.New(color.BgBlue).PrintlnFunc() - - // CreatePrintln is fmt.Println with red as foreground color. - CreatePrintln = func(a ...interface{}) { - conditionalPrintln(createPrintln, a...) - } - - // DeletePrintln is fmt.Println with green as foreground color. - DeletePrintln = func(a ...interface{}) { - conditionalPrintln(deletePrintln, a...) - } - - // UpdatePrintln is fmt.Println with yellow as foreground color. - UpdatePrintln = func(a ...interface{}) { - conditionalPrintln(updatePrintln, a...) - } - - BluePrintLn = func(a ...interface{}) { - conditionalPrintln(bluePrintln, a...) - } -) diff --git a/pkg/cprint/color_test.go b/pkg/cprint/color_test.go deleted file mode 100644 index cbec6c2..0000000 --- a/pkg/cprint/color_test.go +++ /dev/null @@ -1,95 +0,0 @@ -package cprint - -import ( - "bytes" - "os" - "testing" - - "github.com/fatih/color" - "github.com/stretchr/testify/assert" -) - -// captureOutput captures color.Output and returns the recorded output as -// f runs. -// It is not thread-safe. -func captureOutput(f func()) string { - backupOutput := color.Output - defer func() { - color.Output = backupOutput - }() - var out bytes.Buffer - color.Output = &out - f() - return out.String() -} - -func TestMain(m *testing.M) { - backup := color.NoColor - color.NoColor = false - exitVal := m.Run() - color.NoColor = backup - os.Exit(exitVal) -} - -func TestPrint(t *testing.T) { - tests := []struct { - name string - DisableOutput bool - Run func() - Expected string - }{ - { - name: "println prints colored output", - DisableOutput: false, - Run: func() { - CreatePrintln("foo") - UpdatePrintln("bar") - DeletePrintln("fubaz") - }, - Expected: "\x1b[32mfoo\n\x1b[0m\x1b[33mbar\n\x1b[0m\x1b[31mfubaz\n\x1b[0m", - }, - { - name: "println doesn't output anything when disabled", - DisableOutput: true, - Run: func() { - CreatePrintln("foo") - UpdatePrintln("bar") - DeletePrintln("fubaz") - }, - Expected: "", - }, - { - name: "printf prints colored output", - DisableOutput: false, - Run: func() { - CreatePrintf("%s", "foo") - UpdatePrintf("%s", "bar") - DeletePrintf("%s", "fubaz") - }, - Expected: "\x1b[32mfoo\x1b[0m\x1b[33mbar\x1b[0m\x1b[31mfubaz\x1b[0m", - }, - { - name: "printf doesn't output anything when disabled", - DisableOutput: true, - Run: func() { - CreatePrintln("foo") - UpdatePrintln("bar") - DeletePrintln("fubaz") - }, - Expected: "", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - DisableOutput = tt.DisableOutput - defer func() { - DisableOutput = false - }() - - output := captureOutput(func() { - tt.Run() - }) - assert.Equal(t, tt.Expected, output) - }) - } -}