difi/internal/ui/delegate.go

54 lines
1.3 KiB
Go

package ui
import (
"fmt"
"io"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/ansi"
"github.com/oug-t/difi/internal/tree"
)
type TreeDelegate struct {
Focused bool
}
func (d TreeDelegate) Height() int { return 1 }
func (d TreeDelegate) Spacing() int { return 0 }
func (d TreeDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd { return nil }
func (d TreeDelegate) Render(w io.Writer, m list.Model, index int, item list.Item) {
i, ok := item.(tree.TreeItem)
if !ok {
return
}
var title string
if i.Flat {
iconWidth := lipgloss.Width(i.Icon) + 1 // icon + space
path := tree.ShortenPath(i.FullPath, m.Width()-2-iconWidth)
title = fmt.Sprintf("%s %s", i.Icon, path)
} else {
title = i.Title()
}
title = ansi.Truncate(title, m.Width()-2, "…")
if index == m.Index() {
style := lipgloss.NewStyle().
Background(lipgloss.Color("237")). // Dark gray background
Foreground(lipgloss.Color("255")). // White text
Bold(true)
if !d.Focused {
style = style.Foreground(lipgloss.Color("245"))
}
fmt.Fprint(w, style.Render(title))
} else {
style := lipgloss.NewStyle().Foreground(lipgloss.Color("252"))
fmt.Fprint(w, style.Render(title))
}
}