-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatus.go
More file actions
221 lines (195 loc) · 5.62 KB
/
Copy pathstatus.go
File metadata and controls
221 lines (195 loc) · 5.62 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package wui
import "strings"
// StatusItem is one segment of the status line.
type StatusItem struct {
// Text is the segment's content.
Text string
// Style overrides the bar's default styling for this segment.
Style Style
// Right pins the segment to the right-hand end of the bar. Items
// keep their declared order within each side.
Right bool
}
// Status is the content of the bottom status line: a list of segments
// plus the separator drawn between them.
//
// wui fills in defaults an app rarely wants to write itself — the web
// URL when WithWebServer is active, and the key map's short help when
// WithKeyMap is set — so a status line is usually one option, not a
// layout problem:
//
// wui.NewProgram(m,
// wui.WithKeyMap(keys),
// wui.WithStatus(func(m wui.Model) wui.Status {
// return wui.NewStatus().Left(m.(model).filename).Right("ln 4")
// }),
// )
type Status struct {
Items []StatusItem
// Separator is drawn between adjacent segments on the same side.
// Empty means the default (" │ ").
Separator string
// Style overrides the bar's default appearance (reverse video in
// the TUI, a bordered strip in HTML).
Style Style
// Hidden suppresses the bar entirely for this frame.
Hidden bool
}
// NewStatus returns an empty Status, as the head of a builder chain.
func NewStatus() Status { return Status{} }
// Left appends left-aligned segments.
func (s Status) Left(texts ...string) Status {
for _, t := range texts {
s.Items = append(s.Items, StatusItem{Text: t})
}
return s
}
// Right appends right-aligned segments.
func (s Status) Right(texts ...string) Status {
for _, t := range texts {
s.Items = append(s.Items, StatusItem{Text: t, Right: true})
}
return s
}
// Item appends a fully specified segment.
func (s Status) Item(it StatusItem) Status {
s.Items = append(s.Items, it)
return s
}
// Styled appends a left-aligned segment with its own style.
func (s Status) Styled(text string, style Style) Status {
s.Items = append(s.Items, StatusItem{Text: text, Style: style})
return s
}
// WithSeparator sets the text drawn between adjacent segments.
func (s Status) WithSeparator(sep string) Status {
s.Separator = sep
return s
}
// WithStyle overrides the bar's default appearance.
func (s Status) WithStyle(style Style) Status {
s.Style = style
return s
}
// Hide suppresses the bar for this frame.
func (s Status) Hide() Status {
s.Hidden = true
return s
}
// Empty reports whether the bar has nothing to draw.
func (s Status) Empty() bool { return len(s.Items) == 0 }
// sides splits the items into left- and right-aligned groups, keeping
// declaration order within each.
func (s Status) sides() (left, right []StatusItem) {
for _, it := range s.Items {
if it.Right {
right = append(right, it)
} else {
left = append(left, it)
}
}
return left, right
}
func (s Status) separator() string {
if s.Separator == "" {
return " │ "
}
return s.Separator
}
// StatusFunc builds the status line for the current model state. It is
// called on every frame, after Update.
type StatusFunc func(Model) Status
// defaultStatus is the Status wui shows when the app supplies no
// StatusFunc: the web URL (when serving) on the left and the key map's
// short help on the right.
func (p *Program) defaultStatus() Status {
var s Status
if url := p.webStatusURL(); url != "" {
s = s.Left("web ⇒ " + url)
}
if help := p.cfg.keyMap.ShortHelp(); help != "" {
s = s.Right(help)
}
return s
}
// buildStatus resolves the status line for this frame: the app's
// StatusFunc when set, else the default.
//
// The web link is prepended to whatever the app returns rather than
// replaced by it — an app adding its own segments should never
// silently lose the link to its own web build. An app that wants the
// bar entirely to itself can return NewStatus().Hide() and draw its
// own, or pass WithoutStatusBar.
func (p *Program) buildStatus() Status {
if p.cfg.status == nil {
return p.defaultStatus()
}
s := p.cfg.status(p.model)
if s.Hidden {
return s
}
if s.Empty() {
return p.defaultStatus()
}
if url := p.webStatusURL(); url != "" {
link := StatusItem{Text: "web ⇒ " + url}
s.Items = append([]StatusItem{link}, s.Items...)
}
return s
}
// plainStatusLine renders the bar's segments as a single line fitted to
// width, with left- and right-aligned groups pushed apart. It is shared
// by both renderers so the two platforms agree on ordering and
// separators; each then applies its own visual treatment.
func plainStatusLine(s Status, width int) string {
left, right := s.sides()
sep := s.separator()
join := func(items []StatusItem) string {
parts := make([]string, 0, len(items))
for _, it := range items {
if it.Text != "" {
parts = append(parts, it.Text)
}
}
return strings.Join(parts, sep)
}
l, r := join(left), join(right)
if width <= 0 {
if r == "" {
return l
}
if l == "" {
return r
}
return l + sep + r
}
// Reserve a leading and trailing space so text never touches the
// screen edge.
inner := width - 2
if inner < 1 {
inner = 1
}
lw, rw := runeLen(l), runeLen(r)
if lw+rw+1 > inner {
// Not enough room for both: the right side is supplementary
// (help text), so drop it before truncating the left.
if lw > inner {
l = truncate(l, inner)
}
return " " + l + " "
}
pad := inner - lw - rw
return " " + l + strings.Repeat(" ", pad) + r + " "
}
func runeLen(s string) int { return len([]rune(s)) }
// truncate shortens s to n runes, marking the cut with an ellipsis.
func truncate(s string, n int) string {
r := []rune(s)
if len(r) <= n {
return s
}
if n <= 1 {
return string(r[:n])
}
return string(r[:n-1]) + "…"
}