From 0da2bea132ee57820572c2636ae52ccd39171345 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Thu, 9 Aug 2018 14:35:55 +0200 Subject: TextWrap: add a version with a left padding --- util/text.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'util/text.go') diff --git a/util/text.go b/util/text.go index 8f762c08..cc652cc6 100644 --- a/util/text.go +++ b/util/text.go @@ -29,10 +29,15 @@ func WordWrap(text string, lineWidth int) (string, int) { } func TextWrap(text string, lineWidth int) (string, int) { + return TextWrapPadded(text, lineWidth, 0) +} + +func TextWrapPadded(text string, lineWidth int, leftPad int) (string, int) { var textBuffer bytes.Buffer var lineBuffer bytes.Buffer nbLine := 1 firstLine := true + pad := strings.Repeat(" ", leftPad) // tabs are formatted as 4 spaces text = strings.Replace(text, "\t", " ", 4) @@ -40,7 +45,7 @@ func TextWrap(text string, lineWidth int) (string, int) { re := regexp.MustCompile(`(\x1b\[\d+m)?([^\x1b]*)(\x1b\[\d+m)?`) for _, line := range strings.Split(text, "\n") { - spaceLeft := lineWidth + spaceLeft := lineWidth - leftPad if !firstLine { textBuffer.WriteString("\n") @@ -78,6 +83,7 @@ func TextWrap(text string, lineWidth int) (string, int) { word = word[l:] lineBuffer.WriteString(part) + textBuffer.WriteString(pad) textBuffer.Write(lineBuffer.Bytes()) lineBuffer.Reset() @@ -86,10 +92,10 @@ func TextWrap(text string, lineWidth int) (string, int) { nbLine++ } - spaceLeft = lineWidth + spaceLeft = lineWidth - leftPad } } else { - textBuffer.WriteString(strings.TrimRight(lineBuffer.String(), " ")) + textBuffer.WriteString(pad + strings.TrimRight(lineBuffer.String(), " ")) textBuffer.WriteString("\n") lineBuffer.Reset() lineBuffer.WriteString(prefix + word + suffix) @@ -99,7 +105,7 @@ func TextWrap(text string, lineWidth int) (string, int) { } } } - textBuffer.WriteString(strings.TrimRight(lineBuffer.String(), " ")) + textBuffer.WriteString(pad + strings.TrimRight(lineBuffer.String(), " ")) lineBuffer.Reset() firstLine = false } -- cgit