summaryrefslogtreecommitdiffstats
path: root/pkg/utils/lines.go
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2021-05-30 15:22:04 +1000
committerJesse Duffield <jessedduffield@gmail.com>2021-06-02 20:33:52 +1000
commit258eedb38c6e8692565d74a92e3590da477dea6b (patch)
treedb0a544b281184b04c704182e5a81f11172ee80e /pkg/utils/lines.go
parentbc044c64b2315c3fb8c523fa0ae7ef71bcba3b31 (diff)
refactor
Diffstat (limited to 'pkg/utils/lines.go')
-rw-r--r--pkg/utils/lines.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/pkg/utils/lines.go b/pkg/utils/lines.go
new file mode 100644
index 000000000..4c654d888
--- /dev/null
+++ b/pkg/utils/lines.go
@@ -0,0 +1,34 @@
+package utils
+
+import "strings"
+
+// SplitLines takes a multiline string and splits it on newlines
+// currently we are also stripping \r's which may have adverse effects for
+// windows users (but no issues have been raised yet)
+func SplitLines(multilineString string) []string {
+ multilineString = strings.Replace(multilineString, "\r", "", -1)
+ if multilineString == "" || multilineString == "\n" {
+ return make([]string, 0)
+ }
+ lines := strings.Split(multilineString, "\n")
+ if lines[len(lines)-1] == "" {
+ return lines[:len(lines)-1]
+ }
+ return lines
+}
+
+// TrimTrailingNewline - Trims the trailing newline
+// TODO: replace with `chomp` after refactor
+func TrimTrailingNewline(str string) string {
+ if strings.HasSuffix(str, "\n") {
+ return str[:len(str)-1]
+ }
+ return str
+}
+
+// NormalizeLinefeeds - Removes all Windows and Mac style line feeds
+func NormalizeLinefeeds(str string) string {
+ str = strings.Replace(str, "\r\n", "\n", -1)
+ str = strings.Replace(str, "\r", "", -1)
+ return str
+}