From 2065fe0d4818515300dadd63bf70ee7629f8e91b Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Sun, 17 Feb 2019 14:54:28 -0500 Subject: New strings.WordWrap function Signed-off-by: Dave Henderson --- strings/strings.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'strings/strings.go') 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) +} -- cgit v1.2.3