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
13func CmdHandler(msg tea.Msg) tea.Cmd {
14	return func() tea.Msg {
15		return msg
16	}
17}
18
19func 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
27type InfoType int
28
29const (
30	InfoTypeInfo InfoType = iota
31	InfoTypeSuccess
32	InfoTypeWarn
33	InfoTypeError
34	InfoTypeUpdate
35)
36
37func ReportInfo(info string) tea.Cmd {
38	return CmdHandler(InfoMsg{
39		Type: InfoTypeInfo,
40		Msg:  info,
41	})
42}
43
44func ReportWarn(warn string) tea.Cmd {
45	return CmdHandler(InfoMsg{
46		Type: InfoTypeWarn,
47		Msg:  warn,
48	})
49}
50
51type (
52	InfoMsg struct {
53		Type InfoType
54		Msg  string
55		TTL  time.Duration
56	}
57	ClearStatusMsg struct{}
58)