on main help sign in
refactor(tui): move UI message handling to internal/uiutil
df6f35aAyman Bagabas
internal/tui/util/util.go+14−34
@@ -1,15 +1,11 @@
1package util
1package util
2
2
3import (
3import (
4− "log/slog"
5− "time"
6−
7 tea "charm.land/bubbletea/v2"
4 tea "charm.land/bubbletea/v2"
5+ "github.com/charmbracelet/crush/internal/uiutil"
8)
6)
9
7
10−type Cursor interface {
8+type Cursor = uiutil.Cursor
11− Cursor() *tea.Cursor
12−}
13
9
14type Model interface {
10type Model interface {
15 Init() tea.Cmd
11 Init() tea.Cmd
@@ -18,48 +14,32 @@ type Model interface {
18}
14}
19
15
20func CmdHandler(msg tea.Msg) tea.Cmd {
16func CmdHandler(msg tea.Msg) tea.Cmd {
21− return func() tea.Msg {
17+ return uiutil.CmdHandler(msg)
22− return msg
23− }
24}
18}
25
19
26func ReportError(err error) tea.Cmd {
20func ReportError(err error) tea.Cmd {
27− slog.Error("Error reported", "error", err)
21+ return uiutil.ReportError(err)
28− return CmdHandler(InfoMsg{
29− Type: InfoTypeError,
30− Msg: err.Error(),
31− })
32}
22}
33
23
34−type InfoType int
24+type InfoType = uiutil.InfoType
35
25
36const (
26const (
37− InfoTypeInfo InfoType = iota
27+ InfoTypeInfo = uiutil.InfoTypeInfo
38− InfoTypeSuccess
28+ InfoTypeSuccess = uiutil.InfoTypeSuccess
39− InfoTypeWarn
29+ InfoTypeWarn = uiutil.InfoTypeWarn
40− InfoTypeError
30+ InfoTypeError = uiutil.InfoTypeError
41− InfoTypeUpdate
31+ InfoTypeUpdate = uiutil.InfoTypeUpdate
42)
32)
43
33
44func ReportInfo(info string) tea.Cmd {
34func ReportInfo(info string) tea.Cmd {
45− return CmdHandler(InfoMsg{
35+ return uiutil.ReportInfo(info)
46− Type: InfoTypeInfo,
47− Msg: info,
48− })
49}
36}
50
37
51func ReportWarn(warn string) tea.Cmd {
38func ReportWarn(warn string) tea.Cmd {
52− return CmdHandler(InfoMsg{
39+ return uiutil.ReportWarn(warn)
53− Type: InfoTypeWarn,
54− Msg: warn,
55− })
56}
40}
57
41
58type (
42type (
59− InfoMsg struct {
43+ InfoMsg = uiutil.InfoMsg
60− Type InfoType
61− Msg string
62− TTL time.Duration
63− }
64− ClearStatusMsg struct{}
44+ ClearStatusMsg = uiutil.ClearStatusMsg
65)
45)
internal/uiutil/uiutil.gonew file+58
@@ -0,0 +1,58 @@
1+// Package uiutil provides utility functions for UI message handling.
2+// TODO: Move to internal/ui/<appropriate_location> once the new UI migration
3+// is finalized.
4+package uiutil
5+
6+import (
7+ "log/slog"
8+ "time"
9+
10+ tea "charm.land/bubbletea/v2"
11+)
12+
13+func CmdHandler(msg tea.Msg) tea.Cmd {
14+ return func() tea.Msg {
15+ return msg
16+ }
17+}
18+
19+func ReportError(err error) tea.Cmd {
20+ slog.Error("Error reported", "error", err)
21+ return CmdHandler(InfoMsg{
22+ Type: InfoTypeError,
23+ Msg: err.Error(),
24+ })
25+}
26+
27+type InfoType int
28+
29+const (
30+ InfoTypeInfo InfoType = iota
31+ InfoTypeSuccess
32+ InfoTypeWarn
33+ InfoTypeError
34+ InfoTypeUpdate
35+)
36+
37+func ReportInfo(info string) tea.Cmd {
38+ return CmdHandler(InfoMsg{
39+ Type: InfoTypeInfo,
40+ Msg: info,
41+ })
42+}
43+
44+func ReportWarn(warn string) tea.Cmd {
45+ return CmdHandler(InfoMsg{
46+ Type: InfoTypeWarn,
47+ Msg: warn,
48+ })
49+}
50+
51+type (
52+ InfoMsg struct {
53+ Type InfoType
54+ Msg string
55+ TTL time.Duration
56+ }
57+ ClearStatusMsg struct{}
58+)