1package refs
2
3import (
4 "fmt"
5 "io"
6 "sort"
7
8 "github.com/charmbracelet/bubbles/list"
9 tea "github.com/charmbracelet/bubbletea"
10 "github.com/charmbracelet/soft-serve/internal/tui/bubbles/git/types"
11 "github.com/charmbracelet/soft-serve/internal/tui/style"
12 "github.com/go-git/go-git/v5/plumbing"
13)
14
15type RefMsg = *plumbing.Reference
16
17type item struct {
18 *plumbing.Reference
19}
20
21func (i item) Short() string {
22 return i.Name().Short()
23}
24
25func (i item) FilterValue() string { return i.Short() }
26
27type items []item
28
29func (cl items) Len() int { return len(cl) }
30func (cl items) Swap(i, j int) { cl[i], cl[j] = cl[j], cl[i] }
31func (cl items) Less(i, j int) bool {
32 return cl[i].Name().Short() < cl[j].Name().Short()
33}
34
35type itemDelegate struct {
36 style *style.Styles
37}
38
39func (d itemDelegate) Height() int { return 1 }
40func (d itemDelegate) Spacing() int { return 0 }
41func (d itemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd { return nil }
42func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
43 s := d.style
44 i, ok := listItem.(item)
45 if !ok {
46 return
47 }
48
49 ref := i.Short()
50 if i.Name().IsTag() {
51 ref = s.RefItemTag.Render(ref)
52 }
53 ref = s.RefItemBranch.Render(ref)
54 refMaxWidth := m.Width() -
55 s.RefItemSelector.GetMarginLeft() -
56 s.RefItemSelector.GetWidth() -
57 s.RefItemInactive.GetMarginLeft()
58 ref = types.TruncateString(ref, refMaxWidth, "…")
59 if index == m.Index() {
60 fmt.Fprint(w, s.RefItemSelector.Render(">")+
61 s.RefItemActive.Render(ref))
62 } else {
63 fmt.Fprint(w, s.LogItemSelector.Render(" ")+
64 s.RefItemInactive.Render(ref))
65 }
66}
67
68type Bubble struct {
69 repo types.Repo
70 list list.Model
71 style *style.Styles
72 width int
73 widthMargin int
74 height int
75 heightMargin int
76 ref *plumbing.Reference
77}
78
79func NewBubble(repo types.Repo, styles *style.Styles, width, widthMargin, height, heightMargin int) *Bubble {
80 l := list.NewModel([]list.Item{}, itemDelegate{styles}, width-widthMargin, height-heightMargin)
81 l.SetShowFilter(false)
82 l.SetShowHelp(false)
83 l.SetShowPagination(true)
84 l.SetShowStatusBar(false)
85 l.SetShowTitle(false)
86 l.SetFilteringEnabled(false)
87 l.DisableQuitKeybindings()
88 b := &Bubble{
89 repo: repo,
90 style: styles,
91 width: width,
92 height: height,
93 widthMargin: widthMargin,
94 heightMargin: heightMargin,
95 list: l,
96 ref: repo.GetHEAD(),
97 }
98 b.SetSize(width, height)
99 return b
100}
101
102func (b *Bubble) SetBranch(ref *plumbing.Reference) (tea.Model, tea.Cmd) {
103 b.ref = ref
104 return b, func() tea.Msg {
105 return RefMsg(ref)
106 }
107}
108
109func (b *Bubble) reset() tea.Cmd {
110 cmd := b.updateItems()
111 b.SetSize(b.width, b.height)
112 return cmd
113}
114
115func (b *Bubble) Init() tea.Cmd {
116 return nil
117}
118
119func (b *Bubble) SetSize(width, height int) {
120 b.width = width
121 b.height = height
122 b.list.SetSize(width-b.widthMargin, height-b.heightMargin)
123 b.list.Styles.PaginationStyle = b.style.RefPaginator.Copy().Width(width - b.widthMargin)
124}
125
126func (b *Bubble) Help() []types.HelpEntry {
127 return nil
128}
129
130func (b *Bubble) updateItems() tea.Cmd {
131 its := make(items, 0)
132 tags := make(items, 0)
133 for _, r := range b.repo.GetReferences() {
134 if r.Type() != plumbing.HashReference {
135 continue
136 }
137 n := r.Name()
138 if n.IsTag() {
139 tags = append(tags, item{r})
140 } else if n.IsBranch() {
141 its = append(its, item{r})
142 }
143 }
144 sort.Sort(its)
145 sort.Sort(tags)
146 its = append(its, tags...)
147 itt := make([]list.Item, len(its))
148 for i, it := range its {
149 itt[i] = it
150 }
151 return b.list.SetItems(itt)
152}
153
154func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
155 cmds := make([]tea.Cmd, 0)
156 switch msg := msg.(type) {
157 case tea.WindowSizeMsg:
158 b.SetSize(msg.Width, msg.Height)
159
160 case tea.KeyMsg:
161 switch msg.String() {
162 case "B":
163 return b, b.reset()
164 case "enter", "right", "l":
165 if b.list.Index() >= 0 {
166 ref := b.list.SelectedItem().(item).Reference
167 return b.SetBranch(ref)
168 }
169 }
170 }
171
172 l, cmd := b.list.Update(msg)
173 b.list = l
174 cmds = append(cmds, cmd)
175
176 return b, tea.Batch(cmds...)
177}
178
179func (b *Bubble) View() string {
180 return b.list.View()
181}