chat.go 2.6 KB

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