tools: introduce tools.Indent() to indent strings
This commit is contained in:
parent
35f6635492
commit
75fde77dda
@ -100,3 +100,15 @@ func Longest(strs []string) string {
|
||||
|
||||
return longest
|
||||
}
|
||||
|
||||
// Indent returns a string which prepends "\t" TAB characters to the beginning
|
||||
// of each line in the given string "str".
|
||||
func Indent(str string) string {
|
||||
indented := strings.Replace(str, "\n", "\n\t", -1)
|
||||
if len(indented) > 0 {
|
||||
indented = "\t" + indented
|
||||
}
|
||||
|
||||
return indented
|
||||
}
|
||||
|
||||
|
@ -112,3 +112,21 @@ func TestLjustLeftJustifiesString(t *testing.T) {
|
||||
|
||||
assert.Equal(t, expected, Ljust(unjust))
|
||||
}
|
||||
|
||||
func TestIndentIndentsStrings(t *testing.T) {
|
||||
assert.Equal(t, "\tfoo\n\tbar", Indent("foo\nbar"))
|
||||
}
|
||||
|
||||
func TestIndentIndentsSingleLineStrings(t *testing.T) {
|
||||
assert.Equal(t, "\tfoo", Indent("foo"))
|
||||
}
|
||||
|
||||
func TestIndentReturnsEmptyStrings(t *testing.T) {
|
||||
assert.Equal(t, "", Indent(""))
|
||||
}
|
||||
|
||||
func TestUndentRemovesLeadingWhitespace(t *testing.T) {
|
||||
assert.Equal(t, "foo", Undent("\t\t\tfoo"))
|
||||
assert.Equal(t, "foo", Undent("foo"))
|
||||
assert.Equal(t, "foo", Undent(" foo"))
|
||||
}
|
||||
|
1
tools/text.go
Normal file
1
tools/text.go
Normal file
@ -0,0 +1 @@
|
||||
package tools
|
1
tools/text_test.go
Normal file
1
tools/text_test.go
Normal file
@ -0,0 +1 @@
|
||||
package tools_test
|
Loading…
Reference in New Issue
Block a user