diff --git a/internal/tui/util/util.go b/internal/tui/util/util.go index ca5c20a837caffbbb257f0527a3b7802c1cf580a..5df57c11cc4491b25a048c7437057408f1e9c30f 100644 --- a/internal/tui/util/util.go +++ b/internal/tui/util/util.go @@ -1,15 +1,11 @@ package util import ( - "log/slog" - "time" - tea "charm.land/bubbletea/v2" + "github.com/charmbracelet/crush/internal/uiutil" ) -type Cursor interface { - Cursor() *tea.Cursor -} +type Cursor = uiutil.Cursor type Model interface { Init() tea.Cmd @@ -18,48 +14,32 @@ type Model interface { } func CmdHandler(msg tea.Msg) tea.Cmd { - return func() tea.Msg { - return msg - } + return uiutil.CmdHandler(msg) } func ReportError(err error) tea.Cmd { - slog.Error("Error reported", "error", err) - return CmdHandler(InfoMsg{ - Type: InfoTypeError, - Msg: err.Error(), - }) + return uiutil.ReportError(err) } -type InfoType int +type InfoType = uiutil.InfoType const ( - InfoTypeInfo InfoType = iota - InfoTypeSuccess - InfoTypeWarn - InfoTypeError - InfoTypeUpdate + InfoTypeInfo = uiutil.InfoTypeInfo + InfoTypeSuccess = uiutil.InfoTypeSuccess + InfoTypeWarn = uiutil.InfoTypeWarn + InfoTypeError = uiutil.InfoTypeError + InfoTypeUpdate = uiutil.InfoTypeUpdate ) func ReportInfo(info string) tea.Cmd { - return CmdHandler(InfoMsg{ - Type: InfoTypeInfo, - Msg: info, - }) + return uiutil.ReportInfo(info) } func ReportWarn(warn string) tea.Cmd { - return CmdHandler(InfoMsg{ - Type: InfoTypeWarn, - Msg: warn, - }) + return uiutil.ReportWarn(warn) } type ( - InfoMsg struct { - Type InfoType - Msg string - TTL time.Duration - } - ClearStatusMsg struct{} + InfoMsg = uiutil.InfoMsg + ClearStatusMsg = uiutil.ClearStatusMsg ) diff --git a/internal/uiutil/uiutil.go b/internal/uiutil/uiutil.go new file mode 100644 index 0000000000000000000000000000000000000000..0031a30519201c261c0cad8af637cbead7962524 --- /dev/null +++ b/internal/uiutil/uiutil.go @@ -0,0 +1,58 @@ +// Package uiutil provides utility functions for UI message handling. +// TODO: Move to internal/ui/ once the new UI migration +// is finalized. +package uiutil + +import ( + "log/slog" + "time" + + tea "charm.land/bubbletea/v2" +) + +func CmdHandler(msg tea.Msg) tea.Cmd { + return func() tea.Msg { + return msg + } +} + +func ReportError(err error) tea.Cmd { + slog.Error("Error reported", "error", err) + return CmdHandler(InfoMsg{ + Type: InfoTypeError, + Msg: err.Error(), + }) +} + +type InfoType int + +const ( + InfoTypeInfo InfoType = iota + InfoTypeSuccess + InfoTypeWarn + InfoTypeError + InfoTypeUpdate +) + +func ReportInfo(info string) tea.Cmd { + return CmdHandler(InfoMsg{ + Type: InfoTypeInfo, + Msg: info, + }) +} + +func ReportWarn(warn string) tea.Cmd { + return CmdHandler(InfoMsg{ + Type: InfoTypeWarn, + Msg: warn, + }) +} + +type ( + InfoMsg struct { + Type InfoType + Msg string + TTL time.Duration + } + ClearStatusMsg struct{} +)