chat.go 2.6 KB

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