diff options
author | Benjamin Chausse <benjamin@chausse.xyz> | 2024-11-24 18:19:04 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-24 18:19:04 -0500 |
commit | 26e4f7f11a8c10c48af66ce4f2d4c50d5a2dc760 (patch) | |
tree | fd72a6c25b97333487aafcd8c86374d709050840 /internal/notices/notices.go | |
parent | 0a8ed6b216ab58bf0ae0695a89f602e477c9573c (diff) |
feat: Notice messages when performing actions (#11)
* Update releaser CI/CD version
* feat: Notice messages when performing actions
* Showcase notices in demo.gif
Diffstat (limited to 'internal/notices/notices.go')
-rw-r--r-- | internal/notices/notices.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/internal/notices/notices.go b/internal/notices/notices.go new file mode 100644 index 0000000..49fb179 --- /dev/null +++ b/internal/notices/notices.go @@ -0,0 +1,60 @@ +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 = 1 // 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) + } +} |