From e228d7dbe7b65a52a7e65009c39d025592a8249b Mon Sep 17 00:00:00 2001 From: Toby Padilla Date: Sat, 7 Aug 2021 16:21:54 -0500 Subject: [PATCH] Load repo by name --- git/git.go | 14 ++++++++++++++ tui/bubble.go | 7 +------ tui/commands.go | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/git/git.go b/git/git.go index 68783e4b2..87a9658ef 100644 --- a/git/git.go +++ b/git/git.go @@ -1,6 +1,7 @@ package git import ( + "errors" "log" "os" "sort" @@ -11,6 +12,8 @@ import ( "github.com/go-git/go-git/v5/plumbing/object" ) +var ErrMissingRepo = errors.New("missing repo") + type Repo struct { Name string Repository *git.Repository @@ -58,6 +61,17 @@ func (rs *RepoSource) AllRepos() []*Repo { return rs.repos } +func (rs *RepoSource) GetRepo(name string) (*Repo, error) { + rs.mtx.Lock() + defer rs.mtx.Unlock() + for _, r := range rs.repos { + if r.Name == name { + return r, nil + } + } + return nil, ErrMissingRepo +} + func (rs *RepoSource) GetCommits(limit int) []RepoCommit { rs.mtx.Lock() defer rs.mtx.Unlock() diff --git a/tui/bubble.go b/tui/bubble.go index 42fdef6b0..d16ca4bc0 100644 --- a/tui/bubble.go +++ b/tui/bubble.go @@ -83,12 +83,7 @@ func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) { b.width = msg.Width b.height = msg.Height case selection.SelectedMsg: - rmd := b.repos[msg.Index].Readme - b.readmeViewport.Viewport.GotoTop() - b.readmeViewport.Viewport.Height = b.height - verticalPadding - viewportHeightConstant - b.readmeViewport.Viewport.Width = boxLeftWidth - 2 - b.readmeViewport.Viewport.SetContent(rmd) - b.boxes[1] = b.readmeViewport + cmds = append(cmds, b.getRepoCmd(b.repos[msg.Index].Name)) } if b.state == loadedState { ab, cmd := b.boxes[b.activeBox].Update(msg) diff --git a/tui/commands.go b/tui/commands.go index c7c62427a..d1749ae0e 100644 --- a/tui/commands.go +++ b/tui/commands.go @@ -21,6 +21,22 @@ func (b *Bubble) windowChangesCmd() tea.Msg { return windowMsg{} } +func (b *Bubble) getRepoCmd(name string) tea.Cmd { + return func() tea.Msg { + r, err := b.repoSource.GetRepo(name) + if err != nil { + return errMsg{err} + } + b.readmeViewport.Viewport.GotoTop() + b.readmeViewport.Viewport.Height = b.height - verticalPadding - viewportHeightConstant + b.readmeViewport.Viewport.Width = boxLeftWidth - 2 + b.readmeViewport.Viewport.SetContent(r.Readme) + b.boxes[1] = b.readmeViewport + b.activeBox = 1 + return nil + } +} + func (b *Bubble) loadGitCmd() tea.Msg { b.repos = b.repoSource.AllRepos() rs := make([]string, 0)