2
0

util.go 891 B

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