chat.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package chat
  2. import (
  3. "fmt"
  4. "sort"
  5. "github.com/charmbracelet/lipgloss"
  6. "github.com/charmbracelet/x/ansi"
  7. "github.com/opencode-ai/opencode/internal/config"
  8. "github.com/opencode-ai/opencode/internal/message"
  9. "github.com/opencode-ai/opencode/internal/session"
  10. "github.com/opencode-ai/opencode/internal/tui/styles"
  11. "github.com/opencode-ai/opencode/internal/tui/theme"
  12. "github.com/opencode-ai/opencode/internal/version"
  13. )
  14. type SendMsg struct {
  15. Text string
  16. Attachments []message.Attachment
  17. }
  18. type SessionSelectedMsg = session.Session
  19. type SessionClearedMsg struct{}
  20. type EditorFocusMsg bool
  21. type CompactSessionMsg struct{}
  22. func header(width int) string {
  23. return lipgloss.JoinVertical(
  24. lipgloss.Top,
  25. logo(width),
  26. repo(width),
  27. "",
  28. cwd(width),
  29. )
  30. }
  31. func lspsConfigured(width int) string {
  32. cfg := config.Get()
  33. title := "LSP Servers"
  34. title = ansi.Truncate(title, width, "…")
  35. t := theme.CurrentTheme()
  36. baseStyle := styles.BaseStyle()
  37. lsps := baseStyle.
  38. Width(width).
  39. Foreground(t.Primary()).
  40. Bold(true).
  41. Render(title)
  42. // Get LSP names and sort them for consistent ordering
  43. var lspNames []string
  44. for name := range cfg.LSP {
  45. lspNames = append(lspNames, name)
  46. }
  47. sort.Strings(lspNames)
  48. var lspViews []string
  49. for _, name := range lspNames {
  50. lsp := cfg.LSP[name]
  51. lspName := baseStyle.
  52. Foreground(t.Text()).
  53. Render(fmt.Sprintf("• %s", name))
  54. cmd := lsp.Command
  55. cmd = ansi.Truncate(cmd, width-lipgloss.Width(lspName)-3, "…")
  56. lspPath := baseStyle.
  57. Foreground(t.TextMuted()).
  58. Render(fmt.Sprintf(" (%s)", cmd))
  59. lspViews = append(lspViews,
  60. baseStyle.
  61. Width(width).
  62. Render(
  63. lipgloss.JoinHorizontal(
  64. lipgloss.Left,
  65. lspName,
  66. lspPath,
  67. ),
  68. ),
  69. )
  70. }
  71. return baseStyle.
  72. Width(width).
  73. Render(
  74. lipgloss.JoinVertical(
  75. lipgloss.Left,
  76. lsps,
  77. lipgloss.JoinVertical(
  78. lipgloss.Left,
  79. lspViews...,
  80. ),
  81. ),
  82. )
  83. }
  84. func logo(width int) string {
  85. logo := fmt.Sprintf("%s %s", styles.OpenCodeIcon, "OpenCode")
  86. t := theme.CurrentTheme()
  87. baseStyle := styles.BaseStyle()
  88. versionText := baseStyle.
  89. Foreground(t.TextMuted()).
  90. Render(version.Version)
  91. return baseStyle.
  92. Bold(true).
  93. Width(width).
  94. Render(
  95. lipgloss.JoinHorizontal(
  96. lipgloss.Left,
  97. logo,
  98. " ",
  99. versionText,
  100. ),
  101. )
  102. }
  103. func repo(width int) string {
  104. repo := "github.com/opencode-ai/opencode"
  105. t := theme.CurrentTheme()
  106. return styles.BaseStyle().
  107. Foreground(t.TextMuted()).
  108. Width(width).
  109. Render(repo)
  110. }
  111. func cwd(width int) string {
  112. cwd := fmt.Sprintf("cwd: %s", config.WorkingDirectory())
  113. t := theme.CurrentTheme()
  114. return styles.BaseStyle().
  115. Foreground(t.TextMuted()).
  116. Width(width).
  117. Render(cwd)
  118. }