-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwidget.go
244 lines (198 loc) · 5.8 KB
/
widget.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package skeleton
import (
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"strings"
)
// widget is a helper for rendering the widget of the terminal.
type widget struct {
termReady bool
viewport *viewport.Model
// widgets are hold the widgets
widgets []*commonWidget
// properties are hold the properties of the widget
properties *widgetProperties
// widgetLength is hold the length of the widget
widgetLength int
updater *Updater
}
// newWidget returns a new Widget.
func newWidget() *widget {
return &widget{
properties: defaultWidgetProperties(),
viewport: newTerminalViewport(),
updater: NewUpdater(),
}
}
type commonWidget struct {
Key string // Key is the name of the Value
Value string // Value is the content of the Value
}
type widgetProperties struct {
borderColor string
leftTabPadding int
rightTabPadding int
widgetStyle lipgloss.Style
}
func defaultWidgetProperties() *widgetProperties {
borderColor := "39"
leftPadding := 2
rightPadding := 2
return &widgetProperties{
borderColor: borderColor,
leftTabPadding: leftPadding,
rightTabPadding: rightPadding,
widgetStyle: func() lipgloss.Style {
b := lipgloss.RoundedBorder()
b.Right = "├"
b.Left = "┤"
return lipgloss.NewStyle().BorderStyle(b).
PaddingLeft(leftPadding).PaddingRight(rightPadding).
BorderForeground(lipgloss.Color("49"))
}(),
}
}
// SetBorderColor sets the border color of the Widget.
func (w *widget) SetBorderColor(color string) *widget {
w.properties.borderColor = color
return w
}
// GetBorderColor returns the border color of the Widget.
func (w *widget) GetBorderColor() string {
return w.properties.borderColor
}
// SetWidgetBorderColor sets the border color of the Widget.
func (w *widget) SetWidgetBorderColor(color string) *widget {
w.properties.widgetStyle = w.properties.widgetStyle.BorderForeground(lipgloss.Color(color))
return w
}
// GetWidgetBorderColor returns the border color of the Widget.
func (w *widget) GetWidgetBorderColor() string {
return w.properties.widgetStyle.BorderForeground().String()
}
// SetLeftPadding sets the left padding of the Widget.
func (w *widget) SetLeftPadding(padding int) *widget {
w.properties.leftTabPadding = padding
w.properties.widgetStyle = w.properties.widgetStyle.PaddingLeft(padding)
return w
}
// SetRightPadding sets the right padding of the Widget.
func (w *widget) SetRightPadding(padding int) *widget {
w.properties.rightTabPadding = padding
w.properties.widgetStyle = w.properties.widgetStyle.PaddingRight(padding)
return w
}
// GetWidget returns the Value by the given key.
func (w *widget) GetWidget(key string) *commonWidget {
for _, widget := range w.widgets {
if widget.Key == key {
return widget
}
}
return nil
}
// DeleteAllWidgets deletes all the widgets.
func (w *widget) DeleteAllWidgets() {
w.widgets = nil
w.calculateWidgetLength()
w.updater.Update()
}
func (w *widget) addNewWidget(key, value string) {
// skip if key already exists
if w.GetWidget(key) != nil {
return
}
w.widgets = append(w.widgets, &commonWidget{
Key: key,
Value: value,
})
w.calculateWidgetLength()
w.updater.Update()
}
func (w *widget) updateWidgetContent(key, value string) {
x := w.GetWidget(key)
if x != nil {
x.Value = value
}
w.calculateWidgetLength()
w.updater.Update()
}
func (w *widget) deleteWidget(key string) {
for i, widget := range w.widgets {
if widget.Key == key {
w.widgets = append(w.widgets[:i], w.widgets[i+1:]...)
break
}
}
w.calculateWidgetLength()
w.updater.Update()
}
type WidgetSizeMsg struct {
NotEnoughToHandleWidgets bool
}
func (w *widget) Init() tea.Cmd {
return nil
}
func (w *widget) Update(msg tea.Msg) (*widget, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
if !w.termReady {
if msg.Width > 0 && msg.Height > 0 {
w.termReady = true
}
}
w.viewport.Width = msg.Width
w.viewport.Height = msg.Height
cmds = append(cmds, w.calculateWidgetLength())
}
return w, tea.Batch(cmds...)
}
// calculateWidgetLength calculates the length of the widgets.
func (w *widget) calculateWidgetLength() tea.Cmd {
var widgetLen int
for _, widget := range w.widgets {
widgetLen += len([]rune(widget.Value))
widgetLen += w.properties.leftTabPadding + w.properties.rightTabPadding
widgetLen += 2 // for the border between widgets
}
requiredLineCount := w.viewport.Width - (widgetLen + 2)
w.widgetLength = widgetLen
if requiredLineCount < 0 {
return func() tea.Msg {
return WidgetSizeMsg{NotEnoughToHandleWidgets: false}
}
} else {
return func() tea.Msg {
return WidgetSizeMsg{NotEnoughToHandleWidgets: true}
}
}
}
func (w *widget) View() string {
if !w.termReady {
return "setting up terminal..."
}
requiredLineCount := w.viewport.Width - (w.widgetLength + 2)
if requiredLineCount < 0 {
return ""
}
line := strings.Repeat("─", requiredLineCount)
line = lipgloss.NewStyle().Foreground(lipgloss.Color(w.properties.borderColor)).Render(line)
var renderedWidgets = make([]string, len(w.widgets))
for i, wgt := range w.widgets {
renderedWidgets[i] = w.properties.widgetStyle.Render(wgt.Value)
}
leftCorner := lipgloss.JoinVertical(lipgloss.Top, "│", "╰")
rightCorner := lipgloss.JoinVertical(lipgloss.Top, "│", "╯")
leftCorner = lipgloss.NewStyle().Foreground(lipgloss.Color(w.properties.borderColor)).Render(leftCorner)
rightCorner = lipgloss.NewStyle().Foreground(lipgloss.Color(w.properties.borderColor)).Render(rightCorner)
var bottom []string
bottom = append(bottom, line)
bottom = append(bottom, renderedWidgets...)
position := lipgloss.Center
if len(w.widgets) > 0 {
position = lipgloss.Top
}
return lipgloss.JoinHorizontal(position, leftCorner, lipgloss.JoinHorizontal(lipgloss.Center, bottom...), rightCorner)
}