diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2019-11-16 16:38:38 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2019-11-21 22:07:14 +1100 |
commit | 61dac10bb94874800d7b955c875cbcc3b3efa546 (patch) | |
tree | 8a70a7645b60eeaf31b4725525dff112af581bb3 | |
parent | b5385f2560d10fe74af34d9b09c7ebbfe0a46424 (diff) |
support navigating remotes view
-rw-r--r-- | pkg/gui/branches_panel.go | 18 | ||||
-rw-r--r-- | pkg/gui/gui.go | 25 | ||||
-rw-r--r-- | pkg/gui/keybindings.go | 7 | ||||
-rw-r--r-- | pkg/gui/list_view.go | 9 | ||||
-rw-r--r-- | pkg/gui/remotes_panel.go | 70 | ||||
-rw-r--r-- | pkg/gui/view_helpers.go | 11 |
6 files changed, 112 insertions, 28 deletions
diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go index a05d3ed2a..cd1a9a69b 100644 --- a/pkg/gui/branches_panel.go +++ b/pkg/gui/branches_panel.go @@ -355,24 +355,6 @@ func (gui *Gui) onBranchesTabClick(tabIndex int) error { return nil } -// gui.refreshStatus is called at the end of this because that's when we can -// be sure there is a state.Branches array to pick the current branch from -func (gui *Gui) refreshRemotes() error { - remotes, err := gui.GitCommand.GetRemotes() - if err != nil { - return gui.createErrorPanel(gui.g, err.Error()) - } - - gui.State.Remotes = remotes - - gui.g.Update(func(g *gocui.Gui) error { - gui.refreshSelectedLine(&gui.State.Panels.Remotes.SelectedLine, len(gui.State.Remotes)) - return nil - }) - - return nil -} - func (gui *Gui) handleNextBranchesTab(g *gocui.Gui, v *gocui.View) error { return gui.onBranchesTabClick( utils.ModuloWithWrap(v.TabIndex+1, len(v.Tabs)), diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 42d79647f..e8056f858 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -190,7 +190,7 @@ func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *comma Panels: &panelStates{ Files: &filePanelState{SelectedLine: -1}, Branches: &branchPanelState{SelectedLine: 0}, - Remotes: &remotePanelState{SelectedLine: -1}, + Remotes: &remotePanelState{SelectedLine: 0}, Commits: &commitPanelState{SelectedLine: -1}, CommitFiles: &commitFilesPanelState{SelectedLine: -1}, Stash: &stashPanelState{SelectedLine: -1}, @@ -598,22 +598,29 @@ func (gui *Gui) layout(g *gocui.Gui) error { type listViewState struct { selectedLine int lineCount int + view *gocui.View + context string } - listViews := map[*gocui.View]listViewState{ - filesView: {selectedLine: gui.State.Panels.Files.SelectedLine, lineCount: len(gui.State.Files)}, - branchesView: {selectedLine: gui.State.Panels.Branches.SelectedLine, lineCount: len(gui.State.Branches)}, - commitsView: {selectedLine: gui.State.Panels.Commits.SelectedLine, lineCount: len(gui.State.Commits)}, - stashView: {selectedLine: gui.State.Panels.Stash.SelectedLine, lineCount: len(gui.State.StashEntries)}, + listViews := []listViewState{ + {view: filesView, context: "", selectedLine: gui.State.Panels.Files.SelectedLine, lineCount: len(gui.State.Files)}, + {view: branchesView, context: "local-branches", selectedLine: gui.State.Panels.Branches.SelectedLine, lineCount: len(gui.State.Branches)}, + {view: branchesView, context: "remotes", selectedLine: gui.State.Panels.Remotes.SelectedLine, lineCount: len(gui.State.Remotes)}, + {view: commitsView, context: "", selectedLine: gui.State.Panels.Commits.SelectedLine, lineCount: len(gui.State.Commits)}, + {view: stashView, context: "", selectedLine: gui.State.Panels.Stash.SelectedLine, lineCount: len(gui.State.StashEntries)}, } // menu view might not exist so we check to be safe if menuView, err := gui.g.View("menu"); err == nil { - listViews[menuView] = listViewState{selectedLine: gui.State.Panels.Menu.SelectedLine, lineCount: gui.State.MenuItemCount} + listViews = append(listViews, listViewState{view: menuView, context: "", selectedLine: gui.State.Panels.Menu.SelectedLine, lineCount: gui.State.MenuItemCount}) } - for view, state := range listViews { + for _, listView := range listViews { + // ignore views where the context doesn't match up with the selected line we're trying to focus + if listView.context != "" && (listView.view.Context != listView.context) { + continue + } // check if the selected line is now out of view and if so refocus it - if err := gui.focusPoint(0, state.selectedLine, state.lineCount, view); err != nil { + if err := gui.focusPoint(0, listView.selectedLine, listView.lineCount, listView.view); err != nil { return err } } diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index ce3dc91c7..3b469d81e 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -1020,6 +1020,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { Handler: gui.handleBranchesClick, }, { + ViewName: "branches", + Contexts: []string{"remotes"}, + Key: gocui.MouseLeft, + Modifier: gocui.ModNone, + Handler: gui.handleRemotesClick, + }, + { ViewName: "commits", Key: gocui.MouseLeft, Modifier: gocui.ModNone, diff --git a/pkg/gui/list_view.go b/pkg/gui/list_view.go index d3f4f8a0c..98dd9414e 100644 --- a/pkg/gui/list_view.go +++ b/pkg/gui/list_view.go @@ -67,6 +67,15 @@ func (gui *Gui) getListViews() []*listView { rendersToMainView: true, }, { + viewName: "branches", + context: "remotes", + getItemsLength: func() int { return len(gui.State.Remotes) }, + getSelectedLine: func() *int { return &gui.State.Panels.Remotes.SelectedLine }, + handleItemSelect: gui.handleRemoteSelect, + gui: gui, + rendersToMainView: true, + }, + { viewName: "commits", getItemsLength: func() int { return len(gui.State.Commits) }, getSelectedLine: func() *int { return &gui.State.Panels.Commits.SelectedLine }, diff --git a/pkg/gui/remotes_panel.go b/pkg/gui/remotes_panel.go new file mode 100644 index 000000000..d40515716 --- /dev/null +++ b/pkg/gui/remotes_panel.go @@ -0,0 +1,70 @@ +package gui + +import ( + "fmt" + "strings" + + "github.com/fatih/color" + "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/commands" + "github.com/jesseduffield/lazygit/pkg/utils" +) + +// list panel functions + +func (gui *Gui) getSelectedRemote() *commands.Remote { + selectedLine := gui.State.Panels.Remotes.SelectedLine + if selectedLine == -1 { + return nil + } + + return gui.State.Remotes[selectedLine] +} + +func (gui *Gui) handleRemotesClick(g *gocui.Gui, v *gocui.View) error { + itemCount := len(gui.State.Remotes) + handleSelect := gui.handleRemoteSelect + selectedLine := &gui.State.Panels.Remotes.SelectedLine + + return gui.handleClick(v, itemCount, selectedLine, handleSelect) +} + +func (gui *Gui) handleRemoteSelect(g *gocui.Gui, v *gocui.View) error { + if gui.popupPanelFocused() { + return nil + } + + gui.State.SplitMainPanel = false + + if _, err := gui.g.SetCurrentView(v.Name()); err != nil { + return err + } + + gui.getMainView().Title = "Remote" + + remote := gui.getSelectedRemote() + gui.focusPoint(0, gui.State.Panels.Menu.SelectedLine, gui.State.MenuItemCount, v) + if err := gui.focusPoint(0, gui.State.Panels.Remotes.SelectedLine, len(gui.State.Remotes), v); err != nil { + return err + } + + return gui.renderString(g, "main", fmt.Sprintf("%s\nUrls:\n%s", utils.ColoredString(remote.Name, color.FgGreen), strings.Join(remote.Urls, "\n"))) +} + +// gui.refreshStatus is called at the end of this because that's when we can +// be sure there is a state.Remotes array to pick the current remote from +func (gui *Gui) refreshRemotes() error { + remotes, err := gui.GitCommand.GetRemotes() + if err != nil { + return gui.createErrorPanel(gui.g, err.Error()) + } + + gui.State.Remotes = remotes + + gui.g.Update(func(g *gocui.Gui) error { + gui.refreshSelectedLine(&gui.State.Panels.Remotes.SelectedLine, len(gui.State.Remotes)) + return nil + }) + + return nil +} diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go index c23bf3e47..076567386 100644 --- a/pkg/gui/view_helpers.go +++ b/pkg/gui/view_helpers.go @@ -5,6 +5,7 @@ import ( "sort" "strings" + "github.com/go-errors/errors" "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/spkg/bom" @@ -103,7 +104,15 @@ func (gui *Gui) newLineFocused(g *gocui.Gui, v *gocui.View) error { case "files": return gui.handleFileSelect(g, v) case "branches": - return gui.handleBranchSelect(g, v) + branchesView := gui.getBranchesView() + switch branchesView.Context { + case "local-branches": + return gui.handleBranchSelect(g, v) + case "remotes": + return gui.handleRemoteSelect(g, v) + default: + return errors.New("unknown branches panel context: " + branchesView.Context) + } case "commits": return gui.handleCommitSelect(g, v) case "commitFiles": |