Files
music_player/main.go
vaibhav 0eadc7b502 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.
2026-01-18 04:02:17 +05:30

93 lines
1.8 KiB
Go

package main
import (
"fmt"
"os"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/vvaibhavv11/player/components"
)
type model struct {
textInput textinput.Model
width int
height int
sidebarContent components.Sidebar
}
func initialModel() model {
ti := textinput.New()
ti.Placeholder = "Search"
ti.Width = 30
ti.CharLimit = 30
ti.Prompt = ""
ti.Focus()
sidebarcon := components.Sidebar{
Data: []string{"Library", "Playlist1", "Playlist2", "Favorites"},
Sindex: 0,
}
return model{
textInput: ti,
sidebarContent: sidebarcon,
}
}
func (m model) Init() tea.Cmd {
return textinput.Blink
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "ctrl+c":
return m, tea.Quit
}
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
}
m.textInput, cmd = m.textInput.Update(msg)
return m, cmd
}
func (m model) View() string {
if m.width == 0 {
return "loading..."
}
searchBox := lipgloss.Place(
m.width,
m.height/12,
lipgloss.Center,
lipgloss.Center,
lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder()).Padding(0, 1).Render(m.textInput.View()),
)
// Create sidebar
// sidebar := lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder()).Padding(1, 2).Render(m.sidebarContent)
// Combine search and sidebar vertically
content := lipgloss.JoinVertical(lipgloss.Left, searchBox, "\n", m.sidebarContent.View())
return lipgloss.Place(
m.width,
m.height,
0,
0,
content,
)
}
func main() {
p := tea.NewProgram(initialModel(), tea.WithAltScreen())
if _, err := p.Run(); err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
}