-
Notifications
You must be signed in to change notification settings - Fork 4
/
statusbar.go
264 lines (217 loc) · 6.36 KB
/
statusbar.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"strconv"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"time"
)
const (
textShiftLimit = 5
statusInfo int = iota
statusWarning
statusError
)
var (
statusProtoHttp2, statusProtoHttps, statusProtoInsecure, statusDefaultIndEmoji string
statusBarStyle, statusNugget, statusBadge, statusBadgeError, statusBadgeOk, statusBadgeWarning,
reqCountStyle, resTimeStyle, statusText, statusTextInfo, statusTextError,
statusTextWarning, indicatorStyle lipgloss.Style
)
// A status bar state.
type StatusBar struct {
status int
text string
textOffset int
screenWidth int
reqScheme string
reqCount int
resTime time.Duration
resStatusCode int
resProto string
resProtoMajor int
}
type StatusBarTickMsg time.Time
func StatusBarDoTick() tea.Cmd {
return tea.Tick(2*time.Second, func(t time.Time) tea.Msg {
return StatusBarTickMsg(t)
})
}
func (s StatusBar) Update(msg tea.Msg) (StatusBar, tea.Cmd) {
switch msg.(type) {
case StatusBarTickMsg:
s.applyTimeChanges()
return s, StatusBarDoTick()
}
return s, nil
}
// Apply time changes.
func (s *StatusBar) applyTimeChanges() {
s.textOffset += textShiftLimit
}
// Get part of creeping text.
func (s *StatusBar) getText(w int) string {
tw := len(s.text)
if tw < w {
return s.text
}
if s.textOffset < tw && s.text[s.textOffset] > 127 { // unicode detected
s.textOffset += 4 // do not break unicode sequence, skip it
}
if s.textOffset >= tw {
s.textOffset = 0
return s.text[:w]
}
tl := s.textOffset + w
if tl >= tw {
appendix := w - (tw - s.textOffset)
if s.text[appendix] > 127 { // unicode detected
appendix-- // do not break unicode sequence, skip it
}
return s.text[s.textOffset:] + " " + s.text[:appendix]
}
if s.text[tl-1] > 127 { // unicode detected
tl-- // do not break unicode sequence, skip it
}
return s.text[s.textOffset:tl]
}
// Get status message.
func (s *StatusBar) getStatusText(w int) string {
var style lipgloss.Style
// apply style
switch s.status {
case statusInfo:
style = statusTextInfo
case statusWarning:
style = statusTextWarning
case statusError:
style = statusTextError
}
return style.Render(s.getText(w))
}
// Set status.
func (s *StatusBar) setStatus(status int, text string) {
s.textOffset = 0
s.status = status
s.text = time.Now().Format("[15:04:05] ") + text
}
// Info message.
func (s *StatusBar) Info(text string) {
s.setStatus(statusInfo, text)
}
// Warning message.
func (s *StatusBar) Warning(text string) {
s.setStatus(statusWarning, text)
}
// Error message.
func (s *StatusBar) Error(text string) {
s.setStatus(statusError, text)
}
// Increment count of requests.
func (s *StatusBar) IncReqCount() {
s.reqCount++
}
// Get count of requests.
func (s *StatusBar) GetReqCount() int {
return s.reqCount
}
// Get last response time.
func (s *StatusBar) GetResTime() string {
return s.resTime.String()
}
// Set last response time.
func (s *StatusBar) SetResTime(t time.Duration) {
s.resTime = t
}
// Set last response status code.
func (s *StatusBar) SetResStatusCode(c int) {
s.resStatusCode = c
}
// Set last response proto.
func (s *StatusBar) SetResProto(major int, proto, scheme string) {
s.resProtoMajor = major
s.resProto = proto
s.reqScheme = scheme
}
// Set count of requests.
func (s *StatusBar) SetReqCount(c int) {
s.reqCount = c
}
// Set screen width.
func (s *StatusBar) SetScreenWidth(w int) {
s.screenWidth = w
}
// Get status badge.
func (s *StatusBar) getStatusBadge(text string) (badge string) {
var style lipgloss.Style
switch s.status {
case statusInfo:
style = statusBadgeOk
case statusWarning:
style = statusBadgeWarning
case statusError:
style = statusBadgeError
default:
style = statusBadge
}
return style.Render(text)
}
// Get status logo indicator.
func (s *StatusBar) protoIndicator() (ind string) {
switch s.resProtoMajor {
case 1:
if s.reqScheme == "https" {
ind = statusProtoHttps + s.resProto
} else {
ind = statusProtoInsecure + s.resProto
}
case 2:
ind = statusProtoHttp2 + s.resProto
default:
ind = statusDefaultIndEmoji + "rHttp"
}
return
}
// Format status bar.
func (s *StatusBar) FormatStatusBar() string {
w := lipgloss.Width
status := s.getStatusBadge("STATUS")
reqCounter := reqCountStyle.Render(strconv.Itoa(s.reqCount))
resTime := resTimeStyle.Render(s.GetResTime())
proto := indicatorStyle.Render(s.protoIndicator())
maxTextWidth := screenWidth - w(status) - w(reqCounter) - w(resTime) - w(proto)
statusVal := statusText.Copy().Width(maxTextWidth).Render(s.getStatusText(maxTextWidth))
bar := lipgloss.JoinHorizontal(lipgloss.Top, status, statusVal, reqCounter, resTime, proto)
return statusBarStyle.Width(screenWidth).Render(bar)
}
func NewStatusBar(conf *Config) StatusBar {
statusProtoHttp2 = conf.Emoji("statusbarProtoHttp2")
statusProtoHttps = conf.Emoji("statusbarProtoHttps")
statusDefaultIndEmoji = conf.Emoji("statusbarDefaultIndicator")
statusProtoInsecure = conf.Emoji("statusbarProtoInsecure")
statusBarStyle = lipgloss.NewStyle().
Foreground(conf.Color("statusbarFg")).
Background(conf.Color("statusbarBg"))
statusNugget = lipgloss.NewStyle().Foreground(conf.Color("statusbarNugget")).Padding(0, 1)
statusBadge = lipgloss.NewStyle().Inherit(statusBarStyle).
Background(conf.Color("statusbarBadgeBg")).
Foreground(conf.Color("statusbarBadgeFg")).
Padding(0, 1).MarginRight(1)
statusBadgeError = lipgloss.NewStyle().Inherit(statusBadge).
Background(conf.Color("statusbarBadgeError")).Padding(0, 1)
statusBadgeOk = lipgloss.NewStyle().Inherit(statusBadge).
Background(conf.Color("statusbarBadgeOk")).Padding(0, 1)
statusBadgeWarning = lipgloss.NewStyle().Inherit(statusBadge).
Background(conf.Color("statusbarBadgeWarning")).Padding(0, 1)
reqCountStyle = statusNugget.Copy().
Background(conf.Color("statusbarReqCount")).Align(lipgloss.Right)
resTimeStyle = statusNugget.Copy().
Background(conf.Color("statusbarResTime")).Align(lipgloss.Right)
statusText = lipgloss.NewStyle().Inherit(statusBarStyle)
statusTextInfo = lipgloss.NewStyle().Inherit(statusText)
statusTextError = lipgloss.NewStyle().Inherit(statusText).
Foreground(conf.Color("statusbarTextError"))
statusTextWarning = lipgloss.NewStyle().Inherit(statusText).
Foreground(conf.Color("statusbarTextWarning"))
indicatorStyle = statusNugget.Copy().Background(conf.Color("statusbarIndicator"))
return StatusBar{}
}