64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
package tui
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/charmbracelet/bubbles/list"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/lipgloss"
|
|
)
|
|
|
|
// itemDelegate is a custom list delegate that renders both folderItems and
|
|
// requestItems in a single-row style with method colour coding.
|
|
type itemDelegate struct{}
|
|
|
|
func (d itemDelegate) Height() int { return 1 }
|
|
func (d itemDelegate) Spacing() int { return 0 }
|
|
|
|
func (d itemDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
|
|
|
|
func (d itemDelegate) Render(w io.Writer, m list.Model, index int, itm list.Item) {
|
|
isSelected := index == m.Index()
|
|
maxW := m.Width()
|
|
if maxW <= 0 {
|
|
maxW = 30
|
|
}
|
|
|
|
switch i := itm.(type) {
|
|
case folderItem:
|
|
line := folderItemStyle.Render(" ▸ " + strings.ToUpper(i.name))
|
|
fmt.Fprint(w, line)
|
|
|
|
case requestItem:
|
|
method := fmt.Sprintf("%-6s", i.request.Method)
|
|
styledMethod := methodStyle(i.request.Method).Render(method)
|
|
|
|
name := i.request.Name
|
|
// Truncate to avoid overflowing the panel
|
|
nameMax := maxW - 10
|
|
if nameMax < 1 {
|
|
nameMax = 1
|
|
}
|
|
if len(name) > nameMax {
|
|
name = name[:nameMax-1] + "…"
|
|
}
|
|
|
|
indent := " "
|
|
if i.folder != "" {
|
|
indent = " "
|
|
}
|
|
|
|
var styledName string
|
|
if isSelected {
|
|
styledName = selectedItemStyle.Render(name)
|
|
cursor := lipgloss.NewStyle().Foreground(colorPurple).Render("▶")
|
|
fmt.Fprintf(w, "%s%s %s %s", indent, cursor, styledMethod, styledName)
|
|
} else {
|
|
styledName = lipgloss.NewStyle().Foreground(colorFg).Render(name)
|
|
fmt.Fprintf(w, "%s %s %s", indent, styledMethod, styledName)
|
|
}
|
|
}
|
|
}
|