header.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package header
  2. import (
  3. "fmt"
  4. "strings"
  5. tea "github.com/charmbracelet/bubbletea/v2"
  6. "github.com/charmbracelet/crush/internal/config"
  7. "github.com/charmbracelet/crush/internal/fsext"
  8. "github.com/charmbracelet/crush/internal/lsp"
  9. "github.com/charmbracelet/crush/internal/lsp/protocol"
  10. "github.com/charmbracelet/crush/internal/pubsub"
  11. "github.com/charmbracelet/crush/internal/session"
  12. "github.com/charmbracelet/crush/internal/tui/styles"
  13. "github.com/charmbracelet/crush/internal/tui/util"
  14. "github.com/charmbracelet/lipgloss/v2"
  15. )
  16. type Header interface {
  17. util.Model
  18. SetSession(session session.Session) tea.Cmd
  19. SetWidth(width int) tea.Cmd
  20. SetDetailsOpen(open bool)
  21. ShowingDetails() bool
  22. }
  23. type header struct {
  24. width int
  25. session session.Session
  26. lspClients map[string]*lsp.Client
  27. detailsOpen bool
  28. }
  29. func New(lspClients map[string]*lsp.Client) Header {
  30. return &header{
  31. lspClients: lspClients,
  32. width: 0,
  33. }
  34. }
  35. func (h *header) Init() tea.Cmd {
  36. return nil
  37. }
  38. func (p *header) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  39. switch msg := msg.(type) {
  40. case pubsub.Event[session.Session]:
  41. if msg.Type == pubsub.UpdatedEvent {
  42. if p.session.ID == msg.Payload.ID {
  43. p.session = msg.Payload
  44. }
  45. }
  46. }
  47. return p, nil
  48. }
  49. func (p *header) View() string {
  50. if p.session.ID == "" {
  51. return ""
  52. }
  53. t := styles.CurrentTheme()
  54. details := p.details()
  55. parts := []string{
  56. t.S().Base.Foreground(t.Secondary).Render("Charm™"),
  57. " ",
  58. styles.ApplyBoldForegroundGrad("CRUSH", t.Secondary, t.Primary),
  59. " ",
  60. }
  61. remainingWidth := p.width - lipgloss.Width(strings.Join(parts, "")) - lipgloss.Width(details) - 2
  62. if remainingWidth > 0 {
  63. char := "╱"
  64. lines := strings.Repeat(char, remainingWidth)
  65. parts = append(parts, t.S().Base.Foreground(t.Primary).Render(lines), " ")
  66. }
  67. parts = append(parts, details)
  68. content := t.S().Base.Padding(0, 1).Render(
  69. lipgloss.JoinHorizontal(
  70. lipgloss.Left,
  71. parts...,
  72. ),
  73. )
  74. return content
  75. }
  76. func (h *header) details() string {
  77. t := styles.CurrentTheme()
  78. cwd := fsext.DirTrim(fsext.PrettyPath(config.Get().WorkingDir()), 4)
  79. parts := []string{
  80. t.S().Muted.Render(cwd),
  81. }
  82. errorCount := 0
  83. for _, l := range h.lspClients {
  84. for _, diagnostics := range l.GetDiagnostics() {
  85. for _, diagnostic := range diagnostics {
  86. if diagnostic.Severity == protocol.SeverityError {
  87. errorCount++
  88. }
  89. }
  90. }
  91. }
  92. if errorCount > 0 {
  93. parts = append(parts, t.S().Error.Render(fmt.Sprintf("%s%d", styles.ErrorIcon, errorCount)))
  94. }
  95. agentCfg := config.Get().Agents["coder"]
  96. model := config.Get().GetModelByType(agentCfg.Model)
  97. percentage := (float64(h.session.CompletionTokens+h.session.PromptTokens) / float64(model.ContextWindow)) * 100
  98. formattedPercentage := t.S().Muted.Render(fmt.Sprintf("%d%%", int(percentage)))
  99. parts = append(parts, formattedPercentage)
  100. if h.detailsOpen {
  101. parts = append(parts, t.S().Muted.Render("ctrl+d")+t.S().Subtle.Render(" close"))
  102. } else {
  103. parts = append(parts, t.S().Muted.Render("ctrl+d")+t.S().Subtle.Render(" open "))
  104. }
  105. dot := t.S().Subtle.Render(" • ")
  106. return strings.Join(parts, dot)
  107. }
  108. func (h *header) SetDetailsOpen(open bool) {
  109. h.detailsOpen = open
  110. }
  111. // SetSession implements Header.
  112. func (h *header) SetSession(session session.Session) tea.Cmd {
  113. h.session = session
  114. return nil
  115. }
  116. // SetWidth implements Header.
  117. func (h *header) SetWidth(width int) tea.Cmd {
  118. h.width = width
  119. return nil
  120. }
  121. // ShowingDetails implements Header.
  122. func (h *header) ShowingDetails() bool {
  123. return h.detailsOpen
  124. }