util.go

 1package util
 2
 3import tea "github.com/charmbracelet/bubbletea"
 4
 5func CmdHandler(msg tea.Msg) tea.Cmd {
 6	return func() tea.Msg {
 7		return msg
 8	}
 9}
10
11func ReportError(err error) tea.Cmd {
12	return CmdHandler(InfoMsg{
13		Type: InfoTypeError,
14		Msg:  err.Error(),
15	})
16}
17
18type InfoType int
19
20const (
21	InfoTypeInfo InfoType = iota
22	InfoTypeWarn
23	InfoTypeError
24)
25
26func ReportInfo(info string) tea.Cmd {
27	return CmdHandler(InfoMsg{
28		Type: InfoTypeInfo,
29		Msg:  info,
30	})
31}
32
33func ReportWarn(warn string) tea.Cmd {
34	return CmdHandler(InfoMsg{
35		Type: InfoTypeWarn,
36		Msg:  warn,
37	})
38}
39
40type (
41	InfoMsg struct {
42		Type InfoType
43		Msg  string
44	}
45	ClearStatusMsg struct{}
46)
47
48func Clamp(v, low, high int) int {
49	if high < low {
50		low, high = high, low
51	}
52	return min(high, max(low, v))
53}