status.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package core
  2. import (
  3. "time"
  4. tea "github.com/charmbracelet/bubbletea"
  5. "github.com/charmbracelet/lipgloss"
  6. "github.com/kujtimiihoxha/termai/internal/config"
  7. "github.com/kujtimiihoxha/termai/internal/llm/models"
  8. "github.com/kujtimiihoxha/termai/internal/tui/styles"
  9. "github.com/kujtimiihoxha/termai/internal/tui/util"
  10. "github.com/kujtimiihoxha/termai/internal/version"
  11. )
  12. type statusCmp struct {
  13. err error
  14. info string
  15. width int
  16. messageTTL time.Duration
  17. }
  18. // clearMessageCmd is a command that clears status messages after a timeout
  19. func (m statusCmp) clearMessageCmd() tea.Cmd {
  20. return tea.Tick(m.messageTTL, func(time.Time) tea.Msg {
  21. return util.ClearStatusMsg{}
  22. })
  23. }
  24. func (m statusCmp) Init() tea.Cmd {
  25. return nil
  26. }
  27. func (m statusCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  28. switch msg := msg.(type) {
  29. case tea.WindowSizeMsg:
  30. m.width = msg.Width
  31. case util.ErrorMsg:
  32. m.err = msg
  33. m.info = ""
  34. return m, m.clearMessageCmd()
  35. case util.InfoMsg:
  36. m.info = string(msg)
  37. m.err = nil
  38. return m, m.clearMessageCmd()
  39. case util.ClearStatusMsg:
  40. m.info = ""
  41. m.err = nil
  42. }
  43. return m, nil
  44. }
  45. var (
  46. versionWidget = styles.Padded.Background(styles.DarkGrey).Foreground(styles.Text).Render(version.Version)
  47. helpWidget = styles.Padded.Background(styles.Grey).Foreground(styles.Text).Render("? help")
  48. )
  49. func (m statusCmp) View() string {
  50. status := styles.Padded.Background(styles.Grey).Foreground(styles.Text).Render("? help")
  51. if m.err != nil {
  52. status += styles.Regular.Padding(0, 1).
  53. Background(styles.Red).
  54. Foreground(styles.Text).
  55. Width(m.availableFooterMsgWidth()).
  56. Render(m.err.Error())
  57. } else if m.info != "" {
  58. status += styles.Padded.
  59. Foreground(styles.Base).
  60. Background(styles.Green).
  61. Width(m.availableFooterMsgWidth()).
  62. Render(m.info)
  63. } else {
  64. status += styles.Padded.
  65. Foreground(styles.Base).
  66. Background(styles.LightGrey).
  67. Width(m.availableFooterMsgWidth()).
  68. Render(m.info)
  69. }
  70. status += m.model()
  71. status += versionWidget
  72. return status
  73. }
  74. func (m statusCmp) availableFooterMsgWidth() int {
  75. // -2 to accommodate padding
  76. return max(0, m.width-lipgloss.Width(helpWidget)-lipgloss.Width(versionWidget)-lipgloss.Width(m.model()))
  77. }
  78. func (m statusCmp) model() string {
  79. model := models.SupportedModels[config.Get().Model.Coder]
  80. return styles.Padded.Background(styles.Grey).Foreground(styles.Text).Render(model.Name)
  81. }
  82. func NewStatusCmp() tea.Model {
  83. return &statusCmp{
  84. messageTTL: 5 * time.Second,
  85. }
  86. }