browse.go

  1package browse
  2
  3import (
  4	"fmt"
  5	"path/filepath"
  6	"time"
  7
  8	"github.com/charmbracelet/bubbles/v2/key"
  9	tea "github.com/charmbracelet/bubbletea/v2"
 10	"github.com/charmbracelet/lipgloss/v2"
 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		ctx := cmd.Context()
 43		c := common.NewCommon(ctx, 0, 0)
 44		c.HideCloneCmd = true
 45		comps := []common.TabComponent{
 46			repo.NewReadme(c),
 47			repo.NewFiles(c),
 48			repo.NewLog(c),
 49		}
 50		if !r.IsBare {
 51			comps = append(comps, repo.NewStash(c))
 52		}
 53		comps = append(comps, repo.NewRefs(c, git.RefsHeads), repo.NewRefs(c, git.RefsTags))
 54		m := &model{
 55			model:  repo.New(c, comps...),
 56			repo:   repository{r},
 57			common: c,
 58		}
 59
 60		m.footer = footer.New(c, m)
 61		p := tea.NewProgram(m,
 62			tea.WithAltScreen(),
 63			tea.WithMouseCellMotion(),
 64		)
 65
 66		_, err = p.Run()
 67		return err
 68	},
 69}
 70
 71type state int
 72
 73const (
 74	startState state = iota
 75	errorState
 76)
 77
 78type model struct {
 79	model      *repo.Repo
 80	footer     *footer.Footer
 81	repo       proto.Repository
 82	common     common.Common
 83	state      state
 84	showFooter bool
 85	error      error
 86}
 87
 88var _ tea.Model = &model{}
 89
 90func (m *model) SetSize(w, h int) {
 91	m.common.SetSize(w, h)
 92	style := m.common.Styles.App
 93	wm := style.GetHorizontalFrameSize()
 94	hm := style.GetVerticalFrameSize()
 95	if m.showFooter {
 96		hm += m.footer.Height()
 97	}
 98
 99	m.footer.SetSize(w-wm, h-hm)
100	m.model.SetSize(w-wm, h-hm)
101}
102
103// ShortHelp implements help.KeyMap.
104func (m model) ShortHelp() []key.Binding {
105	switch m.state {
106	case errorState:
107		return []key.Binding{
108			m.common.KeyMap.Back,
109			m.common.KeyMap.Quit,
110			m.common.KeyMap.Help,
111		}
112	default:
113		return m.model.ShortHelp()
114	}
115}
116
117// FullHelp implements help.KeyMap.
118func (m model) FullHelp() [][]key.Binding {
119	switch m.state {
120	case errorState:
121		return [][]key.Binding{
122			{
123				m.common.KeyMap.Back,
124			},
125			{
126				m.common.KeyMap.Quit,
127				m.common.KeyMap.Help,
128			},
129		}
130	default:
131		return m.model.FullHelp()
132	}
133}
134
135// Init implements tea.Model.
136func (m *model) Init() tea.Cmd {
137	return tea.Batch(
138		m.model.Init(),
139		m.footer.Init(),
140		func() tea.Msg {
141			return repo.RepoMsg(m.repo)
142		},
143		repo.UpdateRefCmd(m.repo),
144	)
145}
146
147// Update implements tea.Model.
148func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
149	m.common.Logger.Debugf("msg received: %T", msg)
150	cmds := make([]tea.Cmd, 0)
151	switch msg := msg.(type) {
152	case tea.WindowSizeMsg:
153		m.SetSize(msg.Width, msg.Height)
154	case tea.KeyMsg:
155		switch {
156		case key.Matches(msg, m.common.KeyMap.Back) && m.error != nil:
157			m.error = nil
158			m.state = startState
159			// Always show the footer on error.
160			m.showFooter = m.footer.ShowAll()
161		case key.Matches(msg, m.common.KeyMap.Help):
162			cmds = append(cmds, footer.ToggleFooterCmd)
163		case key.Matches(msg, m.common.KeyMap.Quit):
164			// Stop bubblezone background workers.
165			m.common.Zone.Close()
166			return m, tea.Quit
167		}
168	case tea.MouseClickMsg:
169		mouse := msg.Mouse()
170		switch mouse.Button {
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
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.
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.Left, 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}