summaryrefslogtreecommitdiff
path: root/strings/strings.go
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2019-02-17 14:54:28 -0500
committerDave Henderson <dhenderson@gmail.com>2019-02-17 16:20:41 -0500
commit2065fe0d4818515300dadd63bf70ee7629f8e91b (patch)
tree09d55085da86d2e535662c3746b55c31985a4f2f /strings/strings.go
parenta9b3f9390642d3f361f64528073e3199a116222a (diff)
New strings.WordWrap function
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'strings/strings.go')
-rw-r--r--strings/strings.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/strings/strings.go b/strings/strings.go
index 8c10865a..c9b029fc 100644
--- a/strings/strings.go
+++ b/strings/strings.go
@@ -4,6 +4,8 @@ import (
"regexp"
"sort"
"strings"
+
+ "github.com/Masterminds/goutils"
)
// Indent - indent each line of the string with the given indent string
@@ -81,3 +83,30 @@ func CamelCase(in string) string {
s = strings.Replace(s, string(s[0]), string(in[0]), 1)
return nonAlphaNum.ReplaceAllString(s, "")
}
+
+// WordWrapOpts defines the options to apply to the WordWrap function
+type WordWrapOpts struct {
+ // The desired maximum line length in characters (defaults to 80)
+ Width uint
+
+ // Line-break sequence to insert (defaults to "\n")
+ LBSeq string
+}
+
+// applies default options
+func wwDefaults(opts WordWrapOpts) WordWrapOpts {
+ if opts.Width == 0 {
+ opts.Width = 80
+ }
+ if opts.LBSeq == "" {
+ opts.LBSeq = "\n"
+ }
+ return opts
+}
+
+// WordWrap - insert line-breaks into the string, before it reaches the given
+// width.
+func WordWrap(in string, opts WordWrapOpts) string {
+ opts = wwDefaults(opts)
+ return goutils.WrapCustom(in, int(opts.Width), opts.LBSeq, false)
+}