Skip to content

Commit e7798da

Browse files
committed
- [+] add indent & pad indent
1 parent c566955 commit e7798da

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

t_strings_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,22 @@ func TestStringManipulation(t *testing.T) {
183183
// "a PEACH",
184184
// },
185185
// == my added strings functions
186+
{
187+
`{{ indent 2 "a" }}`,
188+
"a",
189+
},
190+
{
191+
`{{ indent 2 "a\nb\nc" }}`,
192+
"a\n b\n c",
193+
},
194+
{
195+
`{{ pindent 2 "a" }}`,
196+
" a",
197+
},
198+
{
199+
`{{ pindent 2 "a\nb\nc" }}`,
200+
" a\n b\n c",
201+
},
186202
{
187203
`{{ coalesce "a" }}`,
188204
"a",

template.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ var egFuncMap = FuncMap{
178178
// == my added functions
179179
"ENV": os.Getenv,
180180
"substr": Substr,
181+
"indent": Indent,
182+
"pindent": PIndent,
181183
"coalesce": Coalesce,
182184
"quote4shell": Quote4shell,
183185

tf-strings.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ func RegexpSplit(s string, regExp string, n int) []string {
150150
////////////////////////////////////////////////////////////////////////////
151151
// Misc
152152

153+
// Indent will indent the following lines according to the specified spaces
154+
func Indent(spaces int, v string) string {
155+
pad := strings.Repeat(" ", spaces)
156+
return strings.Replace(v, "\n", "\n"+pad, -1)
157+
}
158+
159+
// PIndent will indent all lines according to the specified spaces
160+
func PIndent(spaces int, v string) string {
161+
pad := strings.Repeat(" ", spaces)
162+
return pad + Indent(spaces, v)
163+
}
164+
153165
// Coalesce function takes two or more string arguments and returns the first argument that is not empty.
154166
// The result is empty only if all the arguments are empty.
155167
func Coalesce(s ...interface{}) string {

0 commit comments

Comments
 (0)