aboutsummaryrefslogtreecommitdiffstats
path: root/util/text.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-08-09 14:35:55 +0200
committerMichael Muré <batolettre@gmail.com>2018-08-09 14:35:55 +0200
commit0da2bea132ee57820572c2636ae52ccd39171345 (patch)
treef7f61ea876f8959d049db1fe956d2d4fea75d06e /util/text.go
parentca31258cd24d7f7951f14644fe592724579a53b8 (diff)
downloadgit-bug-0da2bea132ee57820572c2636ae52ccd39171345.tar.gz
TextWrap: add a version with a left padding
Diffstat (limited to 'util/text.go')
-rw-r--r--util/text.go14
1 files changed, 10 insertions, 4 deletions
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
}