refsitem.go

 1package repo
 2
 3import (
 4	"fmt"
 5	"io"
 6
 7	"github.com/charmbracelet/bubbles/list"
 8	tea "github.com/charmbracelet/bubbletea"
 9	"github.com/charmbracelet/soft-serve/git"
10	"github.com/charmbracelet/soft-serve/tui/common"
11	"github.com/charmbracelet/soft-serve/ui/styles"
12)
13
14type RefItem struct {
15	*git.Reference
16}
17
18func (i RefItem) ID() string {
19	return i.Reference.Name().String()
20}
21
22func (i RefItem) Title() string {
23	return i.Reference.Name().Short()
24}
25
26func (i RefItem) Description() string {
27	return ""
28}
29
30func (i RefItem) Short() string {
31	return i.Reference.Name().Short()
32}
33
34func (i RefItem) FilterValue() string { return i.Short() }
35
36type RefItems []RefItem
37
38func (cl RefItems) Len() int      { return len(cl) }
39func (cl RefItems) Swap(i, j int) { cl[i], cl[j] = cl[j], cl[i] }
40func (cl RefItems) Less(i, j int) bool {
41	return cl[i].Short() < cl[j].Short()
42}
43
44type RefItemDelegate struct {
45	style *styles.Styles
46}
47
48func (d RefItemDelegate) Height() int                               { return 1 }
49func (d RefItemDelegate) Spacing() int                              { return 0 }
50func (d RefItemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd { return nil }
51func (d RefItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
52	s := d.style
53	i, ok := listItem.(RefItem)
54	if !ok {
55		return
56	}
57
58	ref := i.Short()
59	if i.Reference.IsTag() {
60		ref = s.RefItemTag.Render(ref)
61	}
62	ref = s.RefItemBranch.Render(ref)
63	refMaxWidth := m.Width() -
64		s.RefItemSelector.GetMarginLeft() -
65		s.RefItemSelector.GetWidth() -
66		s.RefItemInactive.GetMarginLeft()
67	ref = common.TruncateString(ref, refMaxWidth, "…")
68	if index == m.Index() {
69		fmt.Fprint(w, s.RefItemSelector.Render(">")+
70			s.RefItemActive.Render(ref))
71	} else {
72		fmt.Fprint(w, s.LogItemSelector.Render(" ")+
73			s.RefItemInactive.Render(ref))
74	}
75}