bubble.go

  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(false)
 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	return b.updateItems()
111}
112
113func (b *Bubble) Init() tea.Cmd {
114	return nil
115}
116
117func (b *Bubble) SetSize(width, height int) {
118	b.width = width
119	b.height = height
120	b.list.SetSize(width-b.widthMargin, height-b.heightMargin)
121}
122
123func (b *Bubble) Help() []types.HelpEntry {
124	return nil
125}
126
127func (b *Bubble) updateItems() tea.Cmd {
128	its := make(items, 0)
129	tags := make(items, 0)
130	for _, r := range b.repo.GetReferences() {
131		if r.Type() != plumbing.HashReference {
132			continue
133		}
134		if r.Name().IsTag() {
135			tags = append(tags, item{r})
136		} else {
137			its = append(its, item{r})
138		}
139	}
140	sort.Sort(its)
141	sort.Sort(tags)
142	its = append(its, tags...)
143	itt := make([]list.Item, len(its))
144	for i, it := range its {
145		itt[i] = it
146	}
147	return b.list.SetItems(itt)
148}
149
150func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
151	cmds := make([]tea.Cmd, 0)
152	switch msg := msg.(type) {
153	case tea.WindowSizeMsg:
154		b.SetSize(msg.Width, msg.Height)
155
156	case tea.KeyMsg:
157		switch msg.String() {
158		case "B":
159			return b, b.reset()
160		case "enter", "right", "l":
161			if b.list.Index() >= 0 {
162				ref := b.list.SelectedItem().(item).Reference
163				return b.SetBranch(ref)
164			}
165		}
166	}
167
168	l, cmd := b.list.Update(msg)
169	b.list = l
170	cmds = append(cmds, cmd)
171
172	return b, tea.Batch(cmds...)
173}
174
175func (b *Bubble) View() string {
176	return b.list.View()
177}