-
Notifications
You must be signed in to change notification settings - Fork 303
/
edits.go
164 lines (138 loc) · 3.44 KB
/
edits.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package hud
import (
"fmt"
"path"
"github.com/tilt-dev/tilt/internal/rty"
"github.com/tilt-dev/tilt/pkg/model"
)
type EditStatusLineComponent struct {
bs buildStatus
}
var _ rty.Component = &EditStatusLineComponent{}
func NewEditStatusLine(buildStatus buildStatus) rty.Component {
return &EditStatusLineComponent{
bs: buildStatus,
}
}
func (esl *EditStatusLineComponent) Size(availWidth, availHeight int) (int, int, error) {
return availWidth, 1, nil
}
func (esl *EditStatusLineComponent) makeEditsComponents(width int) (rty.Component, rty.Component) {
var filenames, filenamesEtAl rty.Component
// fit as many filenames as possible into `width`
if len(esl.bs.edits) > 0 {
// technically this might be a multi-digit number and could lose digits as we make more filenames explicit,
// but close enough
spaceForFilenames := width - len(fmt.Sprintf(" (+%d more)", len(esl.bs.edits)))
edits := esl.bs.edits
s := ""
for len(edits) > 0 {
next := path.Base(edits[0])
if s != "" {
next = " " + next
}
if len(next) <= spaceForFilenames {
spaceForFilenames -= len(next)
s += next
edits = edits[1:]
} else {
break
}
}
filenames = rty.TextString(s)
if len(edits) > 0 {
if len(s) == 0 {
filenamesEtAl = rty.TextString(fmt.Sprintf("%d files", len(edits)))
} else {
filenamesEtAl = rty.TextString(fmt.Sprintf(" (+%d more)", len(edits)))
}
}
}
return filenames, filenamesEtAl
}
func (esl *EditStatusLineComponent) buildStatusText() rty.Component {
return buildStatusCell(esl.bs)
}
func (esl *EditStatusLineComponent) buildAgeText() rty.Component {
return deployTimeCell(esl.bs.deployTime, esl.bs.defaultTextColor())
}
func (esl *EditStatusLineComponent) rightPane() rty.Component {
l := rty.NewConcatLayout(rty.DirHor)
l.Add(esl.buildStatusText())
l.Add(middotText())
l.Add(esl.buildAgeText())
return l
}
func (esl *EditStatusLineComponent) Render(w rty.Writer, width, height int) error {
offset := 0
allocated := 0
sb := rty.NewStringBuilder()
bs := esl.bs
if len(bs.edits) == 0 {
if bs.reason.Has(model.BuildReasonFlagInit) {
sb.Fg(cLightText).Text("FIRST BUILD ")
} else if bs.reason.Has(model.BuildReasonFlagCrash) {
sb.Fg(cLightText).Text("CRASH BUILD ")
}
} else {
sb.Fg(cLightText).Text("EDITED FILES ")
}
lhs := sb.Build()
lhsW, _, err := lhs.Size(width, 1)
if err != nil {
return err
}
allocated += lhsW
rhs := esl.rightPane()
rhsW, _, err := rhs.Size(width, 1)
if err != nil {
return err
}
allocated += rhsW
filenames, filenamesEtAl := esl.makeEditsComponents(width - allocated)
var filenamesW, filenamesEtAlW int
if filenamesEtAl != nil {
filenamesEtAlW, _, err = filenamesEtAl.Size(width, 1)
if err != nil {
return err
}
allocated += filenamesEtAlW
}
if filenames != nil && allocated < width {
filenamesW, _, err = filenames.Size(width-allocated, 1)
if err != nil {
return err
}
}
{
w, err := w.Divide(0, 0, width, 1)
if err != nil {
return err
}
w.RenderChild(lhs)
offset += lhsW
}
if filenames != nil && filenamesW > 0 {
w, err := w.Divide(offset, 0, filenamesW, 1)
if err != nil {
return err
}
w.RenderChild(filenames)
offset += filenamesW
}
if filenamesEtAl != nil {
w, err := w.Divide(offset, 0, filenamesEtAlW, 1)
if err != nil {
return err
}
w.RenderChild(filenamesEtAl)
}
{
w, err := w.Divide(width-rhsW, 0, rhsW, 1)
if err != nil {
return err
}
w.RenderChild(rhs)
}
return nil
}