-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
padding.go
69 lines (59 loc) 路 1.37 KB
/
padding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package ui
import (
"strings"
"time"
"unicode"
"github.com/derailed/k9s/internal/render"
"k8s.io/apimachinery/pkg/util/duration"
)
// MaxyPad tracks uniform column padding.
type MaxyPad []int
// ComputeMaxColumns figures out column max size and necessary padding.
func ComputeMaxColumns(pads MaxyPad, sortColName string, header render.Header, ee render.RowEvents) {
const colPadding = 1
for index, h := range header {
pads[index] = len(h.Name)
if h.Name == sortColName {
pads[index] = len(h.Name) + 2
}
}
var row int
for _, e := range ee {
for index, field := range e.Row.Fields {
if header.IsAgeCol(index) {
field = toAgeHuman(field)
}
width := len(field) + colPadding
if index < len(pads) && width > pads[index] {
pads[index] = width
}
}
row++
}
}
// IsASCII checks if table cell has all ascii characters.
func IsASCII(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] > unicode.MaxASCII {
return false
}
}
return true
}
// Pad a string up to the given length or truncates if greater than length.
func Pad(s string, width int) string {
if len(s) == width {
return s
}
if len(s) > width {
return render.Truncate(s, width)
}
return s + strings.Repeat(" ", width-len(s))
}
func toAgeHuman(s string) string {
d, err := time.ParseDuration(s)
if err != nil {
return render.NAValue
}
return duration.HumanDuration(d)
}