-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert.go
227 lines (185 loc) · 5.29 KB
/
alert.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
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 = " "
InfoUniPrefix = "(i)"
WarningUniPrefix = "(!)"
ErrorUniPrefix = "[!!]"
DebugUniPrefix = "(?)"
)
// 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"
)
// Constant colors and stylings used for included alert types.
var (
infoColor, _ = colorful.Hex("#00FF00")
warnColor, _ = colorful.Hex("#FFFF00")
errorColor, _ = colorful.Hex("#FF0000")
debugColor, _ = colorful.Hex("#FF00FF")
backColor, _ = colorful.Hex("#000000")
baseStyle = lipgloss.NewStyle().BorderStyle(lipgloss.RoundedBorder())
)
// 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
// location: where on the screen it should appear
// style: Mimic nvim.notify's style options perhaps?
}
func (m AlertModel) newNotif(key, msg string, dur time.Duration) *alert {
if msg == "" || key == "" {
return nil
}
alertDef, ok := m.alertTypes[key]
if !ok {
return nil
}
// Can safely discard error because we validated the color
// when registering the alert defition
color, _ := colorful.Hex(alertDef.ForeColor)
return &alert{
message: msg,
deathTime: time.Now().Add(dur),
prefix: alertDef.Prefix,
foreColor: color,
style: alertDef.Style,
width: m.width,
curLerpStep: 0.3,
}
}
// 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
curLerpStep float64
// animation
// location
}
// 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())
newStyle := baseStyle.Foreground(lipColor).BorderForeground(lipColor).Width(n.width)
return newStyle.Render(fmt.Sprintf("%v %v", n.prefix, n.message))
}
// 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: time.Second * 2}
}
}
// 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
}
// 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 = InfoUniPrefix
warnPref = WarningUniPrefix
errPref = ErrorUniPrefix
debugPref = DebugUniPrefix
}
infoDef := AlertDefinition{
Key: "Info",
Prefix: infoPref,
ForeColor: InfoColor,
}
m.RegisterNewAlertType(infoDef)
warnDef := AlertDefinition{
Key: "Warn",
Prefix: warnPref,
ForeColor: WarnColor,
}
m.RegisterNewAlertType(warnDef)
errorDef := AlertDefinition{
Key: "Error",
Prefix: errPref,
ForeColor: ErrorColor,
}
m.RegisterNewAlertType(errorDef)
debugDef := AlertDefinition{
Key: "Debug",
Prefix: debugPref,
ForeColor: DebugColor,
}
m.RegisterNewAlertType(debugDef)
}