From 29bb7364c8e873c72a386f4f6fa26248e5355cb2 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Mon, 30 Jul 2018 18:09:44 +0200 Subject: termui: interactive and responsible list of bugs --- util/left_padded.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 util/left_padded.go (limited to 'util/left_padded.go') 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 +} -- cgit