chat.go

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