sidebar.go

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