summaryrefslogtreecommitdiff
path: root/utilities
diff options
context:
space:
mode:
Diffstat (limited to 'utilities')
-rw-r--r--utilities/utilities.go15
-rw-r--r--utilities/utilities_test.go20
2 files changed, 35 insertions, 0 deletions
diff --git a/utilities/utilities.go b/utilities/utilities.go
index b976f77..a1ec805 100644
--- a/utilities/utilities.go
+++ b/utilities/utilities.go
@@ -220,3 +220,18 @@ type Pair[T1, T2 any] struct {
func PerSecondToInterval(rate int64) time.Duration {
return time.Duration(time.Second.Nanoseconds() / rate)
}
+
+func IndentOutput(output string, depth uint, character string) string {
+ finalNewline := false
+ if strings.LastIndex(output, "\n") == len(output)-1 {
+ finalNewline = true
+ output = strings.TrimSuffix(output, "\n")
+ }
+ indentedOutput := strings.Join(Fmap[string](strings.SplitAfter(output, "\n"), func(line string) string {
+ return strings.Repeat(character, int(depth)) + line
+ }), "")
+ if finalNewline {
+ indentedOutput += "\n"
+ }
+ return indentedOutput
+}
diff --git a/utilities/utilities_test.go b/utilities/utilities_test.go
index 7f3d83a..8f43c6d 100644
--- a/utilities/utilities_test.go
+++ b/utilities/utilities_test.go
@@ -188,3 +188,23 @@ func TestTrimmedMean(t *testing.T) {
t.Fatalf("The trimmed mean result %v does not match the expected value %v", result, expected)
}
}
+
+func TestIndentStringOneNewline(t *testing.T) {
+ output := "This is my output\n"
+
+ indendentedOutput := IndentOutput(output, 3, "--")
+
+ if indendentedOutput != "------This is my output\n" {
+ t.Fatalf("I expected the indented output to be ####%v#### but got ####%v####!", output, indendentedOutput)
+ }
+}
+
+func TestIndentStringMultipleNewlines(t *testing.T) {
+ output := "This is my output\n\n"
+
+ indendentedOutput := IndentOutput(output, 3, "--")
+
+ if indendentedOutput != "------This is my output\n------\n" {
+ t.Fatalf("I expected the indented output to be ####%v#### but got ####%v####!", output, indendentedOutput)
+ }
+}