on main help sign in
refactor(tui): move UI message handling to internal/uiutil
commitdf6f35a694dd3e36ead0b985b996b45b9a68489d authorAyman Bagabas <ayman.bagabas@gmail.com> dateMon 15 Dec 2025, 18:33 (1 year ago) parent31e155c
2 files changed+72−34
internal/tui/util/util.go+14−34 internal/uiutil/uiutil.go+58
internal/tui/util/util.go+14−34
@@ -1,15 +1,11 @@
11package util
22
33import (
4− "log/slog"
5− "time"
6−
74 tea "charm.land/bubbletea/v2"
5+ "github.com/charmbracelet/crush/internal/uiutil"
86)
97
10−type Cursor interface {
11− Cursor() *tea.Cursor
12−}
8+type Cursor = uiutil.Cursor
139
1410type Model interface {
1511 Init() tea.Cmd
@@ -18,48 +14,32 @@ type Model interface {
1814}
1915
2016func CmdHandler(msg tea.Msg) tea.Cmd {
21− return func() tea.Msg {
22− return msg
23− }
17+ return uiutil.CmdHandler(msg)
2418}
2519
2620func ReportError(err error) tea.Cmd {
27− slog.Error("Error reported", "error", err)
28− return CmdHandler(InfoMsg{
29− Type: InfoTypeError,
30− Msg: err.Error(),
31− })
21+ return uiutil.ReportError(err)
3222}
3323
34−type InfoType int
24+type InfoType = uiutil.InfoType
3525
3626const (
37− InfoTypeInfo InfoType = iota
38− InfoTypeSuccess
39− InfoTypeWarn
40− InfoTypeError
41− InfoTypeUpdate
27+ InfoTypeInfo = uiutil.InfoTypeInfo
28+ InfoTypeSuccess = uiutil.InfoTypeSuccess
29+ InfoTypeWarn = uiutil.InfoTypeWarn
30+ InfoTypeError = uiutil.InfoTypeError
31+ InfoTypeUpdate = uiutil.InfoTypeUpdate
4232)
4333
4434func ReportInfo(info string) tea.Cmd {
45− return CmdHandler(InfoMsg{
46− Type: InfoTypeInfo,
47− Msg: info,
48− })
35+ return uiutil.ReportInfo(info)
4936}
5037
5138func ReportWarn(warn string) tea.Cmd {
52− return CmdHandler(InfoMsg{
53− Type: InfoTypeWarn,
54− Msg: warn,
55− })
39+ return uiutil.ReportWarn(warn)
5640}
5741
5842type (
59− InfoMsg struct {
60− Type InfoType
61− Msg string
62− TTL time.Duration
63− }
64− ClearStatusMsg struct{}
43+ InfoMsg = uiutil.InfoMsg
44+ ClearStatusMsg = uiutil.ClearStatusMsg
6545)
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+)