1package repo
2
3import (
4 "fmt"
5
6 "github.com/charmbracelet/bubbles/help"
7 "github.com/charmbracelet/bubbles/key"
8 "github.com/charmbracelet/bubbles/spinner"
9 tea "github.com/charmbracelet/bubbletea"
10 "github.com/charmbracelet/lipgloss"
11 "github.com/charmbracelet/soft-serve/config"
12 ggit "github.com/charmbracelet/soft-serve/git"
13 "github.com/charmbracelet/soft-serve/ui/common"
14 "github.com/charmbracelet/soft-serve/ui/components/statusbar"
15 "github.com/charmbracelet/soft-serve/ui/components/tabs"
16 "github.com/charmbracelet/soft-serve/ui/git"
17)
18
19type tab int
20
21const (
22 readmeTab tab = iota
23 filesTab
24 commitsTab
25 branchesTab
26 tagsTab
27 lastTab
28)
29
30func (t tab) String() string {
31 return []string{
32 "Readme",
33 "Files",
34 "Commits",
35 "Branches",
36 "Tags",
37 }[t]
38}
39
40// UpdateStatusBarMsg updates the status bar.
41type UpdateStatusBarMsg struct{}
42
43// RepoMsg is a message that contains a git.Repository.
44type RepoMsg git.GitRepo
45
46// RefMsg is a message that contains a git.Reference.
47type RefMsg *ggit.Reference
48
49// Repo is a view for a git repository.
50type Repo struct {
51 common common.Common
52 cfg *config.Config
53 rs git.GitRepoSource
54 selectedRepo git.GitRepo
55 activeTab tab
56 tabs *tabs.Tabs
57 statusbar *statusbar.StatusBar
58 boxes []common.Component
59 ref *ggit.Reference
60}
61
62// New returns a new Repo.
63func New(cfg *config.Config, rs git.GitRepoSource, c common.Common) *Repo {
64 sb := statusbar.New(c)
65 ts := make([]string, lastTab)
66 // Tabs must match the order of tab constants above.
67 for i, t := range []tab{readmeTab, filesTab, commitsTab, branchesTab, tagsTab} {
68 ts[i] = t.String()
69 }
70 tb := tabs.New(c, ts)
71 readme := NewReadme(c)
72 log := NewLog(c)
73 files := NewFiles(c)
74 branches := NewRefs(c, ggit.RefsHeads)
75 tags := NewRefs(c, ggit.RefsTags)
76 // Make sure the order matches the order of tab constants above.
77 boxes := []common.Component{
78 readme,
79 files,
80 log,
81 branches,
82 tags,
83 }
84 r := &Repo{
85 cfg: cfg,
86 common: c,
87 rs: rs,
88 tabs: tb,
89 statusbar: sb,
90 boxes: boxes,
91 }
92 return r
93}
94
95// SetSize implements common.Component.
96func (r *Repo) SetSize(width, height int) {
97 r.common.SetSize(width, height)
98 hm := r.common.Styles.RepoBody.GetVerticalFrameSize() +
99 r.common.Styles.RepoHeader.GetHeight() +
100 r.common.Styles.RepoHeader.GetVerticalFrameSize() +
101 r.common.Styles.StatusBar.GetHeight() +
102 r.common.Styles.Tabs.GetHeight() +
103 r.common.Styles.Tabs.GetVerticalFrameSize()
104 r.tabs.SetSize(width, height-hm)
105 r.statusbar.SetSize(width, height-hm)
106 for _, b := range r.boxes {
107 b.SetSize(width, height-hm)
108 }
109}
110
111func (r *Repo) commonHelp() []key.Binding {
112 b := make([]key.Binding, 0)
113 back := r.common.KeyMap.Back
114 back.SetHelp("esc", "back to menu")
115 tab := r.common.KeyMap.Section
116 tab.SetHelp("tab", "switch tab")
117 b = append(b, back)
118 b = append(b, tab)
119 return b
120}
121
122// ShortHelp implements help.KeyMap.
123func (r *Repo) ShortHelp() []key.Binding {
124 b := r.commonHelp()
125 b = append(b, r.boxes[r.activeTab].(help.KeyMap).ShortHelp()...)
126 return b
127}
128
129// FullHelp implements help.KeyMap.
130func (r *Repo) FullHelp() [][]key.Binding {
131 b := make([][]key.Binding, 0)
132 b = append(b, r.commonHelp())
133 b = append(b, r.boxes[r.activeTab].(help.KeyMap).FullHelp()...)
134 return b
135}
136
137// Init implements tea.View.
138func (r *Repo) Init() tea.Cmd {
139 return tea.Batch(
140 r.tabs.Init(),
141 r.statusbar.Init(),
142 )
143}
144
145// Update implements tea.Model.
146func (r *Repo) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
147 cmds := make([]tea.Cmd, 0)
148 switch msg := msg.(type) {
149 case RepoMsg:
150 r.activeTab = 0
151 r.selectedRepo = git.GitRepo(msg)
152 cmds = append(cmds,
153 r.tabs.Init(),
154 r.updateRefCmd,
155 r.updateModels(msg),
156 )
157 case RefMsg:
158 r.ref = msg
159 for _, b := range r.boxes {
160 cmds = append(cmds, b.Init())
161 }
162 cmds = append(cmds,
163 r.updateStatusBarCmd,
164 r.updateModels(msg),
165 )
166 case tabs.SelectTabMsg:
167 r.activeTab = tab(msg)
168 t, cmd := r.tabs.Update(msg)
169 r.tabs = t.(*tabs.Tabs)
170 if cmd != nil {
171 cmds = append(cmds, cmd)
172 }
173 case tabs.ActiveTabMsg:
174 r.activeTab = tab(msg)
175 if r.selectedRepo != nil {
176 cmds = append(cmds, r.updateStatusBarCmd)
177 }
178 case tea.KeyMsg, tea.MouseMsg:
179 t, cmd := r.tabs.Update(msg)
180 r.tabs = t.(*tabs.Tabs)
181 if cmd != nil {
182 cmds = append(cmds, cmd)
183 }
184 if r.selectedRepo != nil {
185 cmds = append(cmds, r.updateStatusBarCmd)
186 }
187 case FileItemsMsg:
188 f, cmd := r.boxes[filesTab].Update(msg)
189 r.boxes[filesTab] = f.(*Files)
190 if cmd != nil {
191 cmds = append(cmds, cmd)
192 }
193 // The Log bubble is the only bubble that uses a spinner, so this is fine
194 // for now. We need to pass the TickMsg to the Log bubble when the Log is
195 // loading but not the current selected tab so that the spinner works.
196 case LogCountMsg, LogItemsMsg, spinner.TickMsg:
197 l, cmd := r.boxes[commitsTab].Update(msg)
198 r.boxes[commitsTab] = l.(*Log)
199 if cmd != nil {
200 cmds = append(cmds, cmd)
201 }
202 case RefItemsMsg:
203 switch msg.prefix {
204 case ggit.RefsHeads:
205 b, cmd := r.boxes[branchesTab].Update(msg)
206 r.boxes[branchesTab] = b.(*Refs)
207 if cmd != nil {
208 cmds = append(cmds, cmd)
209 }
210 case ggit.RefsTags:
211 t, cmd := r.boxes[tagsTab].Update(msg)
212 r.boxes[tagsTab] = t.(*Refs)
213 if cmd != nil {
214 cmds = append(cmds, cmd)
215 }
216 }
217 case UpdateStatusBarMsg:
218 cmds = append(cmds, r.updateStatusBarCmd)
219 case tea.WindowSizeMsg:
220 cmds = append(cmds, r.updateModels(msg))
221 }
222 s, cmd := r.statusbar.Update(msg)
223 r.statusbar = s.(*statusbar.StatusBar)
224 if cmd != nil {
225 cmds = append(cmds, cmd)
226 }
227 m, cmd := r.boxes[r.activeTab].Update(msg)
228 r.boxes[r.activeTab] = m.(common.Component)
229 if cmd != nil {
230 cmds = append(cmds, cmd)
231 }
232 return r, tea.Batch(cmds...)
233}
234
235// View implements tea.Model.
236func (r *Repo) View() string {
237 s := r.common.Styles.Repo.Copy().
238 Width(r.common.Width).
239 Height(r.common.Height)
240 repoBodyStyle := r.common.Styles.RepoBody.Copy()
241 hm := repoBodyStyle.GetVerticalFrameSize() +
242 r.common.Styles.RepoHeader.GetHeight() +
243 r.common.Styles.RepoHeader.GetVerticalFrameSize() +
244 r.common.Styles.StatusBar.GetHeight() +
245 r.common.Styles.Tabs.GetHeight() +
246 r.common.Styles.Tabs.GetVerticalFrameSize()
247 mainStyle := repoBodyStyle.
248 Height(r.common.Height - hm)
249 main := r.boxes[r.activeTab].View()
250 view := lipgloss.JoinVertical(lipgloss.Top,
251 r.headerView(),
252 r.tabs.View(),
253 mainStyle.Render(main),
254 r.statusbar.View(),
255 )
256 return s.Render(view)
257}
258
259func (r *Repo) headerView() string {
260 if r.selectedRepo == nil {
261 return ""
262 }
263 cfg := r.cfg
264 name := r.common.Styles.RepoHeaderName.Render(r.selectedRepo.Name())
265 url := git.RepoURL(cfg.Host, cfg.Port, r.selectedRepo.Repo())
266 // TODO move this into a style.
267 url = lipgloss.NewStyle().
268 MarginLeft(1).
269 Foreground(lipgloss.Color("168")).
270 Width(r.common.Width - lipgloss.Width(name) - 1).
271 Align(lipgloss.Right).
272 Render(url)
273 desc := r.common.Styles.RepoHeaderDesc.Render(r.selectedRepo.Description())
274 style := r.common.Styles.RepoHeader.Copy().Width(r.common.Width)
275 return style.Render(
276 lipgloss.JoinVertical(lipgloss.Top,
277 lipgloss.JoinHorizontal(lipgloss.Left,
278 name,
279 url,
280 ),
281 desc,
282 ),
283 )
284}
285
286func (r *Repo) updateStatusBarCmd() tea.Msg {
287 if r.selectedRepo == nil {
288 return nil
289 }
290 value := r.boxes[r.activeTab].(statusbar.Model).StatusBarValue()
291 info := r.boxes[r.activeTab].(statusbar.Model).StatusBarInfo()
292 ref := ""
293 if r.ref != nil {
294 ref = r.ref.Name().Short()
295 }
296 return statusbar.StatusBarMsg{
297 Key: r.selectedRepo.Repo(),
298 Value: value,
299 Info: info,
300 Branch: fmt.Sprintf(" %s", ref),
301 }
302}
303
304func (r *Repo) updateRefCmd() tea.Msg {
305 if r.selectedRepo == nil {
306 return nil
307 }
308 head, err := r.selectedRepo.HEAD()
309 if err != nil {
310 return common.ErrorMsg(err)
311 }
312 return RefMsg(head)
313}
314
315func (r *Repo) updateModels(msg tea.Msg) tea.Cmd {
316 cmds := make([]tea.Cmd, 0)
317 for i, b := range r.boxes {
318 m, cmd := b.Update(msg)
319 r.boxes[i] = m.(common.Component)
320 if cmd != nil {
321 cmds = append(cmds, cmd)
322 }
323 }
324 return tea.Batch(cmds...)
325}
326
327func updateStatusBarCmd() tea.Msg {
328 return UpdateStatusBarMsg{}
329}