-
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #166 from hairyhenderson/enhance-indent-165
Enhancing indent function
- Loading branch information
Showing
11 changed files
with
157 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package strings | ||
|
||
import "strings" | ||
|
||
// Indent - indent each line of the string with the given indent string | ||
func Indent(width int, indent, s string) string { | ||
if width > 1 { | ||
indent = strings.Repeat(indent, width) | ||
} | ||
var res []byte | ||
bol := true | ||
for i := 0; i < len(s); i++ { | ||
c := s[i] | ||
if bol && c != '\n' { | ||
res = append(res, indent...) | ||
} | ||
res = append(res, c) | ||
bol = c == '\n' | ||
} | ||
return string(res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package strings | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIndent(t *testing.T) { | ||
actual := "hello\nworld\n!" | ||
expected := " hello\n world\n !" | ||
assert.Equal(t, expected, Indent(1, " ", actual)) | ||
assert.Equal(t, "\n", Indent(1, " ", "\n")) | ||
assert.Equal(t, " foo\n", Indent(1, " ", "foo\n")) | ||
assert.Equal(t, " foo", Indent(1, " ", "foo")) | ||
assert.Equal(t, " foo", Indent(3, " ", "foo")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env bats | ||
|
||
load helper | ||
|
||
tmpdir=$(mktemp -u) | ||
|
||
function setup () { | ||
mkdir -p $tmpdir | ||
} | ||
|
||
function teardown () { | ||
rm -rf $tmpdir || true | ||
} | ||
|
||
@test "'strings.Indent'" { | ||
gomplate -i '{{ strings.Indent " " "hello world" }} | ||
{{ "hello\nmultiline\nworld" | indent 2 "-" }} | ||
{{ "foo\nbar" | strings.Indent 2 }} | ||
{{"hello\nworld" | strings.Indent 5 | strings.TrimSpace }} | ||
' | ||
[ "$status" -eq 0 ] | ||
[[ "${output}" == " hello world | ||
--hello | ||
--multiline | ||
--world | ||
foo | ||
bar | ||
hello | ||
world" ]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters