tprint/utils.go
2022-08-25 17:06:18 +02:00

58 lines
978 B
Go

package tprint
import (
"strings"
"unicode"
)
func maxLengthStr(a, b string) string {
if strLen(a) > strLen(b) {
return a
}
return b
}
func maxLengthSlice[T any](a, b []T) []T {
if len(a) > len(b) {
return a
}
return b
}
func strLen(str string) int {
l := 0
runes := []rune(str)
for i := 0; i < len(runes); i++ {
// skip control sequences
if unicode.IsControl(runes[i]) {
for j := i; j < len(runes)-1 && runes[j] != 'm'; j++ {
i = j
}
i++
continue
}
l++
}
return l
}
func padStringRight(str string, pad rune, length int) string {
padding := length - strLen(str)
return str + strings.Repeat(string(pad), padding)
}
func padStringLeft(str string, pad rune, length int) string {
padding := length - strLen(str)
return strings.Repeat(string(pad), padding) + str
}
func padStringCenter(str string, pad rune, length int) string {
l := strLen(str)
return padStringLeft(padStringRight(str, pad, (length-l)/2+l), pad, length)
}