From 26e4f7f11a8c10c48af66ce4f2d4c50d5a2dc760 Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Sun, 24 Nov 2024 18:19:04 -0500 Subject: feat: Notice messages when performing actions (#11) * Update releaser CI/CD version * feat: Notice messages when performing actions * Showcase notices in demo.gif --- internal/notices/notices.go | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 internal/notices/notices.go (limited to 'internal/notices/notices.go') 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) + } +} -- cgit v1.2.3