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