1package commands
2
3import (
4 tea "github.com/charmbracelet/bubbletea/v2"
5 "github.com/charmbracelet/crush/internal/tui/components/core"
6 "github.com/charmbracelet/crush/internal/tui/components/core/layout"
7 "github.com/charmbracelet/crush/internal/tui/components/core/list"
8 "github.com/charmbracelet/crush/internal/tui/styles"
9 "github.com/charmbracelet/crush/internal/tui/util"
10 "github.com/charmbracelet/x/ansi"
11)
12
13type ItemSection interface {
14 util.Model
15 layout.Sizeable
16 list.SectionHeader
17 SetInfo(info string)
18}
19type itemSectionModel struct {
20 width int
21 title string
22 info string
23}
24
25func NewItemSection(title string) ItemSection {
26 return &itemSectionModel{
27 title: title,
28 }
29}
30
31func (m *itemSectionModel) Init() tea.Cmd {
32 return nil
33}
34
35func (m *itemSectionModel) Update(tea.Msg) (tea.Model, tea.Cmd) {
36 return m, nil
37}
38
39func (m *itemSectionModel) View() tea.View {
40 t := styles.CurrentTheme()
41 title := ansi.Truncate(m.title, m.width-2, "…")
42 style := t.S().Base.Padding(1, 1, 0, 1)
43 title = t.S().Muted.Render(title)
44 section := ""
45 if m.info != "" {
46 section = core.SectionWithInfo(title, m.width-2, m.info)
47 } else {
48 section = core.Section(title, m.width-2)
49 }
50
51 return tea.NewView(style.Render(section))
52}
53
54func (m *itemSectionModel) GetSize() (int, int) {
55 return m.width, 1
56}
57
58func (m *itemSectionModel) SetSize(width int, height int) tea.Cmd {
59 m.width = width
60 return nil
61}
62
63func (m *itemSectionModel) IsSectionHeader() bool {
64 return true
65}
66
67func (m *itemSectionModel) SetInfo(info string) {
68 m.info = info
69}