sidebar.go

 1package model
 2
 3import (
 4	tea "charm.land/bubbletea/v2"
 5	"charm.land/lipgloss/v2"
 6	"github.com/charmbracelet/crush/internal/ui/common"
 7)
 8
 9// SidebarModel is the model for the sidebar UI component.
10type SidebarModel struct {
11	com *common.Common
12
13	// width of the sidebar.
14	width int
15
16	// Cached rendered logo string.
17	logo string
18	// Cached cwd string.
19	cwd string
20
21	// TODO: lsp, files, session
22
23	// Whether to render the sidebar in compact mode.
24	compact bool
25}
26
27// NewSidebarModel creates a new SidebarModel instance.
28func NewSidebarModel(com *common.Common) *SidebarModel {
29	return &SidebarModel{
30		com:     com,
31		compact: true,
32		cwd:     com.Config().WorkingDir(),
33	}
34}
35
36// Init initializes the sidebar model.
37func (m *SidebarModel) Init() tea.Cmd {
38	return nil
39}
40
41// Update updates the sidebar model based on incoming messages.
42func (m *SidebarModel) Update(msg tea.Msg) (*SidebarModel, tea.Cmd) {
43	return m, nil
44}
45
46// View renders the sidebar model as a string.
47func (m *SidebarModel) View() string {
48	s := m.com.Styles.SidebarFull
49	if m.compact {
50		s = m.com.Styles.SidebarCompact
51	}
52
53	blocks := []string{
54		m.logo,
55	}
56
57	return s.Render(lipgloss.JoinVertical(
58		lipgloss.Top,
59		blocks...,
60	))
61}
62
63// SetWidth sets the width of the sidebar and updates the logo accordingly.
64func (m *SidebarModel) SetWidth(width int) {
65	m.logo = renderLogo(m.com.Styles, true, width)
66	m.width = width
67}