browse.go

  1// Package browse provides the browse command for soft-serve.
  2package browse
  3
  4import (
  5	"fmt"
  6	"path/filepath"
  7	"time"
  8
  9	"github.com/charmbracelet/bubbles/v2/key"
 10	tea "github.com/charmbracelet/bubbletea/v2"
 11	"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/pages/repo"
 17	"github.com/spf13/cobra"
 18)
 19
 20// Command is the browse command.
 21var Command = &cobra.Command{
 22	Use:   "browse PATH",
 23	Short: "Browse a repository",
 24	Args:  cobra.MaximumNArgs(1),
 25	RunE: func(cmd *cobra.Command, args []string) error {
 26		rp := "."
 27		if len(args) > 0 {
 28			rp = args[0]
 29		}
 30
 31		abs, err := filepath.Abs(rp)
 32		if err != nil {
 33			return fmt.Errorf("failed to get absolute path for %s: %w", rp, err)
 34		}
 35
 36		r, err := git.Open(abs)
 37		if err != nil {
 38			return fmt.Errorf("failed to open repository: %w", err)
 39		}
 40
 41		// Bubble Tea uses Termenv default output so we have to use the same
 42		// thing here.
 43		ctx := cmd.Context()
 44		c := common.NewCommon(ctx, 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 //nolint:wrapcheck
 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
 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 { //nolint:exhaustive
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 { //nolint:exhaustive
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.KeyPressMsg:
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.MouseClickMsg:
170		mouse := msg.Mouse()
171		switch mouse.Button {
172		case tea.MouseLeft:
173			switch {
174			case m.common.Zone.Get("footer").InBounds(msg):
175				cmds = append(cmds, footer.ToggleFooterCmd)
176			}
177		}
178	case footer.ToggleFooterMsg:
179		m.footer.SetShowAll(!m.footer.ShowAll())
180		m.showFooter = !m.showFooter
181	case common.ErrorMsg:
182		m.error = msg
183		m.state = errorState
184		m.showFooter = true
185	}
186
187	f, cmd := m.footer.Update(msg)
188	m.footer = f.(*footer.Footer)
189	if cmd != nil {
190		cmds = append(cmds, cmd)
191	}
192
193	r, cmd := m.model.Update(msg)
194	m.model = r.(*repo.Repo)
195	if cmd != nil {
196		cmds = append(cmds, cmd)
197	}
198
199	// This fixes determining the height margin of the footer.
200	m.SetSize(m.common.Width, m.common.Height)
201
202	return m, tea.Batch(cmds...)
203}
204
205// View implements tea.Model.
206func (m *model) View() string {
207	style := m.common.Styles.App
208	wm, hm := style.GetHorizontalFrameSize(), style.GetVerticalFrameSize()
209	if m.showFooter {
210		hm += m.footer.Height()
211	}
212
213	var view string
214	switch m.state {
215	case startState:
216		view = m.model.View()
217	case errorState:
218		err := m.common.Styles.ErrorTitle.Render("Bummer")
219		err += m.common.Styles.ErrorBody.Render(m.error.Error())
220		view = m.common.Styles.Error.
221			Width(m.common.Width -
222				wm -
223				m.common.Styles.ErrorBody.GetHorizontalFrameSize()).
224			Height(m.common.Height -
225				hm -
226				m.common.Styles.Error.GetVerticalFrameSize()).
227			Render(err)
228	}
229
230	if m.showFooter {
231		view = lipgloss.JoinVertical(lipgloss.Left, view, m.footer.View())
232	}
233
234	return m.common.Zone.Scan(style.Render(view))
235}
236
237type repository struct {
238	r *git.Repository
239}
240
241var _ proto.Repository = repository{}
242
243// Description implements proto.Repository.
244func (r repository) Description() string {
245	return ""
246}
247
248// ID implements proto.Repository.
249func (r repository) ID() int64 {
250	return 0
251}
252
253// IsHidden implements proto.Repository.
254func (repository) IsHidden() bool {
255	return false
256}
257
258// IsMirror implements proto.Repository.
259func (repository) IsMirror() bool {
260	return false
261}
262
263// IsPrivate implements proto.Repository.
264func (repository) IsPrivate() bool {
265	return false
266}
267
268// Name implements proto.Repository.
269func (r repository) Name() string {
270	return filepath.Base(r.r.Path)
271}
272
273// Open implements proto.Repository.
274func (r repository) Open() (*git.Repository, error) {
275	return r.r, nil
276}
277
278// ProjectName implements proto.Repository.
279func (r repository) ProjectName() string {
280	return r.Name()
281}
282
283// UpdatedAt implements proto.Repository.
284func (r repository) UpdatedAt() time.Time {
285	t, err := r.r.LatestCommitTime()
286	if err != nil {
287		return time.Time{}
288	}
289
290	return t
291}
292
293// UserID implements proto.Repository.
294func (r repository) UserID() int64 {
295	return 0
296}
297
298// CreatedAt implements proto.Repository.
299func (r repository) CreatedAt() time.Time {
300	return time.Time{}
301}