mirror of
https://github.com/cheat/cheat.git
synced 2024-12-22 04:35:06 +01:00
feat(display): add methods to display
- Add `indent`, `faint`, and `underline` methods to `display` - Add tests for the above
This commit is contained in:
parent
367673d5d9
commit
aa16f68620
8
internal/display/faint.go
Normal file
8
internal/display/faint.go
Normal file
@ -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))
|
||||
}
|
14
internal/display/faint_test.go
Normal file
14
internal/display/faint_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
16
internal/display/indent.go
Normal file
16
internal/display/indent.go
Normal file
@ -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")
|
||||
}
|
12
internal/display/indent_test.go
Normal file
12
internal/display/indent_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
8
internal/display/underline.go
Normal file
8
internal/display/underline.go
Normal file
@ -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))
|
||||
}
|
14
internal/display/underline_test.go
Normal file
14
internal/display/underline_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user