1package repo
2
3import (
4 "fmt"
5 "strings"
6
7 "github.com/charmbracelet/bubbles/v2/help"
8 "github.com/charmbracelet/bubbles/v2/key"
9 "github.com/charmbracelet/bubbles/v2/spinner"
10 tea "github.com/charmbracelet/bubbletea/v2"
11 lipgloss "github.com/charmbracelet/lipgloss/v2"
12 "github.com/charmbracelet/soft-serve/git"
13 "github.com/charmbracelet/soft-serve/pkg/proto"
14 "github.com/charmbracelet/soft-serve/pkg/ui/common"
15 "github.com/charmbracelet/soft-serve/pkg/ui/components/footer"
16 "github.com/charmbracelet/soft-serve/pkg/ui/components/selector"
17 "github.com/charmbracelet/soft-serve/pkg/ui/components/statusbar"
18 "github.com/charmbracelet/soft-serve/pkg/ui/components/tabs"
19)
20
21type state int
22
23const (
24 loadingState state = iota
25 readyState
26)
27
28// EmptyRepoMsg is a message to indicate that the repository is empty.
29type EmptyRepoMsg struct{}
30
31// CopyURLMsg is a message to copy the URL of the current repository.
32type CopyURLMsg struct{}
33
34// RepoMsg is a message that contains a git.Repository.
35type RepoMsg proto.Repository //nolint:revive
36
37// GoBackMsg is a message to go back to the previous view.
38type GoBackMsg struct{}
39
40// CopyMsg is a message to indicate copied text.
41type CopyMsg struct {
42 Text string
43 Message string
44}
45
46// SwitchTabMsg is a message to switch tabs.
47type SwitchTabMsg common.TabComponent
48
49// Repo is a view for a git repository.
50type Repo struct {
51 common common.Common
52 selectedRepo proto.Repository
53 activeTab int
54 tabs *tabs.Tabs
55 statusbar *statusbar.Model
56 panes []common.TabComponent
57 ref *git.Reference
58 state state
59 spinner spinner.Model
60 panesReady []bool
61}
62
63// New returns a new Repo.
64func New(c common.Common, comps ...common.TabComponent) *Repo {
65 sb := statusbar.New(c)
66 ts := make([]string, 0)
67 for _, c := range comps {
68 ts = append(ts, c.TabName())
69 }
70 c.Logger = c.Logger.WithPrefix("ui.repo")
71 tb := tabs.New(c, ts)
72 // Make sure the order matches the order of tab constants above.
73 s := spinner.New(spinner.WithSpinner(spinner.Dot),
74 spinner.WithStyle(c.Styles.Spinner))
75 r := &Repo{
76 common: c,
77 tabs: tb,
78 statusbar: sb,
79 panes: comps,
80 state: loadingState,
81 spinner: s,
82 panesReady: make([]bool, len(comps)),
83 }
84 return r
85}
86
87func (r *Repo) getMargins() (int, int) {
88 hh := lipgloss.Height(r.headerView())
89 hm := r.common.Styles.Repo.Body.GetVerticalFrameSize() +
90 hh +
91 r.common.Styles.Repo.Header.GetVerticalFrameSize() +
92 r.common.Styles.StatusBar.GetHeight()
93 return 0, hm
94}
95
96// SetSize implements common.Component.
97func (r *Repo) SetSize(width, height int) {
98 r.common.SetSize(width, height)
99 _, hm := r.getMargins()
100 r.tabs.SetSize(width, height-hm)
101 r.statusbar.SetSize(width, height-hm)
102 for _, p := range r.panes {
103 p.SetSize(width, height-hm)
104 }
105}
106
107// Path returns the current component path.
108func (r *Repo) Path() string {
109 return r.panes[r.activeTab].Path()
110}
111
112func (r *Repo) commonHelp() []key.Binding {
113 b := make([]key.Binding, 0)
114 back := r.common.KeyMap.Back
115 back.SetHelp("esc", "back to menu")
116 tab := r.common.KeyMap.Section
117 tab.SetHelp("tab", "switch tab")
118 b = append(b, back)
119 b = append(b, tab)
120 return b
121}
122
123// ShortHelp implements help.KeyMap.
124func (r *Repo) ShortHelp() []key.Binding {
125 b := r.commonHelp()
126 b = append(b, r.panes[r.activeTab].(help.KeyMap).ShortHelp()...)
127 return b
128}
129
130// FullHelp implements help.KeyMap.
131func (r *Repo) FullHelp() [][]key.Binding {
132 b := make([][]key.Binding, 0)
133 b = append(b, r.commonHelp())
134 b = append(b, r.panes[r.activeTab].(help.KeyMap).FullHelp()...)
135 return b
136}
137
138// Init implements tea.View.
139func (r *Repo) Init() tea.Cmd {
140 r.state = loadingState
141 r.activeTab = 0
142 return tea.Batch(
143 r.tabs.Init(),
144 r.statusbar.Init(),
145 r.spinner.Tick,
146 )
147}
148
149// Update implements tea.Model.
150func (r *Repo) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
151 cmds := make([]tea.Cmd, 0)
152 switch msg := msg.(type) {
153 case RepoMsg:
154 // Set the state to loading when we get a new repository.
155 r.selectedRepo = msg
156 cmds = append(cmds,
157 r.Init(),
158 // This will set the selected repo in each pane's model.
159 r.updateModels(msg),
160 )
161 case RefMsg:
162 r.ref = msg
163 cmds = append(cmds, r.updateModels(msg))
164 r.state = readyState
165 case tabs.SelectTabMsg:
166 r.activeTab = int(msg)
167 t, cmd := r.tabs.Update(msg)
168 r.tabs = t.(*tabs.Tabs)
169 if cmd != nil {
170 cmds = append(cmds, cmd)
171 }
172 case tabs.ActiveTabMsg:
173 r.activeTab = int(msg)
174 case tea.KeyPressMsg, tea.MouseClickMsg:
175 t, cmd := r.tabs.Update(msg)
176 r.tabs = t.(*tabs.Tabs)
177 if cmd != nil {
178 cmds = append(cmds, cmd)
179 }
180 if r.selectedRepo != nil {
181 urlID := fmt.Sprintf("%s-url", r.selectedRepo.Name())
182 cmd := r.common.CloneCmd(r.common.Config().SSH.PublicURL, r.selectedRepo.Name())
183 if msg, ok := msg.(tea.MouseMsg); ok && r.common.Zone.Get(urlID).InBounds(msg) {
184 cmds = append(cmds, copyCmd(cmd, "Command copied to clipboard"))
185 }
186 }
187 switch msg := msg.(type) {
188 case tea.MouseClickMsg:
189 switch msg.Button {
190 case tea.MouseLeft:
191 switch {
192 case r.common.Zone.Get("repo-help").InBounds(msg):
193 cmds = append(cmds, footer.ToggleFooterCmd)
194 }
195 case tea.MouseRight:
196 switch {
197 case r.common.Zone.Get("repo-main").InBounds(msg):
198 cmds = append(cmds, goBackCmd)
199 }
200 default:
201 // Handle other mouse buttons
202 }
203 }
204 switch msg := msg.(type) {
205 case tea.KeyPressMsg:
206 switch {
207 case key.Matches(msg, r.common.KeyMap.Back):
208 cmds = append(cmds, goBackCmd)
209 }
210 }
211 case CopyMsg:
212 txt := msg.Text
213 if cfg := r.common.Config(); cfg != nil {
214 cmds = append(cmds, tea.SetClipboard(txt))
215 }
216 r.statusbar.SetStatus("", msg.Message, "", "")
217 case ReadmeMsg:
218 cmds = append(cmds, r.updateTabComponent(&Readme{}, msg))
219 case FileItemsMsg, FileContentMsg:
220 cmds = append(cmds, r.updateTabComponent(&Files{}, msg))
221 case LogItemsMsg, LogDiffMsg, LogCountMsg:
222 cmds = append(cmds, r.updateTabComponent(&Log{}, msg))
223 case RefItemsMsg:
224 cmds = append(cmds, r.updateTabComponent(&Refs{refPrefix: msg.prefix}, msg))
225 case StashListMsg, StashPatchMsg:
226 cmds = append(cmds, r.updateTabComponent(&Stash{}, msg))
227 // We have two spinners, one is used to when loading the repository and the
228 // other is used when loading the log.
229 // Check if the spinner ID matches the spinner model.
230 case spinner.TickMsg:
231 if r.state == loadingState && r.spinner.ID() == msg.ID {
232 s, cmd := r.spinner.Update(msg)
233 r.spinner = s
234 if cmd != nil {
235 cmds = append(cmds, cmd)
236 }
237 } else {
238 for i, c := range r.panes {
239 if c.SpinnerID() == msg.ID {
240 m, cmd := c.Update(msg)
241 r.panes[i] = m.(common.TabComponent)
242 if cmd != nil {
243 cmds = append(cmds, cmd)
244 }
245 break
246 }
247 }
248 }
249 case tea.WindowSizeMsg:
250 r.SetSize(msg.Width, msg.Height)
251 cmds = append(cmds, r.updateModels(msg))
252 case EmptyRepoMsg:
253 r.ref = nil
254 r.state = readyState
255 cmds = append(cmds, r.updateModels(msg))
256 case common.ErrorMsg:
257 r.state = readyState
258 case SwitchTabMsg:
259 for i, c := range r.panes {
260 if c.TabName() == msg.TabName() {
261 cmds = append(cmds, tabs.SelectTabCmd(i))
262 break
263 }
264 }
265 }
266 active := r.panes[r.activeTab]
267 m, cmd := active.Update(msg)
268 r.panes[r.activeTab] = m.(common.TabComponent)
269 if cmd != nil {
270 cmds = append(cmds, cmd)
271 }
272
273 // Update the status bar on these events
274 // Must come after we've updated the active tab
275 switch msg.(type) {
276 case RepoMsg, RefMsg, tabs.ActiveTabMsg, tea.KeyPressMsg,
277 tea.MouseClickMsg, tea.MouseWheelMsg, FileItemsMsg, FileContentMsg,
278 FileBlameMsg, selector.ActiveMsg, LogItemsMsg, GoBackMsg, LogDiffMsg,
279 EmptyRepoMsg, StashListMsg, StashPatchMsg:
280 r.setStatusBarInfo()
281 }
282
283 s, cmd := r.statusbar.Update(msg)
284 r.statusbar = s.(*statusbar.Model)
285 if cmd != nil {
286 cmds = append(cmds, cmd)
287 }
288
289 return r, tea.Batch(cmds...)
290}
291
292// View implements tea.Model.
293func (r *Repo) View() string {
294 wm, hm := r.getMargins()
295 hm += r.common.Styles.Tabs.GetHeight() +
296 r.common.Styles.Tabs.GetVerticalFrameSize()
297 s := r.common.Styles.Repo.Base.
298 Width(r.common.Width - wm).
299 Height(r.common.Height - hm)
300 mainStyle := r.common.Styles.Repo.Body.
301 Height(r.common.Height - hm)
302 var main string
303 var statusbar string
304 switch r.state {
305 case loadingState:
306 main = fmt.Sprintf("%s loading…", r.spinner.View())
307 case readyState:
308 main = r.panes[r.activeTab].View()
309 statusbar = r.statusbar.View()
310 }
311 main = r.common.Zone.Mark(
312 "repo-main",
313 mainStyle.Render(main),
314 )
315 view := lipgloss.JoinVertical(lipgloss.Left,
316 r.headerView(),
317 r.tabs.View(),
318 main,
319 statusbar,
320 )
321 return s.Render(view)
322}
323
324func (r *Repo) headerView() string {
325 if r.selectedRepo == nil {
326 return ""
327 }
328 truncate := lipgloss.NewStyle().MaxWidth(r.common.Width)
329 header := r.selectedRepo.ProjectName()
330 if header == "" {
331 header = r.selectedRepo.Name()
332 }
333 header = r.common.Styles.Repo.HeaderName.Render(header)
334 desc := strings.TrimSpace(r.selectedRepo.Description())
335 if desc != "" {
336 header = lipgloss.JoinVertical(lipgloss.Left,
337 header,
338 r.common.Styles.Repo.HeaderDesc.Render(desc),
339 )
340 }
341 urlStyle := r.common.Styles.URLStyle.
342 Width(r.common.Width - lipgloss.Width(header) - 1).
343 Align(lipgloss.Right)
344 var url string
345 if cfg := r.common.Config(); cfg != nil {
346 url = r.common.CloneCmd(cfg.SSH.PublicURL, r.selectedRepo.Name())
347 }
348 url = common.TruncateString(url, r.common.Width-lipgloss.Width(header)-1)
349 url = r.common.Zone.Mark(
350 fmt.Sprintf("%s-url", r.selectedRepo.Name()),
351 urlStyle.Render(url),
352 )
353
354 header = lipgloss.JoinHorizontal(lipgloss.Top, header, url)
355
356 style := r.common.Styles.Repo.Header.Width(r.common.Width)
357 return style.Render(
358 truncate.Render(header),
359 )
360}
361
362func (r *Repo) setStatusBarInfo() {
363 if r.selectedRepo == nil {
364 return
365 }
366
367 active := r.panes[r.activeTab]
368 key := r.selectedRepo.Name()
369 value := active.StatusBarValue()
370 info := active.StatusBarInfo()
371 extra := "*"
372 if r.ref != nil {
373 extra += " " + r.ref.Name().Short()
374 }
375
376 r.statusbar.SetStatus(key, value, info, extra)
377}
378
379func (r *Repo) updateTabComponent(c common.TabComponent, msg tea.Msg) tea.Cmd {
380 cmds := make([]tea.Cmd, 0)
381 for i, b := range r.panes {
382 if b.TabName() == c.TabName() {
383 m, cmd := b.Update(msg)
384 r.panes[i] = m.(common.TabComponent)
385 if cmd != nil {
386 cmds = append(cmds, cmd)
387 }
388 break
389 }
390 }
391 return tea.Batch(cmds...)
392}
393
394func (r *Repo) updateModels(msg tea.Msg) tea.Cmd {
395 cmds := make([]tea.Cmd, 0)
396 for i, b := range r.panes {
397 m, cmd := b.Update(msg)
398 r.panes[i] = m.(common.TabComponent)
399 if cmd != nil {
400 cmds = append(cmds, cmd)
401 }
402 }
403 return tea.Batch(cmds...)
404}
405
406func copyCmd(text, msg string) tea.Cmd {
407 return func() tea.Msg {
408 return CopyMsg{
409 Text: text,
410 Message: msg,
411 }
412 }
413}
414
415func goBackCmd() tea.Msg {
416 return GoBackMsg{}
417}
418
419func switchTabCmd(m common.TabComponent) tea.Cmd {
420 return func() tea.Msg {
421 return SwitchTabMsg(m)
422 }
423}
424
425func renderLoading(c common.Common, s spinner.Model) string {
426 msg := fmt.Sprintf("%s loading…", s.View())
427 return c.Styles.SpinnerContainer.
428 Height(c.Height).
429 Render(msg)
430}