1package browse
2
3import (
4 "fmt"
5 "path/filepath"
6 "time"
7
8 "github.com/charmbracelet/bubbles/key"
9 tea "github.com/charmbracelet/bubbletea"
10 "github.com/charmbracelet/lipgloss"
11 "github.com/charmbracelet/soft-serve/git"
12 "github.com/charmbracelet/soft-serve/pkg/proto"
13 "github.com/charmbracelet/soft-serve/pkg/ui/common"
14 "github.com/charmbracelet/soft-serve/pkg/ui/components/footer"
15 "github.com/charmbracelet/soft-serve/pkg/ui/pages/repo"
16 "github.com/spf13/cobra"
17)
18
19// Command is the browse command.
20var Command = &cobra.Command{
21 Use: "browse PATH",
22 Short: "Browse a repository",
23 Args: cobra.MaximumNArgs(1),
24 RunE: func(cmd *cobra.Command, args []string) error {
25 rp := "."
26 if len(args) > 0 {
27 rp = args[0]
28 }
29
30 abs, err := filepath.Abs(rp)
31 if err != nil {
32 return err
33 }
34
35 r, err := git.Open(abs)
36 if err != nil {
37 return fmt.Errorf("failed to open repository: %w", err)
38 }
39
40 // Bubble Tea uses Termenv default output so we have to use the same
41 // thing here.
42 output := lipgloss.DefaultRenderer()
43 ctx := cmd.Context()
44 c := common.NewCommon(ctx, output, 0, 0)
45 c.HideCloneCmd = true
46 comps := []common.TabComponent{
47 repo.NewReadme(c),
48 repo.NewFiles(c),
49 repo.NewLog(c),
50 }
51 if !r.IsBare {
52 comps = append(comps, repo.NewStash(c))
53 }
54 comps = append(comps, repo.NewRefs(c, git.RefsHeads), repo.NewRefs(c, git.RefsTags))
55 m := &model{
56 model: repo.New(c, comps...),
57 repo: repository{r},
58 common: c,
59 }
60
61 m.footer = footer.New(c, m)
62 p := tea.NewProgram(m,
63 tea.WithAltScreen(),
64 tea.WithMouseCellMotion(),
65 )
66
67 _, err = p.Run()
68 return err
69 },
70}
71
72type state int
73
74const (
75 startState state = iota
76 errorState
77)
78
79type model struct {
80 model *repo.Repo
81 footer *footer.Footer
82 repo proto.Repository
83 common common.Common
84 state state
85 showFooter bool
86 error error
87}
88
89var _ tea.Model = &model{}
90
91func (m *model) SetSize(w, h int) {
92 m.common.SetSize(w, h)
93 style := m.common.Styles.App.Copy()
94 wm := style.GetHorizontalFrameSize()
95 hm := style.GetVerticalFrameSize()
96 if m.showFooter {
97 hm += m.footer.Height()
98 }
99
100 m.footer.SetSize(w-wm, h-hm)
101 m.model.SetSize(w-wm, h-hm)
102}
103
104// ShortHelp implements help.KeyMap.
105func (m model) ShortHelp() []key.Binding {
106 switch m.state {
107 case errorState:
108 return []key.Binding{
109 m.common.KeyMap.Back,
110 m.common.KeyMap.Quit,
111 m.common.KeyMap.Help,
112 }
113 default:
114 return m.model.ShortHelp()
115 }
116}
117
118// FullHelp implements help.KeyMap.
119func (m model) FullHelp() [][]key.Binding {
120 switch m.state {
121 case errorState:
122 return [][]key.Binding{
123 {
124 m.common.KeyMap.Back,
125 },
126 {
127 m.common.KeyMap.Quit,
128 m.common.KeyMap.Help,
129 },
130 }
131 default:
132 return m.model.FullHelp()
133 }
134}
135
136// Init implements tea.Model.
137func (m *model) Init() tea.Cmd {
138 return tea.Batch(
139 m.model.Init(),
140 m.footer.Init(),
141 func() tea.Msg {
142 return repo.RepoMsg(m.repo)
143 },
144 repo.UpdateRefCmd(m.repo),
145 )
146}
147
148// Update implements tea.Model.
149func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
150 m.common.Logger.Debugf("msg received: %T", msg)
151 cmds := make([]tea.Cmd, 0)
152 switch msg := msg.(type) {
153 case tea.WindowSizeMsg:
154 m.SetSize(msg.Width, msg.Height)
155 case tea.KeyMsg:
156 switch {
157 case key.Matches(msg, m.common.KeyMap.Back) && m.error != nil:
158 m.error = nil
159 m.state = startState
160 // Always show the footer on error.
161 m.showFooter = m.footer.ShowAll()
162 case key.Matches(msg, m.common.KeyMap.Help):
163 cmds = append(cmds, footer.ToggleFooterCmd)
164 case key.Matches(msg, m.common.KeyMap.Quit):
165 // Stop bubblezone background workers.
166 m.common.Zone.Close()
167 return m, tea.Quit
168 }
169 case tea.MouseMsg:
170 switch msg.Type {
171 case tea.MouseLeft:
172 switch {
173 case m.common.Zone.Get("footer").InBounds(msg):
174 cmds = append(cmds, footer.ToggleFooterCmd)
175 }
176 }
177 case footer.ToggleFooterMsg:
178 m.footer.SetShowAll(!m.footer.ShowAll())
179 m.showFooter = !m.showFooter
180 case common.ErrorMsg:
181 m.error = msg
182 m.state = errorState
183 m.showFooter = true
184 }
185
186 f, cmd := m.footer.Update(msg)
187 m.footer = f.(*footer.Footer)
188 if cmd != nil {
189 cmds = append(cmds, cmd)
190 }
191
192 r, cmd := m.model.Update(msg)
193 m.model = r.(*repo.Repo)
194 if cmd != nil {
195 cmds = append(cmds, cmd)
196 }
197
198 // This fixes determining the height margin of the footer.
199 m.SetSize(m.common.Width, m.common.Height)
200
201 return m, tea.Batch(cmds...)
202}
203
204// View implements tea.Model.
205func (m *model) View() string {
206 style := m.common.Styles.App.Copy()
207 wm, hm := style.GetHorizontalFrameSize(), style.GetVerticalFrameSize()
208 if m.showFooter {
209 hm += m.footer.Height()
210 }
211
212 var view string
213 switch m.state {
214 case startState:
215 view = m.model.View()
216 case errorState:
217 err := m.common.Styles.ErrorTitle.Render("Bummer")
218 err += m.common.Styles.ErrorBody.Render(m.error.Error())
219 view = m.common.Styles.Error.Copy().
220 Width(m.common.Width -
221 wm -
222 m.common.Styles.ErrorBody.GetHorizontalFrameSize()).
223 Height(m.common.Height -
224 hm -
225 m.common.Styles.Error.GetVerticalFrameSize()).
226 Render(err)
227 }
228
229 if m.showFooter {
230 view = lipgloss.JoinVertical(lipgloss.Top, view, m.footer.View())
231 }
232
233 return m.common.Zone.Scan(style.Render(view))
234}
235
236type repository struct {
237 r *git.Repository
238}
239
240var _ proto.Repository = repository{}
241
242// Description implements proto.Repository.
243func (r repository) Description() string {
244 return ""
245}
246
247// ID implements proto.Repository.
248func (r repository) ID() int64 {
249 return 0
250}
251
252// IsHidden implements proto.Repository.
253func (repository) IsHidden() bool {
254 return false
255}
256
257// IsMirror implements proto.Repository.
258func (repository) IsMirror() bool {
259 return false
260}
261
262// IsPrivate implements proto.Repository.
263func (repository) IsPrivate() bool {
264 return false
265}
266
267// Name implements proto.Repository.
268func (r repository) Name() string {
269 return filepath.Base(r.r.Path)
270}
271
272// Open implements proto.Repository.
273func (r repository) Open() (*git.Repository, error) {
274 return r.r, nil
275}
276
277// ProjectName implements proto.Repository.
278func (r repository) ProjectName() string {
279 return r.Name()
280}
281
282// UpdatedAt implements proto.Repository.
283func (r repository) UpdatedAt() time.Time {
284 t, err := r.r.LatestCommitTime()
285 if err != nil {
286 return time.Time{}
287 }
288
289 return t
290}
291
292// UserID implements proto.Repository.
293func (r repository) UserID() int64 {
294 return 0
295}
296
297// CreatedAt implements proto.Repository.
298func (r repository) CreatedAt() time.Time {
299 return time.Time{}
300}