status.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package core
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. tea "github.com/charmbracelet/bubbletea"
  7. "github.com/charmbracelet/lipgloss"
  8. "github.com/kujtimiihoxha/opencode/internal/config"
  9. "github.com/kujtimiihoxha/opencode/internal/llm/models"
  10. "github.com/kujtimiihoxha/opencode/internal/lsp"
  11. "github.com/kujtimiihoxha/opencode/internal/lsp/protocol"
  12. "github.com/kujtimiihoxha/opencode/internal/tui/styles"
  13. "github.com/kujtimiihoxha/opencode/internal/tui/util"
  14. )
  15. type statusCmp struct {
  16. info util.InfoMsg
  17. width int
  18. messageTTL time.Duration
  19. lspClients map[string]*lsp.Client
  20. }
  21. // clearMessageCmd is a command that clears status messages after a timeout
  22. func (m statusCmp) clearMessageCmd(ttl time.Duration) tea.Cmd {
  23. return tea.Tick(ttl, func(time.Time) tea.Msg {
  24. return util.ClearStatusMsg{}
  25. })
  26. }
  27. func (m statusCmp) Init() tea.Cmd {
  28. return nil
  29. }
  30. func (m statusCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  31. switch msg := msg.(type) {
  32. case tea.WindowSizeMsg:
  33. m.width = msg.Width
  34. return m, nil
  35. case util.InfoMsg:
  36. m.info = msg
  37. ttl := msg.TTL
  38. if ttl == 0 {
  39. ttl = m.messageTTL
  40. }
  41. return m, m.clearMessageCmd(ttl)
  42. case util.ClearStatusMsg:
  43. m.info = util.InfoMsg{}
  44. }
  45. return m, nil
  46. }
  47. var helpWidget = styles.Padded.Background(styles.ForgroundMid).Foreground(styles.BackgroundDarker).Bold(true).Render("ctrl+? help")
  48. func (m statusCmp) View() string {
  49. status := helpWidget
  50. diagnostics := styles.Padded.Background(styles.BackgroundDarker).Render(m.projectDiagnostics())
  51. if m.info.Msg != "" {
  52. infoStyle := styles.Padded.
  53. Foreground(styles.Base).
  54. Width(m.availableFooterMsgWidth(diagnostics))
  55. switch m.info.Type {
  56. case util.InfoTypeInfo:
  57. infoStyle = infoStyle.Background(styles.BorderColor)
  58. case util.InfoTypeWarn:
  59. infoStyle = infoStyle.Background(styles.Peach)
  60. case util.InfoTypeError:
  61. infoStyle = infoStyle.Background(styles.Red)
  62. }
  63. // Truncate message if it's longer than available width
  64. msg := m.info.Msg
  65. availWidth := m.availableFooterMsgWidth(diagnostics) - 10
  66. if len(msg) > availWidth && availWidth > 0 {
  67. msg = msg[:availWidth] + "..."
  68. }
  69. status += infoStyle.Render(msg)
  70. } else {
  71. status += styles.Padded.
  72. Foreground(styles.Base).
  73. Background(styles.BackgroundDim).
  74. Width(m.availableFooterMsgWidth(diagnostics)).
  75. Render("")
  76. }
  77. status += diagnostics
  78. status += m.model()
  79. return status
  80. }
  81. func (m *statusCmp) projectDiagnostics() string {
  82. errorDiagnostics := []protocol.Diagnostic{}
  83. warnDiagnostics := []protocol.Diagnostic{}
  84. hintDiagnostics := []protocol.Diagnostic{}
  85. infoDiagnostics := []protocol.Diagnostic{}
  86. for _, client := range m.lspClients {
  87. for _, d := range client.GetDiagnostics() {
  88. for _, diag := range d {
  89. switch diag.Severity {
  90. case protocol.SeverityError:
  91. errorDiagnostics = append(errorDiagnostics, diag)
  92. case protocol.SeverityWarning:
  93. warnDiagnostics = append(warnDiagnostics, diag)
  94. case protocol.SeverityHint:
  95. hintDiagnostics = append(hintDiagnostics, diag)
  96. case protocol.SeverityInformation:
  97. infoDiagnostics = append(infoDiagnostics, diag)
  98. }
  99. }
  100. }
  101. }
  102. if len(errorDiagnostics) == 0 && len(warnDiagnostics) == 0 && len(hintDiagnostics) == 0 && len(infoDiagnostics) == 0 {
  103. return "No diagnostics"
  104. }
  105. diagnostics := []string{}
  106. if len(errorDiagnostics) > 0 {
  107. errStr := lipgloss.NewStyle().Foreground(styles.Error).Render(fmt.Sprintf("%s %d", styles.ErrorIcon, len(errorDiagnostics)))
  108. diagnostics = append(diagnostics, errStr)
  109. }
  110. if len(warnDiagnostics) > 0 {
  111. warnStr := lipgloss.NewStyle().Foreground(styles.Warning).Render(fmt.Sprintf("%s %d", styles.WarningIcon, len(warnDiagnostics)))
  112. diagnostics = append(diagnostics, warnStr)
  113. }
  114. if len(hintDiagnostics) > 0 {
  115. hintStr := lipgloss.NewStyle().Foreground(styles.Text).Render(fmt.Sprintf("%s %d", styles.HintIcon, len(hintDiagnostics)))
  116. diagnostics = append(diagnostics, hintStr)
  117. }
  118. if len(infoDiagnostics) > 0 {
  119. infoStr := lipgloss.NewStyle().Foreground(styles.Peach).Render(fmt.Sprintf("%s %d", styles.InfoIcon, len(infoDiagnostics)))
  120. diagnostics = append(diagnostics, infoStr)
  121. }
  122. return strings.Join(diagnostics, " ")
  123. }
  124. func (m statusCmp) availableFooterMsgWidth(diagnostics string) int {
  125. return max(0, m.width-lipgloss.Width(helpWidget)-lipgloss.Width(m.model())-lipgloss.Width(diagnostics))
  126. }
  127. func (m statusCmp) model() string {
  128. cfg := config.Get()
  129. coder, ok := cfg.Agents[config.AgentCoder]
  130. if !ok {
  131. return "Unknown"
  132. }
  133. model := models.SupportedModels[coder.Model]
  134. return styles.Padded.Background(styles.Grey).Foreground(styles.Text).Render(model.Name)
  135. }
  136. func NewStatusCmp(lspClients map[string]*lsp.Client) tea.Model {
  137. return &statusCmp{
  138. messageTTL: 10 * time.Second,
  139. lspClients: lspClients,
  140. }
  141. }