aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/MichaelMure/go-term-text/left_pad.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2019-11-03 13:09:55 +0000
committerGitHub <noreply@github.com>2019-11-03 13:09:55 +0000
commit8c7c9880b1adcf876ad63ea39e46e62bd7ebde5d (patch)
treef9c6369a6593e3c00cfb3c0b45009dfb6d055d7b /vendor/github.com/MichaelMure/go-term-text/left_pad.go
parentf5193cc76dd1a1c1bb55194a64ef90bae2278115 (diff)
parent912b5ca320891f2a4f1a88f1a137ce8ee46a1a03 (diff)
downloadgit-bug-8c7c9880b1adcf876ad63ea39e46e62bd7ebde5d.tar.gz
Merge pull request #228 from ludovicm67/patch-cli-label-colors
Display label colors in termui
Diffstat (limited to 'vendor/github.com/MichaelMure/go-term-text/left_pad.go')
-rw-r--r--vendor/github.com/MichaelMure/go-term-text/left_pad.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/vendor/github.com/MichaelMure/go-term-text/left_pad.go b/vendor/github.com/MichaelMure/go-term-text/left_pad.go
new file mode 100644
index 00000000..a63fedb9
--- /dev/null
+++ b/vendor/github.com/MichaelMure/go-term-text/left_pad.go
@@ -0,0 +1,50 @@
+package text
+
+import (
+ "bytes"
+ "strings"
+
+ "github.com/mattn/go-runewidth"
+)
+
+// LeftPadMaxLine pads a line on the left by a specified amount and pads the
+// string on the right to fill the maxLength.
+// If the given string is too long, it is truncated with an ellipsis.
+// Handle properly terminal color escape code
+func LeftPadMaxLine(line string, length, leftPad int) string {
+ cleaned, escapes := ExtractTermEscapes(line)
+
+ scrWidth := runewidth.StringWidth(cleaned)
+ // truncate and ellipse if needed
+ if scrWidth+leftPad > length {
+ cleaned = runewidth.Truncate(cleaned, length-leftPad, "…")
+ } else if scrWidth+leftPad < length {
+ cleaned = runewidth.FillRight(cleaned, length-leftPad)
+ }
+
+ rightPart := ApplyTermEscapes(cleaned, escapes)
+ pad := strings.Repeat(" ", leftPad)
+
+ return pad + rightPart
+}
+
+// LeftPad left pad each line of the given text
+func LeftPadLines(text string, leftPad int) string {
+ var result bytes.Buffer
+
+ pad := strings.Repeat(" ", leftPad)
+
+ lines := strings.Split(text, "\n")
+
+ for i, line := range lines {
+ result.WriteString(pad)
+ result.WriteString(line)
+
+ // no additional line break at the end
+ if i < len(lines)-1 {
+ result.WriteString("\n")
+ }
+ }
+
+ return result.String()
+}