1package repo
2
3import (
4 "fmt"
5 "io"
6 "io/fs"
7 "strings"
8
9 "github.com/charmbracelet/bubbles/key"
10 "github.com/charmbracelet/bubbles/list"
11 tea "github.com/charmbracelet/bubbletea"
12 "github.com/charmbracelet/lipgloss"
13 "github.com/charmbracelet/soft-serve/git"
14 "github.com/charmbracelet/soft-serve/server/ui/common"
15 "github.com/dustin/go-humanize"
16)
17
18// FileItem is a list item for a file.
19type FileItem struct {
20 entry *git.TreeEntry
21}
22
23// ID returns the ID of the file item.
24func (i FileItem) ID() string {
25 return i.entry.Name()
26}
27
28// Title returns the title of the file item.
29func (i FileItem) Title() string {
30 return i.entry.Name()
31}
32
33// Description returns the description of the file item.
34func (i FileItem) Description() string {
35 return ""
36}
37
38// Mode returns the mode of the file item.
39func (i FileItem) Mode() fs.FileMode {
40 return i.entry.Mode()
41}
42
43// FilterValue implements list.Item.
44func (i FileItem) FilterValue() string { return i.Title() }
45
46// FileItems is a list of file items.
47type FileItems []FileItem
48
49// Len implements sort.Interface.
50func (cl FileItems) Len() int { return len(cl) }
51
52// Swap implements sort.Interface.
53func (cl FileItems) Swap(i, j int) { cl[i], cl[j] = cl[j], cl[i] }
54
55// Less implements sort.Interface.
56func (cl FileItems) Less(i, j int) bool {
57 if cl[i].entry.IsTree() && cl[j].entry.IsTree() {
58 return cl[i].Title() < cl[j].Title()
59 } else if cl[i].entry.IsTree() {
60 return true
61 } else if cl[j].entry.IsTree() {
62 return false
63 } else {
64 return cl[i].Title() < cl[j].Title()
65 }
66}
67
68// FileItemDelegate is the delegate for the file item list.
69type FileItemDelegate struct {
70 common *common.Common
71}
72
73// Height returns the height of the file item list. Implements list.ItemDelegate.
74func (d FileItemDelegate) Height() int { return 1 }
75
76// Spacing returns the spacing of the file item list. Implements list.ItemDelegate.
77func (d FileItemDelegate) Spacing() int { return 0 }
78
79// Update implements list.ItemDelegate.
80func (d FileItemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd {
81 item, ok := m.SelectedItem().(FileItem)
82 if !ok {
83 return nil
84 }
85 switch msg := msg.(type) {
86 case tea.KeyMsg:
87 switch {
88 case key.Matches(msg, d.common.KeyMap.Copy):
89 return copyCmd(item.entry.Name(), fmt.Sprintf("File name %q copied to clipboard", item.entry.Name()))
90 }
91 }
92 return nil
93}
94
95// Render implements list.ItemDelegate.
96func (d FileItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
97 i, ok := listItem.(FileItem)
98 if !ok {
99 return
100 }
101
102 s := d.common.Styles.Tree
103
104 name := i.Title()
105 size := humanize.Bytes(uint64(i.entry.Size()))
106 size = strings.ReplaceAll(size, " ", "")
107 sizeLen := lipgloss.Width(size)
108 if i.entry.IsTree() {
109 size = strings.Repeat(" ", sizeLen)
110 if index == m.Index() {
111 name = s.Active.FileDir.Render(name)
112 } else {
113 name = s.Normal.FileDir.Render(name)
114 }
115 }
116 var nameStyle, sizeStyle, modeStyle lipgloss.Style
117 mode := i.Mode()
118 if index == m.Index() {
119 nameStyle = s.Active.FileName
120 sizeStyle = s.Active.FileSize
121 modeStyle = s.Active.FileMode
122 fmt.Fprint(w, s.Selector.Render(">"))
123 } else {
124 nameStyle = s.Normal.FileName
125 sizeStyle = s.Normal.FileSize
126 modeStyle = s.Normal.FileMode
127 fmt.Fprint(w, s.Selector.Render(" "))
128 }
129 sizeStyle = sizeStyle.Copy().
130 Width(8).
131 Align(lipgloss.Right).
132 MarginLeft(1)
133 leftMargin := s.Selector.GetMarginLeft() +
134 s.Selector.GetWidth() +
135 s.Normal.FileMode.GetMarginLeft() +
136 s.Normal.FileMode.GetWidth() +
137 nameStyle.GetMarginLeft() +
138 sizeStyle.GetHorizontalFrameSize()
139 name = common.TruncateString(name, m.Width()-leftMargin)
140 name = nameStyle.Render(name)
141 size = sizeStyle.Render(size)
142 modeStr := modeStyle.Render(mode.String())
143 truncate := d.common.Renderer.NewStyle().MaxWidth(m.Width() -
144 s.Selector.GetHorizontalFrameSize() -
145 s.Selector.GetWidth())
146 fmt.Fprint(w,
147 d.common.Zone.Mark(
148 i.ID(),
149 truncate.Render(fmt.Sprintf("%s%s%s",
150 modeStr,
151 size,
152 name,
153 )),
154 ),
155 )
156}