-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
screen_dump.go
81 lines (66 loc) 路 1.71 KB
/
screen_dump.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
70
71
72
73
74
75
76
77
78
79
80
81
package render
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/gdamore/tcell"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// ScreenDump renders a screendumps to screen.
type ScreenDump struct{}
// ColorerFunc colors a resource row.
func (ScreenDump) ColorerFunc() ColorerFunc {
return func(ns string, _ Header, re RowEvent) tcell.Color {
return tcell.ColorNavajoWhite
}
}
// DecoratorFunc decorates a string.
type DecoratorFunc func(string) string
// AgeDecorator represents a timestamped as human column.
var AgeDecorator = func(a string) string {
return toAgeHuman(a)
}
// Header returns a header row.
func (ScreenDump) Header(ns string) Header {
return Header{
HeaderColumn{Name: "NAME"},
HeaderColumn{Name: "DIR"},
HeaderColumn{Name: "VALID", Wide: true},
HeaderColumn{Name: "AGE", Time: true, Decorator: AgeDecorator},
}
}
// Render renders a K8s resource to screen.
func (b ScreenDump) Render(o interface{}, ns string, r *Row) error {
f, ok := o.(FileRes)
if !ok {
return fmt.Errorf("expecting screendumper, but got %T", o)
}
r.ID = filepath.Join(f.Dir, f.File.Name())
r.Fields = Fields{
f.File.Name(),
f.Dir,
"",
timeToAge(f.File.ModTime()),
}
return nil
}
// ----------------------------------------------------------------------------
// Helpers...
func timeToAge(timestamp time.Time) string {
return time.Since(timestamp).String()
}
// FileRes represents a file resource.
type FileRes struct {
File os.FileInfo
Dir string
}
// GetObjectKind returns a schema object.
func (c FileRes) GetObjectKind() schema.ObjectKind {
return nil
}
// DeepCopyObject returns a container copy.
func (c FileRes) DeepCopyObject() runtime.Object {
return c
}