item.go

 1package commands
 2
 3import (
 4	"strings"
 5
 6	tea "github.com/charmbracelet/bubbletea/v2"
 7	"github.com/charmbracelet/lipgloss/v2"
 8	"github.com/charmbracelet/x/ansi"
 9	"github.com/opencode-ai/opencode/internal/tui/components/core/list"
10	"github.com/opencode-ai/opencode/internal/tui/layout"
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/tui/util"
14)
15
16type ItemSection interface {
17	util.Model
18	layout.Sizeable
19	list.SectionHeader
20}
21type itemSectionModel struct {
22	width int
23	title string
24}
25
26func NewItemSection(title string) ItemSection {
27	return &itemSectionModel{
28		title: title,
29	}
30}
31
32func (m *itemSectionModel) Init() tea.Cmd {
33	return nil
34}
35
36func (m *itemSectionModel) Update(tea.Msg) (tea.Model, tea.Cmd) {
37	return m, nil
38}
39
40func (m *itemSectionModel) View() tea.View {
41	t := theme.CurrentTheme()
42	title := ansi.Truncate(m.title, m.width-1, "…")
43	style := styles.BaseStyle().Padding(1, 0, 0, 0).Width(m.width).Foreground(t.TextMuted()).Bold(true)
44	if len(title) < m.width {
45		remainingWidth := m.width - lipgloss.Width(title)
46		if remainingWidth > 0 {
47			title += " " + strings.Repeat("─", remainingWidth-1)
48		}
49	}
50	return tea.NewView(style.Render(title))
51}
52
53func (m *itemSectionModel) GetSize() (int, int) {
54	return m.width, 1
55}
56
57func (m *itemSectionModel) SetSize(width int, height int) tea.Cmd {
58	m.width = width
59	return nil
60}
61
62func (m *itemSectionModel) IsSectionHeader() bool {
63	return true
64}