2021-05-30 07:22:04 +02:00
|
|
|
package utils
|
|
|
|
|
2021-10-24 07:26:03 +02:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2022-03-19 10:12:58 +02:00
|
|
|
"github.com/jesseduffield/generics/slices"
|
2021-10-24 07:26:03 +02:00
|
|
|
"github.com/mattn/go-runewidth"
|
2022-03-19 10:12:58 +02:00
|
|
|
"github.com/samber/lo"
|
2021-10-24 07:26:03 +02:00
|
|
|
)
|
2021-05-30 07:22:04 +02:00
|
|
|
|
|
|
|
// WithPadding pads a string as much as you want
|
|
|
|
func WithPadding(str string, padding int) string {
|
|
|
|
uncoloredStr := Decolorise(str)
|
2021-10-24 09:18:08 +02:00
|
|
|
width := runewidth.StringWidth(uncoloredStr)
|
|
|
|
if padding < width {
|
2021-05-30 07:22:04 +02:00
|
|
|
return str
|
|
|
|
}
|
2021-10-24 09:18:08 +02:00
|
|
|
return str + strings.Repeat(" ", padding-width)
|
2021-05-30 07:22:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func RenderDisplayStrings(displayStringsArr [][]string) string {
|
2021-10-31 13:29:43 +02:00
|
|
|
displayStringsArr = excludeBlankColumns(displayStringsArr)
|
2021-05-30 07:22:04 +02:00
|
|
|
padWidths := getPadWidths(displayStringsArr)
|
2021-10-31 13:29:43 +02:00
|
|
|
output := getPaddedDisplayStrings(displayStringsArr, padWidths)
|
2021-05-30 07:22:04 +02:00
|
|
|
|
2021-10-31 13:29:43 +02:00
|
|
|
return output
|
2021-05-30 07:22:04 +02:00
|
|
|
}
|
|
|
|
|
2021-10-31 13:29:43 +02:00
|
|
|
// NOTE: this mutates the input slice for the sake of performance
|
|
|
|
func excludeBlankColumns(displayStringsArr [][]string) [][]string {
|
|
|
|
if len(displayStringsArr) == 0 {
|
|
|
|
return displayStringsArr
|
|
|
|
}
|
|
|
|
|
|
|
|
// if all rows share a blank column, we want to remove that column
|
|
|
|
toRemove := []int{}
|
|
|
|
outer:
|
|
|
|
for i := range displayStringsArr[0] {
|
|
|
|
for _, strings := range displayStringsArr {
|
|
|
|
if strings[i] != "" {
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
toRemove = append(toRemove, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(toRemove) == 0 {
|
|
|
|
return displayStringsArr
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove the columns
|
|
|
|
for i, strings := range displayStringsArr {
|
|
|
|
for j := len(toRemove) - 1; j >= 0; j-- {
|
|
|
|
strings = append(strings[:toRemove[j]], strings[toRemove[j]+1:]...)
|
|
|
|
}
|
|
|
|
displayStringsArr[i] = strings
|
|
|
|
}
|
|
|
|
|
|
|
|
return displayStringsArr
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPaddedDisplayStrings(stringArrays [][]string, padWidths []int) string {
|
|
|
|
builder := strings.Builder{}
|
2021-05-30 07:22:04 +02:00
|
|
|
for i, stringArray := range stringArrays {
|
|
|
|
if len(stringArray) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for j, padWidth := range padWidths {
|
|
|
|
if len(stringArray)-1 < j {
|
|
|
|
continue
|
|
|
|
}
|
2021-10-31 13:29:43 +02:00
|
|
|
builder.WriteString(WithPadding(stringArray[j], padWidth))
|
|
|
|
builder.WriteString(" ")
|
2021-05-30 07:22:04 +02:00
|
|
|
}
|
|
|
|
if len(stringArray)-1 < len(padWidths) {
|
|
|
|
continue
|
|
|
|
}
|
2021-10-31 13:29:43 +02:00
|
|
|
builder.WriteString(stringArray[len(padWidths)])
|
|
|
|
|
|
|
|
if i < len(stringArrays)-1 {
|
|
|
|
builder.WriteString("\n")
|
|
|
|
}
|
2021-05-30 07:22:04 +02:00
|
|
|
}
|
2021-10-31 13:29:43 +02:00
|
|
|
return builder.String()
|
2021-05-30 07:22:04 +02:00
|
|
|
}
|
2021-10-24 07:26:03 +02:00
|
|
|
|
|
|
|
func getPadWidths(stringArrays [][]string) []int {
|
2022-03-19 10:12:58 +02:00
|
|
|
maxWidth := slices.MaxBy(stringArrays, func(stringArray []string) int {
|
|
|
|
return len(stringArray)
|
|
|
|
})
|
|
|
|
|
2021-10-24 07:26:03 +02:00
|
|
|
if maxWidth-1 < 0 {
|
|
|
|
return []int{}
|
|
|
|
}
|
2022-03-19 10:12:58 +02:00
|
|
|
return slices.Map(lo.Range(maxWidth-1), func(i int) int {
|
|
|
|
return slices.MaxBy(stringArrays, func(stringArray []string) int {
|
|
|
|
uncoloredStr := Decolorise(stringArray[i])
|
|
|
|
|
|
|
|
return runewidth.StringWidth(uncoloredStr)
|
|
|
|
})
|
|
|
|
})
|
2021-10-24 07:26:03 +02:00
|
|
|
}
|
2021-10-30 11:15:50 +02:00
|
|
|
|
|
|
|
// TruncateWithEllipsis returns a string, truncated to a certain length, with an ellipsis
|
|
|
|
func TruncateWithEllipsis(str string, limit int) string {
|
|
|
|
if runewidth.StringWidth(str) > limit && limit <= 3 {
|
|
|
|
return strings.Repeat(".", limit)
|
|
|
|
}
|
|
|
|
return runewidth.Truncate(str, limit, "...")
|
|
|
|
}
|
|
|
|
|
|
|
|
func SafeTruncate(str string, limit int) string {
|
|
|
|
if len(str) > limit {
|
|
|
|
return str[0:limit]
|
|
|
|
} else {
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
}
|
2022-01-19 09:32:27 +02:00
|
|
|
|
|
|
|
func ShortSha(sha string) string {
|
|
|
|
if len(sha) < 8 {
|
|
|
|
return sha
|
|
|
|
}
|
|
|
|
return sha[:8]
|
|
|
|
}
|