Skip to content

add indentnonempty function #434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,18 @@ indent 4 $lots_of_text

The above will indent every line of text by 4 space characters.

## indentnonempty

The `indentnonempty` behaves the same as `indent`, but only indents non-empty
lines (i.e., lines containing non-space characters). Empty lines are kept as-is.

```
indentnonempty 4 $lots_of_text
```

This is useful when indenting generated code as it avoids adding spaces to empty
lines.

## nindent

The `nindent` function is the same as the indent function, but prepends a new
Expand Down
31 changes: 16 additions & 15 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,22 @@ var genericMap = map[string]interface{}{
"wrap": func(l int, s string) string { return util.Wrap(s, l) },
"wrapWith": func(l int, sep, str string) string { return util.WrapCustom(str, l, sep, true) },
// Switch order so that "foobar" | contains "foo"
"contains": func(substr string, str string) bool { return strings.Contains(str, substr) },
"hasPrefix": func(substr string, str string) bool { return strings.HasPrefix(str, substr) },
"hasSuffix": func(substr string, str string) bool { return strings.HasSuffix(str, substr) },
"quote": quote,
"squote": squote,
"cat": cat,
"indent": indent,
"nindent": nindent,
"replace": replace,
"plural": plural,
"sha1sum": sha1sum,
"sha256sum": sha256sum,
"sha512sum": sha512sum,
"adler32sum": adler32sum,
"toString": strval,
"contains": func(substr string, str string) bool { return strings.Contains(str, substr) },
"hasPrefix": func(substr string, str string) bool { return strings.HasPrefix(str, substr) },
"hasSuffix": func(substr string, str string) bool { return strings.HasSuffix(str, substr) },
"quote": quote,
"squote": squote,
"cat": cat,
"indent": indent,
"indentnonempty": indentnonempty,
"nindent": nindent,
"replace": replace,
"plural": plural,
"sha1sum": sha1sum,
"sha256sum": sha256sum,
"sha512sum": sha512sum,
"adler32sum": adler32sum,
"toString": strval,

// Wrap Atoi to stop errors.
"atoi": func(a string) int { i, _ := strconv.Atoi(a); return i },
Expand Down
12 changes: 12 additions & 0 deletions strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ func indent(spaces int, v string) string {
return pad + strings.Replace(v, "\n", "\n"+pad, -1)
}

func indentnonempty(spaces int, v string) string {
pad := strings.Repeat(" ", spaces)
lines := strings.Split(v, "\n")
for i, line := range lines {
if strings.TrimSpace(line) == "" {
continue
}
lines[i] = pad + line
}
return strings.Join(lines, "\n")
}

func nindent(spaces int, v string) string {
return "\n" + indent(spaces, v)
}
Expand Down
15 changes: 15 additions & 0 deletions strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,21 @@ func TestIndent(t *testing.T) {
}
}

func TestIndentNonEmpty(t *testing.T) {
tpl := `{{indentnonempty 4 "a\nb\nc"}}`
if err := runt(tpl, " a\n b\n c"); err != nil {
t.Error(err)
}
tpl = `{{indentnonempty 4 "a\n \nb\nc"}}`
if err := runt(tpl, " a\n \n b\n c"); err != nil {
t.Error(err)
}
tpl = `{{indentnonempty 4 "a\n \nb\nc\n"}}`
if err := runt(tpl, " a\n \n b\n c\n"); err != nil {
t.Error(err)
}
}

func TestNindent(t *testing.T) {
tpl := `{{nindent 4 "a\nb\nc"}}`
if err := runt(tpl, "\n a\n b\n c"); err != nil {
Expand Down