-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotify.go
More file actions
324 lines (284 loc) · 9.35 KB
/
Copy pathnotify.go
File metadata and controls
324 lines (284 loc) · 9.35 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Notification types for bubble-overlay: Error (red), Warning (yellow), Info (cyan).
//
// Each Notification is a self-contained Bubble Tea model. Embed one inside your
// parent model, forward messages to it in Update, and call PlaceOn in View.
//
// # Dismiss modes
//
// Three modes control how a notification closes:
//
// - [DismissOnKey] — user presses a configured key (default "q").
// - [DismissAfterTimer] — closes automatically after [WithDuration]; a
// progress bar is shown; no key dismissal.
// - [DismissEither] — closes after [WithDuration] OR when the key is
// pressed; progress bar is shown.
//
// # Placement
//
// Position the notification anywhere by passing [WithPosition]:
//
// n := overlay.NewError(
// overlay.WithTitle("Connection lost"),
// overlay.WithPosition(2, 4),
// )
//
// In View(), call PlaceOn to composite the box onto your base view:
//
// func (m Model) View() string {
// base := m.renderBase()
// return m.notification.PlaceOn(base)
// }
//
// When Done returns true, PlaceOn is a no-op and View returns "".
package overlay
import (
"fmt"
"image/color"
"strings"
"time"
tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
)
// Kind identifies the semantic type and default styling of a [Notification].
type Kind uint8
const (
KindError Kind = iota // red
KindWarning // yellow
KindInfo // cyan
)
// Default Unicode icon characters prepended to the notification header.
// Override per-notification with [WithIcon].
const (
DefaultErrorIcon = "✗"
DefaultWarningIcon = "⚠"
DefaultInfoIcon = "ℹ"
)
// DismissMode controls how a [Notification] can be closed.
type DismissMode uint8
const (
// DismissOnKey closes the notification when the user presses the key set
// by [WithKey] (default "q"). No timer or progress bar is shown.
DismissOnKey DismissMode = iota
// DismissAfterTimer closes the notification automatically after the
// duration set by [WithDuration] (default 5 s). No key dismissal is
// available. A progress bar counts down the remaining time.
DismissAfterTimer
// DismissEither closes the notification after [WithDuration] OR when the
// user presses the key set by [WithKey], whichever comes first. A
// progress bar is shown.
DismissEither
)
// notifyTickMsg is the internal timer tick sent every notifyTickInterval.
type notifyTickMsg time.Time
const notifyTickInterval = 100 * time.Millisecond
// Notification is a Bubble Tea model for a dismissible, styled notification
// box. Use [NewError], [NewWarning], or [NewInfo] to create one.
//
// Notification is a value type — all Bubble Tea methods return updated copies.
type Notification struct {
kind Kind
title string
message string
icon string
dismissMode DismissMode
key string
duration time.Duration
row int
col int
width int
elapsed time.Duration
done bool
}
// Option configures a [Notification].
type Option func(*Notification)
// WithTitle sets the notification title shown next to the icon.
func WithTitle(title string) Option { return func(n *Notification) { n.title = title } }
// WithMessage sets the body text shown below the title.
func WithMessage(msg string) Option { return func(n *Notification) { n.message = msg } }
// WithKey sets the key string used to dismiss the notification (e.g. "q",
// "esc", "enter"). Defaults to "q".
func WithKey(key string) Option { return func(n *Notification) { n.key = key } }
// WithDuration sets the auto-close countdown for timer-based [DismissMode]
// values. Defaults to 5 s.
func WithDuration(d time.Duration) Option { return func(n *Notification) { n.duration = d } }
// WithDismissMode sets how the notification is closed. Defaults to
// [DismissOnKey].
func WithDismissMode(m DismissMode) Option { return func(n *Notification) { n.dismissMode = m } }
// WithPosition sets the (row, col) cell at which the notification is painted
// over the base view when [Notification.PlaceOn] is called. Both are
// 0-indexed. Defaults to (0, 0).
func WithPosition(row, col int) Option {
return func(n *Notification) { n.row = row; n.col = col }
}
// WithIcon overrides the default Unicode icon character shown before the title.
func WithIcon(icon string) Option { return func(n *Notification) { n.icon = icon } }
// WithWidth sets the inner content width of the notification box in terminal
// cells. The rendered box is slightly wider due to border and padding.
// Defaults to 40.
func WithWidth(w int) Option { return func(n *Notification) { n.width = w } }
func newNotification(kind Kind, icon string, opts []Option) Notification {
n := Notification{
kind: kind,
icon: icon,
dismissMode: DismissOnKey,
key: "q",
duration: 5 * time.Second,
width: 40,
}
for _, o := range opts {
o(&n)
}
return n
}
// NewError returns a red error notification. Default dismiss mode is
// [DismissOnKey] with key "q".
func NewError(opts ...Option) Notification {
return newNotification(KindError, DefaultErrorIcon, opts)
}
// NewWarning returns a yellow warning notification. Default dismiss mode is
// [DismissOnKey] with key "q".
func NewWarning(opts ...Option) Notification {
return newNotification(KindWarning, DefaultWarningIcon, opts)
}
// NewInfo returns a cyan informational notification. Default dismiss mode is
// [DismissOnKey] with key "q".
func NewInfo(opts ...Option) Notification {
return newNotification(KindInfo, DefaultInfoIcon, opts)
}
// Init implements [tea.Model]. It starts the internal tick timer when the
// [DismissMode] involves a countdown.
func (n Notification) Init() tea.Cmd {
if n.done {
return nil
}
if n.dismissMode == DismissAfterTimer || n.dismissMode == DismissEither {
return notifyTick()
}
return nil
}
// Update implements [tea.Model]. It handles key presses (for [DismissOnKey]
// and [DismissEither]) and timer ticks (for [DismissAfterTimer] and
// [DismissEither]).
func (n Notification) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if n.done {
return n, nil
}
switch m := msg.(type) {
case tea.KeyMsg:
if (n.dismissMode == DismissOnKey || n.dismissMode == DismissEither) && m.String() == n.key {
n.done = true
}
case notifyTickMsg:
if n.dismissMode == DismissAfterTimer || n.dismissMode == DismissEither {
n.elapsed += notifyTickInterval
if n.elapsed >= n.duration {
n.done = true
return n, nil
}
return n, notifyTick()
}
}
return n, nil
}
// Done reports whether the notification has been dismissed. When true,
// [Notification.View] returns "" and [Notification.PlaceOn] returns the base
// unchanged.
func (n Notification) Done() bool { return n.done }
// PlaceOn composites the notification box onto base at the configured
// (row, col) position using [Block]. When [Done] is true, base is returned
// unchanged.
func (n Notification) PlaceOn(base string) string {
if n.done {
return base
}
return Block(base, strings.Split(n.Render(), "\n"), n.row, n.col)
}
// View implements [tea.Model]. Returns the fully rendered, bordered
// notification box. Returns an empty view when [Done] is true.
func (n Notification) View() tea.View {
return tea.NewView(n.Render())
}
// Render returns the fully rendered, bordered notification box as a string.
// Returns "" when [Done] is true.
func (n Notification) Render() string {
if n.done {
return ""
}
color := kindColor(n.kind)
accent := lipgloss.NewStyle().Foreground(color).Bold(true)
body := lipgloss.NewStyle().Foreground(color)
header := n.icon + " " + kindLabel(n.kind)
if n.title != "" {
header += ": " + n.title
}
var parts []string
parts = append(parts, accent.Render(header))
if n.message != "" {
parts = append(parts, body.Render(n.message))
}
switch n.dismissMode {
case DismissAfterTimer, DismissEither:
barWidth := n.width / 2
if barWidth < 5 {
barWidth = 5
}
bar := notifyProgressBar(n.elapsed, n.duration, barWidth)
remaining := (n.duration - n.elapsed).Round(100 * time.Millisecond)
footer := fmt.Sprintf("%s %s", bar, remaining)
if n.dismissMode == DismissEither && n.key != "" {
footer += fmt.Sprintf(" [%s] close", n.key)
}
parts = append(parts, body.Render(footer))
case DismissOnKey:
if n.key != "" {
parts = append(parts, body.Faint(true).Render(fmt.Sprintf("[%s] close", n.key)))
}
}
return lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(color).
Padding(0, 1).
Width(n.width).
Render(strings.Join(parts, "\n"))
}
func notifyTick() tea.Cmd {
return tea.Tick(notifyTickInterval, func(t time.Time) tea.Msg {
return notifyTickMsg(t)
})
}
// notifyProgressBar renders a Unicode block progress bar of the given width
// showing elapsed/total. █ = elapsed, ░ = remaining.
func notifyProgressBar(elapsed, total time.Duration, width int) string {
if width <= 0 {
return ""
}
ratio := float64(elapsed) / float64(total)
if ratio < 0 {
ratio = 0
}
if ratio > 1 {
ratio = 1
}
filled := int(float64(width) * ratio)
return strings.Repeat("█", filled) + strings.Repeat("░", width-filled)
}
func kindColor(k Kind) color.Color {
switch k {
case KindError:
return lipgloss.Color("9") // bright red
case KindWarning:
return lipgloss.Color("11") // bright yellow
default:
return lipgloss.Color("14") // bright cyan
}
}
func kindLabel(k Kind) string {
switch k {
case KindError:
return "Error"
case KindWarning:
return "Warning"
default:
return "Info"
}
}