util.go

 1package util
 2
 3import (
 4	"log/slog"
 5	"time"
 6
 7	tea "github.com/charmbracelet/bubbletea/v2"
 8)
 9
10type Cursor interface {
11	Cursor() *tea.Cursor
12}
13
14type Model interface {
15	Init() tea.Cmd
16	Update(tea.Msg) (Model, tea.Cmd)
17	View() string
18}
19
20func CmdHandler(msg tea.Msg) tea.Cmd {
21	return func() tea.Msg {
22		return msg
23	}
24}
25
26func ReportError(err error) tea.Cmd {
27	slog.Error("Error reported", "error", err)
28	return CmdHandler(InfoMsg{
29		Type: InfoTypeError,
30		Msg:  err.Error(),
31	})
32}
33
34type InfoType int
35
36const (
37	InfoTypeInfo InfoType = iota
38	InfoTypeSuccess
39	InfoTypeWarn
40	InfoTypeError
41)
42
43func ReportInfo(info string) tea.Cmd {
44	return CmdHandler(InfoMsg{
45		Type: InfoTypeInfo,
46		Msg:  info,
47	})
48}
49
50func ReportWarn(warn string) tea.Cmd {
51	return CmdHandler(InfoMsg{
52		Type: InfoTypeWarn,
53		Msg:  warn,
54	})
55}
56
57type (
58	InfoMsg struct {
59		Type InfoType
60		Msg  string
61		TTL  time.Duration
62	}
63	ClearStatusMsg struct{}
64)