blob: 40b5d1f6bd14556a9235211ea2d26a4902d1fdb1 (
plain)
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
|
package notices
import (
"log/slog"
"strings"
"time"
"github.com/ChausseBenjamin/termpicker/internal/util"
tea "github.com/charmbracelet/bubbletea"
"github.com/hashicorp/go-uuid"
)
const (
expiryDelay = 3 // seconds
)
type NoticeExpiryMsg string
type Model struct {
// Notices is a map of UUIDs pointing to a messages
Notices map[string]string
}
func (m Model) Init() tea.Cmd { return nil }
func (m Model) View() string {
noticeStr := ""
for _, v := range m.Notices {
noticeStr += v + "\n"
}
return strings.TrimRight(noticeStr, "\n")
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case NoticeExpiryMsg:
delete(m.Notices, string(msg))
return m, nil
}
return m, nil
}
func New() Model {
return Model{
Notices: make(map[string]string),
}
}
func (m Model) New(msg string) tea.Cmd {
uuid, err := uuid.GenerateUUID()
if err != nil {
slog.Error("Failed to generate UUID", util.ErrKey, err)
}
m.Notices[uuid] = msg
return func() tea.Msg {
time.Sleep(expiryDelay * time.Second)
return NoticeExpiryMsg(uuid)
}
}
func (m Model) Reset(uuid string) tea.Cmd {
return func() tea.Msg {
time.Sleep(expiryDelay * time.Second)
return NoticeExpiryMsg(uuid)
}
}
|