diff options
Diffstat (limited to 'util/left_padded.go')
-rw-r--r-- | util/left_padded.go | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/util/left_padded.go b/util/left_padded.go new file mode 100644 index 00000000..69c88074 --- /dev/null +++ b/util/left_padded.go @@ -0,0 +1,20 @@ +package util + +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 +} |