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.
54 lines
946 B
Go
54 lines
946 B
Go
package components
|
|
|
|
import (
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/lipgloss"
|
|
)
|
|
|
|
type Sidebar struct {
|
|
Data []string
|
|
Sindex int
|
|
}
|
|
|
|
func (s Sidebar) Init() tea.Cmd {
|
|
return nil
|
|
}
|
|
|
|
func (s *Sidebar) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
switch m := msg.(type) {
|
|
case tea.KeyMsg:
|
|
switch m.String() {
|
|
case "up":
|
|
if s.Sindex > 0 {
|
|
s.Sindex--
|
|
}
|
|
case "down":
|
|
if s.Sindex < len(s.Data)-1 {
|
|
s.Sindex++
|
|
}
|
|
}
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
func (s Sidebar) View() string {
|
|
var items []string
|
|
for i, item := range s.Data {
|
|
if i == s.Sindex {
|
|
items = append(items, lipgloss.
|
|
NewStyle().
|
|
Foreground(lipgloss.Color("#FFFFFF")).
|
|
Background(lipgloss.Color("#93BD57")).
|
|
Render(item))
|
|
} else {
|
|
items = append(items, item)
|
|
}
|
|
}
|
|
|
|
return lipgloss.
|
|
NewStyle().
|
|
BorderStyle(lipgloss.NormalBorder()).
|
|
Padding(1, 2).
|
|
Render(lipgloss.JoinVertical(lipgloss.Left, items...))
|
|
}
|