status.go 2.0 KB

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