-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathalert.go
More file actions
299 lines (244 loc) · 7.06 KB
/
alert.go
File metadata and controls
299 lines (244 loc) · 7.06 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package bubbleup
import (
"fmt"
"log"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/lucasb-eyer/go-colorful"
)
// Alert keys for the included alert types.
const (
InfoKey = "Info"
WarnKey = "Warn"
ErrorKey = "Error"
DebugKey = "Debug"
)
// Symbols used by the included alert types.
// To use the NerdFont symbols, you must be using a NerdFont,
// which can be obtained from https://www.nerdfonts.com/.
// If you want to use the default non-NerdFont symbols, pass
// false into the useNerdFont parameter when creating your alert model.
const (
InfoNerdSymbol = " "
WarnNerdSymbol = " "
ErrorNerdSymbol = " "
DebugNerdSymbol = " "
InfoASCIIPrefix = "(i)"
WarningASCIIPrefix = "(!)"
ErrorASCIIPrefix = "[!!]"
DebugASCIIPrefix = "(?)"
InfoUnicodePrefix = "\u24D8 " // Trailing space is intentional
WarningUnicodePrefix = "\u26A0"
ErrorUnicodePrefix = "\u2718"
DebugUnicodePrefix = "\u003F"
// Deprecated: use InfoASCIIPrefix instead.
InfoUniPrefix = InfoASCIIPrefix
// Deprecated: use WarningASCIIPrefix instead.
WarningUniPrefix = WarningASCIIPrefix
// Deprecated: use ErrorASCIIPrefix instead.
ErrorUniPrefix = ErrorASCIIPrefix
// Deprecated: use DebugASCIIPrefix instead.
DebugUniPrefix = DebugASCIIPrefix
)
// Defaults used by the notification rendering.
const (
DefaultLerpIncrement = 0.18
)
// Colors used by the included alert types.
const (
InfoColor = "#00FF00"
WarnColor = "#FFFF00"
ErrorColor = "#FF0000"
DebugColor = "#FF00FF"
BackColor = "#000000"
)
// Constant colors and stylings used for included alert types.
// Ignoring errors because we are using hardcoded values
var (
infoColor, _ = colorful.Hex(InfoColor)
warnColor, _ = colorful.Hex(WarnColor)
errorColor, _ = colorful.Hex(ErrorColor)
debugColor, _ = colorful.Hex(DebugColor)
backColor, _ = colorful.Hex(BackColor)
baseStyle = lipgloss.NewStyle().BorderStyle(lipgloss.RoundedBorder())
)
var parsedColors = map[string]colorful.Color{
InfoColor: infoColor,
WarnColor: warnColor,
ErrorColor: errorColor,
DebugColor: debugColor,
BackColor: backColor,
}
// alertMsg is the tea.Msg used to activate a notification
type alertMsg struct {
alertKey string
msg string
dur time.Duration
// TODO:
// animation: how the notification should appear and disappear
// style: Mimic nvim.notify's style options perhaps?
}
func (m AlertModel) newAlert(key, msg string, dur time.Duration) *alert {
if msg == "" || key == "" {
return nil
}
alertDef, ok := m.alertTypes[key]
if !ok {
return nil
}
foreColor, ok := parsedColors[alertDef.ForeColor]
if !ok {
// Can safely discard error because we validated the color
// when registering the alert defition
foreColor, _ = colorful.Hex(alertDef.ForeColor)
}
return &alert{
message: msg,
deathTime: time.Now().Add(dur),
prefix: alertDef.Prefix,
foreColor: foreColor,
style: alertDef.Style,
width: m.width,
minWidth: m.minWidth,
curLerpStep: 0.3,
position: m.position,
}
}
// alert represents an instance of an actual alert, including
// all information needed to render and destroy itself
type alert struct {
message string
deathTime time.Time
prefix string
foreColor colorful.Color
style lipgloss.Style
width int
minWidth int
curLerpStep float64
position Position
}
// render will render the given alert based on its values
// Returns the string representation of the alert, ready to be
// overlayed onto the main content.
func (n *alert) render() string {
newColor := backColor.BlendLab(n.foreColor, n.curLerpStep)
lipColor := lipgloss.Color(newColor.Hex())
// Calculate actual width based on minWidth setting
actualWidth := n.width // default to max/fixed width
if n.minWidth > 0 {
// Dynamic mode: measure message width
messageText := fmt.Sprintf("%v %v", n.prefix, n.message)
// Get the width of the message text itself
messageWidth := lipgloss.Width(messageText)
// Account for extra space needed, determined imperically
messageWidth += 3
// Clamp between min and max
if messageWidth < n.minWidth {
actualWidth = n.minWidth
} else if messageWidth > n.width {
actualWidth = n.width
} else {
actualWidth = messageWidth
}
}
newStyle := baseStyle.
Foreground(lipColor).
BorderForeground(lipColor).
Width(actualWidth).
Padding(0, 1)
// Compute width available for text inside border+padding.
textWidth := actualWidth - 2
if textWidth < 1 {
textWidth = 1
}
content := hangingWrap(n.prefix, n.message, textWidth)
return newStyle.Render(content)
}
// Region: Model stuff
// AlertDefinition is all the information needed to register a new alert type.
type AlertDefinition struct {
// (Req) Unique key used to refer to an alert type
Key string
// (Req) Hex code of the color you want your alert to be
ForeColor string
// (Opt) lipgloss.Style used to render the alert
Style lipgloss.Style
// (Opt) String used to prefix the alert message
Prefix string
// DefaultDur time.Duration
// DefaultPos
// Default
}
// NewAlertCmd will construct and return the tea.Cmd needed to trigger
// an alert. This should be called in your Update() function, and the
// returned tea.Cmd should be batched into your return.
func (m AlertModel) NewAlertCmd(alertType, message string) tea.Cmd {
return func() tea.Msg {
return alertMsg{alertKey: alertType, msg: message, dur: m.duration}
}
}
// RegisterNewAlertType will registery a new alert type based on the provided
// AlertDefintion. This can also be used to overwrite the provided defaults
// by providing an AlertDefintion with one of the default keys.
func (m AlertModel) RegisterNewAlertType(definition AlertDefinition) {
_, err := colorful.Hex(definition.ForeColor)
if err != nil {
log.Fatal(err)
return
}
if m.alertTypes == nil {
m.alertTypes = make(map[string]AlertDefinition)
}
m.alertTypes[definition.Key] = definition
}
var unicodePrefixes = map[string]string{
InfoKey: InfoUnicodePrefix,
WarnKey: WarningUnicodePrefix,
ErrorKey: ErrorUnicodePrefix,
DebugKey: DebugUnicodePrefix,
}
// Registers all the alert types that ship with BubbleUp by out of the box.
func (m AlertModel) registerDefaultAlertTypes() {
var (
infoPref string
warnPref string
errPref string
debugPref string
)
if m.useNerdFont {
infoPref = InfoNerdSymbol
warnPref = WarnNerdSymbol
errPref = ErrorNerdSymbol
debugPref = DebugNerdSymbol
} else {
infoPref = InfoASCIIPrefix
warnPref = WarningASCIIPrefix
errPref = ErrorASCIIPrefix
debugPref = DebugASCIIPrefix
}
infoDef := AlertDefinition{
Key: InfoKey,
Prefix: infoPref,
ForeColor: InfoColor,
}
m.RegisterNewAlertType(infoDef)
warnDef := AlertDefinition{
Key: WarnKey,
Prefix: warnPref,
ForeColor: WarnColor,
}
m.RegisterNewAlertType(warnDef)
errorDef := AlertDefinition{
Key: ErrorKey,
Prefix: errPref,
ForeColor: ErrorColor,
}
m.RegisterNewAlertType(errorDef)
debugDef := AlertDefinition{
Key: DebugKey,
Prefix: debugPref,
ForeColor: DebugColor,
}
m.RegisterNewAlertType(debugDef)
}