uiutil.go

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