diff options
author | Michael Muré <batolettre@gmail.com> | 2018-09-11 22:04:16 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-09-11 22:14:46 +0200 |
commit | 3605887345792d2f981f971c6c4a2cb7f86a343e (patch) | |
tree | afd525b6e3a638e4c619a5a986fcb2811c297444 /util/text/left_padded.go | |
parent | 7b05983c19af4da70f2a9a5062913f4e4f5d5faa (diff) | |
download | git-bug-3605887345792d2f981f971c6c4a2cb7f86a343e.tar.gz |
reorganize package for a more idomatic go
Diffstat (limited to 'util/text/left_padded.go')
-rw-r--r-- | util/text/left_padded.go | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/util/text/left_padded.go b/util/text/left_padded.go new file mode 100644 index 00000000..a38dfba5 --- /dev/null +++ b/util/text/left_padded.go @@ -0,0 +1,20 @@ +package text + +import ( + "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 { + 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], " ") + "..." + tmpLength := utf8.RuneCountInString(tmp) + return strings.Repeat(" ", padAmount) + tmp + strings.Repeat(" ", maxValueLength-tmpLength-padAmount) + } + + return value +} |