Refactor sidebar component to use struct

This commit refactors the sidebar component to use a struct for better
organization and maintainability. The `data` and `sindex` fields are now
exported as `Data` and `Sindex` respectively, allowing for easier access
and modification. Additionally, the styling for the selected sidebar
item has been updated for improved visual clarity.
This commit is contained in:
vaibhav
2026-01-18 04:02:17 +05:30
parent 1f413157ef
commit 0eadc7b502
2 changed files with 18 additions and 13 deletions

View File

@@ -6,8 +6,8 @@ import (
) )
type Sidebar struct { type Sidebar struct {
data []string Data []string
sindex int Sindex int
} }
func (s Sidebar) Init() tea.Cmd { func (s Sidebar) Init() tea.Cmd {
@@ -19,12 +19,12 @@ func (s *Sidebar) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg: case tea.KeyMsg:
switch m.String() { switch m.String() {
case "up": case "up":
if s.sindex > 0 { if s.Sindex > 0 {
s.sindex-- s.Sindex--
} }
case "down": case "down":
if s.sindex < len(s.data)-1 { if s.Sindex < len(s.Data)-1 {
s.sindex++ s.Sindex++
} }
} }
} }
@@ -33,12 +33,12 @@ func (s *Sidebar) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (s Sidebar) View() string { func (s Sidebar) View() string {
var items []string var items []string
for i, item := range s.data { for i, item := range s.Data {
if i == s.sindex { if i == s.Sindex {
items = append(items, lipgloss. items = append(items, lipgloss.
NewStyle(). NewStyle().
Background(lipgloss.Color("93BD57")). Foreground(lipgloss.Color("#FFFFFF")).
Padding(1, 2). Background(lipgloss.Color("#93BD57")).
Render(item)) Render(item))
} else { } else {
items = append(items, item) items = append(items, item)

11
main.go
View File

@@ -14,7 +14,7 @@ type model struct {
textInput textinput.Model textInput textinput.Model
width int width int
height int height int
sidebarContent string sidebarContent components.Sidebar
} }
func initialModel() model { func initialModel() model {
@@ -24,9 +24,14 @@ func initialModel() model {
ti.CharLimit = 30 ti.CharLimit = 30
ti.Prompt = "" ti.Prompt = ""
ti.Focus() ti.Focus()
sidebarcon := components.Sidebar{
Data: []string{"Library", "Playlist1", "Playlist2", "Favorites"},
Sindex: 0,
}
return model{ return model{
textInput: ti, textInput: ti,
sidebarContent: "Library\n\nPlaylist 1\nPlaylist 2\nFavorites", sidebarContent: sidebarcon,
} }
} }
@@ -67,7 +72,7 @@ func (m model) View() string {
// sidebar := lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder()).Padding(1, 2).Render(m.sidebarContent) // sidebar := lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder()).Padding(1, 2).Render(m.sidebarContent)
// Combine search and sidebar vertically // Combine search and sidebar vertically
content := lipgloss.JoinVertical(lipgloss.Left, searchBox, "\n", components.Sidebar(m.sidebarContent)) content := lipgloss.JoinVertical(lipgloss.Left, searchBox, "\n", m.sidebarContent.View())
return lipgloss.Place( return lipgloss.Place(
m.width, m.width,