1package repo
2
3import (
4 "fmt"
5 "io"
6
7 "github.com/charmbracelet/bubbles/key"
8 "github.com/charmbracelet/bubbles/list"
9 tea "github.com/charmbracelet/bubbletea"
10 "github.com/charmbracelet/soft-serve/pkg/ui/common"
11 gitm "github.com/aymanbagabas/git-module"
12)
13
14// StashItem represents a stash item.
15type StashItem struct{ *gitm.Stash }
16
17// ID returns the ID of the stash item.
18func (i StashItem) ID() string {
19 return fmt.Sprintf("stash@{%d}", i.Index)
20}
21
22// Title returns the title of the stash item.
23func (i StashItem) Title() string {
24 return i.Message
25}
26
27// Description returns the description of the stash item.
28func (i StashItem) Description() string {
29 return ""
30}
31
32// FilterValue implements list.Item.
33func (i StashItem) FilterValue() string { return i.Title() }
34
35// StashItems is a list of stash items.
36type StashItems []StashItem
37
38// Len implements sort.Interface.
39func (cl StashItems) Len() int { return len(cl) }
40
41// Swap implements sort.Interface.
42func (cl StashItems) Swap(i, j int) { cl[i], cl[j] = cl[j], cl[i] }
43
44// Less implements sort.Interface.
45func (cl StashItems) Less(i, j int) bool {
46 return cl[i].Index < cl[j].Index
47}
48
49// StashItemDelegate is a delegate for stash items.
50type StashItemDelegate struct {
51 common *common.Common
52}
53
54// Height returns the height of the stash item list. Implements list.ItemDelegate.
55func (d StashItemDelegate) Height() int { return 1 }
56
57// Spacing implements list.ItemDelegate.
58func (d StashItemDelegate) Spacing() int { return 0 }
59
60// Update implements list.ItemDelegate.
61func (d StashItemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd {
62 item, ok := m.SelectedItem().(StashItem)
63 if !ok {
64 return nil
65 }
66
67 switch msg := msg.(type) {
68 case tea.KeyMsg:
69 switch {
70 case key.Matches(msg, d.common.KeyMap.Copy):
71 return copyCmd(item.Title(), fmt.Sprintf("Stash message %q copied to clipboard", item.Title()))
72 }
73 }
74
75 return nil
76}
77
78// Render implements list.ItemDelegate.
79func (d StashItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
80 item, ok := listItem.(StashItem)
81 if !ok {
82 return
83 }
84
85 s := d.common.Styles.Stash
86
87 st := s.Normal.Message
88 selector := " "
89 if index == m.Index() {
90 selector = "> "
91 st = s.Active.Message
92 }
93
94 selector = s.Selector.Render(selector)
95 title := st.Render(item.Title())
96 fmt.Fprint(w, d.common.Zone.Mark(
97 item.ID(),
98 common.TruncateString(fmt.Sprintf("%s%s",
99 selector,
100 title,
101 ), m.Width()-
102 s.Selector.GetWidth()-
103 st.GetHorizontalFrameSize(),
104 ),
105 ))
106}