From aa16f68620dd661e894d95f4cb959397bc36bef9 Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Fri, 27 Nov 2020 13:40:24 -0500 Subject: [PATCH] feat(display): add methods to `display` - Add `indent`, `faint`, and `underline` methods to `display` - Add tests for the above --- internal/display/faint.go | 8 ++++++++ internal/display/faint_test.go | 14 ++++++++++++++ internal/display/indent.go | 16 ++++++++++++++++ internal/display/indent_test.go | 12 ++++++++++++ internal/display/underline.go | 8 ++++++++ internal/display/underline_test.go | 14 ++++++++++++++ 6 files changed, 72 insertions(+) create mode 100644 internal/display/faint.go create mode 100644 internal/display/faint_test.go create mode 100644 internal/display/indent.go create mode 100644 internal/display/indent_test.go create mode 100644 internal/display/underline.go create mode 100644 internal/display/underline_test.go diff --git a/internal/display/faint.go b/internal/display/faint.go new file mode 100644 index 0000000..af0c040 --- /dev/null +++ b/internal/display/faint.go @@ -0,0 +1,8 @@ +package display + +import "fmt" + +// Faint returns an faint string +func Faint(str string) string { + return fmt.Sprintf(fmt.Sprintf("\033[2m%s\033[0m", str)) +} diff --git a/internal/display/faint_test.go b/internal/display/faint_test.go new file mode 100644 index 0000000..6591bb4 --- /dev/null +++ b/internal/display/faint_test.go @@ -0,0 +1,14 @@ +package display + +import ( + "testing" +) + +// TestFaint asserts that Faint applies faint formatting +func TestFaint(t *testing.T) { + want := "\033[2mfoo\033[0m" + got := Faint("foo") + if want != got { + t.Errorf("failed to faint: want: %s, got: %s", want, got) + } +} diff --git a/internal/display/indent.go b/internal/display/indent.go new file mode 100644 index 0000000..3b40556 --- /dev/null +++ b/internal/display/indent.go @@ -0,0 +1,16 @@ +package display + +import ( + "fmt" + "strings" +) + +// Indent prepends each line of a string with a tab +func Indent(str string) string { + out := "" + for _, line := range strings.Split(str, "\n") { + out += fmt.Sprintf("\t%s\n", line) + } + + return strings.TrimSuffix(out, "\n") +} diff --git a/internal/display/indent_test.go b/internal/display/indent_test.go new file mode 100644 index 0000000..be50b2d --- /dev/null +++ b/internal/display/indent_test.go @@ -0,0 +1,12 @@ +package display + +import "testing" + +// TestIndent asserts that Indent prepends a tab to each line +func TestIndent(t *testing.T) { + got := Indent("foo\nbar\nbaz") + want := "\tfoo\n\tbar\n\tbaz" + if got != want { + t.Errorf("failed to indent: want: %s, got: %s", want, got) + } +} diff --git a/internal/display/underline.go b/internal/display/underline.go new file mode 100644 index 0000000..4ac914c --- /dev/null +++ b/internal/display/underline.go @@ -0,0 +1,8 @@ +package display + +import "fmt" + +// Underline returns an underlined string +func Underline(str string) string { + return fmt.Sprintf(fmt.Sprintf("\033[4m%s\033[0m", str)) +} diff --git a/internal/display/underline_test.go b/internal/display/underline_test.go new file mode 100644 index 0000000..e9743ee --- /dev/null +++ b/internal/display/underline_test.go @@ -0,0 +1,14 @@ +package display + +import ( + "testing" +) + +// TestUnderline asserts that Underline applies underline formatting +func TestUnderline(t *testing.T) { + want := "\033[4mfoo\033[0m" + got := Underline("foo") + if want != got { + t.Errorf("failed to underline: want: %s, got: %s", want, got) + } +}