diff options
author | Michael Muré <batolettre@gmail.com> | 2018-09-15 20:30:31 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-09-15 20:30:31 +0200 |
commit | bfb5e96aab9e78f05942151060cc92fdaa32bedd (patch) | |
tree | ddac4640aa1b43e13dc466cad6bbaed49f172fd9 /util/text | |
parent | 6b732d4535fc31e37485c7b496f2bbe0c854f661 (diff) | |
download | git-bug-bfb5e96aab9e78f05942151060cc92fdaa32bedd.tar.gz |
commands: git bug comment now show the comments of a bug
Diffstat (limited to 'util/text')
-rw-r--r-- | util/text/left_padded.go | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/util/text/left_padded.go b/util/text/left_padded.go index a38dfba5..78d772d5 100644 --- a/util/text/left_padded.go +++ b/util/text/left_padded.go @@ -1,20 +1,36 @@ package text import ( + "bytes" "strings" "unicode/utf8" ) -// LeftPaddedString pads a string on the left by a specified amount and pads the string on the right to fill the maxLength -func LeftPaddedString(value string, maxValueLength, padAmount int) string { +// LeftPadMaxLine pads a string on the left by a specified amount and pads the string on the right to fill the maxLength +func LeftPadMaxLine(value string, maxValueLength, leftPad int) string { valueLength := utf8.RuneCountInString(value) - if maxValueLength-padAmount >= valueLength { - return strings.Repeat(" ", padAmount) + value + strings.Repeat(" ", maxValueLength-valueLength-padAmount) - } else if maxValueLength-padAmount < valueLength { - tmp := strings.Trim(value[0:maxValueLength-padAmount-3], " ") + "..." + if maxValueLength-leftPad >= valueLength { + return strings.Repeat(" ", leftPad) + value + strings.Repeat(" ", maxValueLength-valueLength-leftPad) + } else if maxValueLength-leftPad < valueLength { + tmp := strings.Trim(value[0:maxValueLength-leftPad-3], " ") + "..." tmpLength := utf8.RuneCountInString(tmp) - return strings.Repeat(" ", padAmount) + tmp + strings.Repeat(" ", maxValueLength-tmpLength-padAmount) + return strings.Repeat(" ", leftPad) + tmp + strings.Repeat(" ", maxValueLength-tmpLength-leftPad) } return value } + +// LeftPad left pad each line of the given text +func LeftPad(text string, leftPad int) string { + var result bytes.Buffer + + pad := strings.Repeat(" ", leftPad) + + for _, line := range strings.Split(text, "\n") { + result.WriteString(pad) + result.WriteString(line) + result.WriteString("\n") + } + + return result.String() +} |