mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-21 00:30:00 +02:00
extract out some logic for list views
This commit is contained in:
@ -105,34 +105,6 @@ func (gui *Gui) refreshBranches(g *gocui.Gui) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleBranchesNextLine(g *gocui.Gui, v *gocui.View) error {
|
|
||||||
if gui.popupPanelFocused() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
panelState := gui.State.Panels.Branches
|
|
||||||
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.Branches), false)
|
|
||||||
|
|
||||||
if err := gui.resetOrigin(gui.getMainView()); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return gui.handleBranchSelect(gui.g, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gui *Gui) handleBranchesPrevLine(g *gocui.Gui, v *gocui.View) error {
|
|
||||||
if gui.popupPanelFocused() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
panelState := gui.State.Panels.Branches
|
|
||||||
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.Branches), true)
|
|
||||||
|
|
||||||
if err := gui.resetOrigin(gui.getMainView()); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return gui.handleBranchSelect(gui.g, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// specific functions
|
// specific functions
|
||||||
|
|
||||||
func (gui *Gui) handleBranchPress(g *gocui.Gui, v *gocui.View) error {
|
func (gui *Gui) handleBranchPress(g *gocui.Gui, v *gocui.View) error {
|
||||||
@ -364,16 +336,17 @@ func (gui *Gui) handleFastForward(g *gocui.Gui, v *gocui.View) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) onBranchesTabClick(tabIndex int) error {
|
func (gui *Gui) onBranchesTabClick(tabIndex int) error {
|
||||||
gui.State.Panels.Branches.ContextIndex = tabIndex
|
contexts := []string{"local-branches", "remotes", "tabs"}
|
||||||
branchesView := gui.getBranchesView()
|
branchesView := gui.getBranchesView()
|
||||||
|
branchesView.Context = contexts[tabIndex]
|
||||||
branchesView.TabIndex = tabIndex
|
branchesView.TabIndex = tabIndex
|
||||||
|
|
||||||
switch tabIndex {
|
switch contexts[tabIndex] {
|
||||||
case 0:
|
case "local-branches":
|
||||||
if err := gui.renderListPanel(branchesView, gui.State.Branches); err != nil {
|
if err := gui.renderListPanel(branchesView, gui.State.Branches); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case 1:
|
case "remotes":
|
||||||
if err := gui.renderListPanel(branchesView, gui.State.Remotes); err != nil {
|
if err := gui.renderListPanel(branchesView, gui.State.Remotes); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -50,20 +50,6 @@ func (gui *Gui) handleCommitFileSelect(g *gocui.Gui, v *gocui.View) error {
|
|||||||
return gui.renderString(g, "main", commitText)
|
return gui.renderString(g, "main", commitText)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleCommitFilesNextLine(g *gocui.Gui, v *gocui.View) error {
|
|
||||||
panelState := gui.State.Panels.CommitFiles
|
|
||||||
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.CommitFiles), false)
|
|
||||||
|
|
||||||
return gui.handleCommitFileSelect(gui.g, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gui *Gui) handleCommitFilesPrevLine(g *gocui.Gui, v *gocui.View) error {
|
|
||||||
panelState := gui.State.Panels.CommitFiles
|
|
||||||
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.CommitFiles), true)
|
|
||||||
|
|
||||||
return gui.handleCommitFileSelect(gui.g, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gui *Gui) handleSwitchToCommitsPanel(g *gocui.Gui, v *gocui.View) error {
|
func (gui *Gui) handleSwitchToCommitsPanel(g *gocui.Gui, v *gocui.View) error {
|
||||||
commitsView, err := g.View("commits")
|
commitsView, err := g.View("commits")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -119,34 +119,6 @@ func (gui *Gui) refreshCommits(g *gocui.Gui) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleCommitsNextLine(g *gocui.Gui, v *gocui.View) error {
|
|
||||||
if gui.popupPanelFocused() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
panelState := gui.State.Panels.Commits
|
|
||||||
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.Commits), false)
|
|
||||||
|
|
||||||
if err := gui.resetOrigin(gui.getMainView()); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return gui.handleCommitSelect(gui.g, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gui *Gui) handleCommitsPrevLine(g *gocui.Gui, v *gocui.View) error {
|
|
||||||
if gui.popupPanelFocused() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
panelState := gui.State.Panels.Commits
|
|
||||||
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.Commits), true)
|
|
||||||
|
|
||||||
if err := gui.resetOrigin(gui.getMainView()); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return gui.handleCommitSelect(gui.g, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// specific functions
|
// specific functions
|
||||||
|
|
||||||
func (gui *Gui) handleResetToCommit(g *gocui.Gui, commitView *gocui.View) error {
|
func (gui *Gui) handleResetToCommit(g *gocui.Gui, commitView *gocui.View) error {
|
||||||
|
@ -36,7 +36,7 @@ func (gui *Gui) handleFilesClick(g *gocui.Gui, v *gocui.View) error {
|
|||||||
newSelectedLineIdx := v.SelectedLineIdx()
|
newSelectedLineIdx := v.SelectedLineIdx()
|
||||||
|
|
||||||
if newSelectedLineIdx > len(gui.State.Files)-1 {
|
if newSelectedLineIdx > len(gui.State.Files)-1 {
|
||||||
return gui.handleFileSelect(gui.g, v, false)
|
return gui.selectFile(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
gui.State.Panels.Files.SelectedLine = newSelectedLineIdx
|
gui.State.Panels.Files.SelectedLine = newSelectedLineIdx
|
||||||
@ -44,24 +44,28 @@ func (gui *Gui) handleFilesClick(g *gocui.Gui, v *gocui.View) error {
|
|||||||
if prevSelectedLineIdx == newSelectedLineIdx && gui.currentViewName() == v.Name() {
|
if prevSelectedLineIdx == newSelectedLineIdx && gui.currentViewName() == v.Name() {
|
||||||
return gui.handleFilePress(gui.g, v)
|
return gui.handleFilePress(gui.g, v)
|
||||||
} else {
|
} else {
|
||||||
return gui.handleFileSelect(gui.g, v, true)
|
return gui.selectFile(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleFileSelect(g *gocui.Gui, v *gocui.View, alreadySelected bool) error {
|
func (gui *Gui) handleFileSelect(g *gocui.Gui, v *gocui.View) error {
|
||||||
if _, err := gui.g.SetCurrentView(v.Name()); err != nil {
|
return gui.selectFile(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gui *Gui) selectFile(alreadySelected bool) error {
|
||||||
|
if _, err := gui.g.SetCurrentView("files"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := gui.getSelectedFile(g)
|
file, err := gui.getSelectedFile(gui.g)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err != gui.Errors.ErrNoFiles {
|
if err != gui.Errors.ErrNoFiles {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return gui.renderString(g, "main", gui.Tr.SLocalize("NoChangedFiles"))
|
return gui.renderString(gui.g, "main", gui.Tr.SLocalize("NoChangedFiles"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := gui.focusPoint(0, gui.State.Panels.Files.SelectedLine, len(gui.State.Files), v); err != nil {
|
if err := gui.focusPoint(0, gui.State.Panels.Files.SelectedLine, len(gui.State.Files), gui.getFilesView()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,7 +94,7 @@ func (gui *Gui) handleFileSelect(g *gocui.Gui, v *gocui.View, alreadySelected bo
|
|||||||
}
|
}
|
||||||
|
|
||||||
if alreadySelected {
|
if alreadySelected {
|
||||||
g.Update(func(*gocui.Gui) error {
|
gui.g.Update(func(*gocui.Gui) error {
|
||||||
if err := gui.setViewContent(gui.g, gui.getSecondaryView(), contentCached); err != nil {
|
if err := gui.setViewContent(gui.g, gui.getSecondaryView(), contentCached); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -98,10 +102,10 @@ func (gui *Gui) handleFileSelect(g *gocui.Gui, v *gocui.View, alreadySelected bo
|
|||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if err := gui.renderString(g, "secondary", contentCached); err != nil {
|
if err := gui.renderString(gui.g, "secondary", contentCached); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return gui.renderString(g, "main", leftContent)
|
return gui.renderString(gui.g, "main", leftContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) refreshFiles() error {
|
func (gui *Gui) refreshFiles() error {
|
||||||
@ -136,7 +140,7 @@ func (gui *Gui) refreshFiles() error {
|
|||||||
if g.CurrentView() == filesView || (g.CurrentView() == gui.getMainView() && gui.State.Context == "merging") {
|
if g.CurrentView() == filesView || (g.CurrentView() == gui.getMainView() && gui.State.Context == "merging") {
|
||||||
newSelectedFile, _ := gui.getSelectedFile(gui.g)
|
newSelectedFile, _ := gui.getSelectedFile(gui.g)
|
||||||
alreadySelected := newSelectedFile.Name == selectedFile.Name
|
alreadySelected := newSelectedFile.Name == selectedFile.Name
|
||||||
return gui.handleFileSelect(g, filesView, alreadySelected)
|
return gui.selectFile(alreadySelected)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -144,28 +148,6 @@ func (gui *Gui) refreshFiles() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleFilesNextLine(g *gocui.Gui, v *gocui.View) error {
|
|
||||||
if gui.popupPanelFocused() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
panelState := gui.State.Panels.Files
|
|
||||||
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.Files), false)
|
|
||||||
|
|
||||||
return gui.handleFileSelect(gui.g, v, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gui *Gui) handleFilesPrevLine(g *gocui.Gui, v *gocui.View) error {
|
|
||||||
if gui.popupPanelFocused() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
panelState := gui.State.Panels.Files
|
|
||||||
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.Files), true)
|
|
||||||
|
|
||||||
return gui.handleFileSelect(gui.g, v, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
// specific functions
|
// specific functions
|
||||||
|
|
||||||
func (gui *Gui) stagedFiles() []*commands.File {
|
func (gui *Gui) stagedFiles() []*commands.File {
|
||||||
@ -248,7 +230,7 @@ func (gui *Gui) handleFilePress(g *gocui.Gui, v *gocui.View) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return gui.handleFileSelect(g, v, true)
|
return gui.selectFile(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) allFilesStaged() bool {
|
func (gui *Gui) allFilesStaged() bool {
|
||||||
@ -275,7 +257,7 @@ func (gui *Gui) handleStageAll(g *gocui.Gui, v *gocui.View) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return gui.handleFileSelect(g, v, false)
|
return gui.handleFileSelect(gui.g, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleIgnoreFile(g *gocui.Gui, v *gocui.View) error {
|
func (gui *Gui) handleIgnoreFile(g *gocui.Gui, v *gocui.View) error {
|
||||||
|
@ -110,7 +110,6 @@ type filePanelState struct {
|
|||||||
// TODO: consider splitting this out into the window and the branches view
|
// TODO: consider splitting this out into the window and the branches view
|
||||||
type branchPanelState struct {
|
type branchPanelState struct {
|
||||||
SelectedLine int
|
SelectedLine int
|
||||||
ContextIndex int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type remotePanelState struct {
|
type remotePanelState struct {
|
||||||
@ -490,7 +489,6 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
|||||||
}
|
}
|
||||||
branchesView.Title = gui.Tr.SLocalize("BranchesTitle")
|
branchesView.Title = gui.Tr.SLocalize("BranchesTitle")
|
||||||
branchesView.Tabs = []string{"Local Branches", "Remotes"}
|
branchesView.Tabs = []string{"Local Branches", "Remotes"}
|
||||||
branchesView.TabIndex = gui.State.Panels.Branches.ContextIndex
|
|
||||||
branchesView.FgColor = textColor
|
branchesView.FgColor = textColor
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -632,6 +630,8 @@ func (gui *Gui) onInitialViewsCreation() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gui.getBranchesView().Context = "local-branches"
|
||||||
|
|
||||||
return gui.loadNewRepo()
|
return gui.loadNewRepo()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -332,6 +332,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
ViewName: "branches",
|
ViewName: "branches",
|
||||||
|
Contexts: []string{"local-branches"},
|
||||||
Key: gocui.KeySpace,
|
Key: gocui.KeySpace,
|
||||||
Modifier: gocui.ModNone,
|
Modifier: gocui.ModNone,
|
||||||
Handler: gui.handleBranchPress,
|
Handler: gui.handleBranchPress,
|
||||||
@ -339,6 +340,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
ViewName: "branches",
|
ViewName: "branches",
|
||||||
|
Contexts: []string{"local-branches"},
|
||||||
Key: 'o',
|
Key: 'o',
|
||||||
Modifier: gocui.ModNone,
|
Modifier: gocui.ModNone,
|
||||||
Handler: gui.handleCreatePullRequestPress,
|
Handler: gui.handleCreatePullRequestPress,
|
||||||
@ -346,6 +348,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
ViewName: "branches",
|
ViewName: "branches",
|
||||||
|
Contexts: []string{"local-branches"},
|
||||||
Key: 'c',
|
Key: 'c',
|
||||||
Modifier: gocui.ModNone,
|
Modifier: gocui.ModNone,
|
||||||
Handler: gui.handleCheckoutByName,
|
Handler: gui.handleCheckoutByName,
|
||||||
@ -353,6 +356,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
ViewName: "branches",
|
ViewName: "branches",
|
||||||
|
Contexts: []string{"local-branches"},
|
||||||
Key: 'F',
|
Key: 'F',
|
||||||
Modifier: gocui.ModNone,
|
Modifier: gocui.ModNone,
|
||||||
Handler: gui.handleForceCheckout,
|
Handler: gui.handleForceCheckout,
|
||||||
@ -360,6 +364,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
ViewName: "branches",
|
ViewName: "branches",
|
||||||
|
Contexts: []string{"local-branches"},
|
||||||
Key: 'n',
|
Key: 'n',
|
||||||
Modifier: gocui.ModNone,
|
Modifier: gocui.ModNone,
|
||||||
Handler: gui.handleNewBranch,
|
Handler: gui.handleNewBranch,
|
||||||
@ -367,6 +372,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
ViewName: "branches",
|
ViewName: "branches",
|
||||||
|
Contexts: []string{"local-branches"},
|
||||||
Key: 'd',
|
Key: 'd',
|
||||||
Modifier: gocui.ModNone,
|
Modifier: gocui.ModNone,
|
||||||
Handler: gui.handleDeleteBranch,
|
Handler: gui.handleDeleteBranch,
|
||||||
@ -374,6 +380,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
ViewName: "branches",
|
ViewName: "branches",
|
||||||
|
Contexts: []string{"local-branches"},
|
||||||
Key: 'r',
|
Key: 'r',
|
||||||
Modifier: gocui.ModNone,
|
Modifier: gocui.ModNone,
|
||||||
Handler: gui.handleRebase,
|
Handler: gui.handleRebase,
|
||||||
@ -381,6 +388,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
ViewName: "branches",
|
ViewName: "branches",
|
||||||
|
Contexts: []string{"local-branches"},
|
||||||
Key: 'M',
|
Key: 'M',
|
||||||
Modifier: gocui.ModNone,
|
Modifier: gocui.ModNone,
|
||||||
Handler: gui.handleMerge,
|
Handler: gui.handleMerge,
|
||||||
@ -388,6 +396,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
ViewName: "branches",
|
ViewName: "branches",
|
||||||
|
Contexts: []string{"local-branches"},
|
||||||
Key: 'f',
|
Key: 'f',
|
||||||
Modifier: gocui.ModNone,
|
Modifier: gocui.ModNone,
|
||||||
Handler: gui.handleFastForward,
|
Handler: gui.handleFastForward,
|
||||||
@ -978,6 +987,50 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
|
|||||||
Handler: gui.handlePopFileSnapshot,
|
Handler: gui.handlePopFileSnapshot,
|
||||||
Description: gui.Tr.SLocalize("Undo"),
|
Description: gui.Tr.SLocalize("Undo"),
|
||||||
},
|
},
|
||||||
|
// click handlers
|
||||||
|
{
|
||||||
|
ViewName: "menu",
|
||||||
|
Key: gocui.MouseLeft,
|
||||||
|
Modifier: gocui.ModNone,
|
||||||
|
Handler: gui.handleMenuClick,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ViewName: "files",
|
||||||
|
Key: gocui.MouseLeft,
|
||||||
|
Modifier: gocui.ModNone,
|
||||||
|
Handler: gui.handleFilesClick,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ViewName: "branches",
|
||||||
|
Contexts: []string{"local-branches"},
|
||||||
|
Key: gocui.MouseLeft,
|
||||||
|
Modifier: gocui.ModNone,
|
||||||
|
Handler: gui.handleBranchesClick,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ViewName: "commits",
|
||||||
|
Key: gocui.MouseLeft,
|
||||||
|
Modifier: gocui.ModNone,
|
||||||
|
Handler: gui.handleCommitsClick,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ViewName: "stash",
|
||||||
|
Key: gocui.MouseLeft,
|
||||||
|
Modifier: gocui.ModNone,
|
||||||
|
Handler: gui.handleStashEntrySelect,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ViewName: "status",
|
||||||
|
Key: gocui.MouseLeft,
|
||||||
|
Modifier: gocui.ModNone,
|
||||||
|
Handler: gui.handleStatusClick,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ViewName: "commitFiles",
|
||||||
|
Key: gocui.MouseLeft,
|
||||||
|
Modifier: gocui.ModNone,
|
||||||
|
Handler: gui.handleCommitFilesClick,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, viewName := range []string{"status", "branches", "files", "commits", "commitFiles", "stash", "menu"} {
|
for _, viewName := range []string{"status", "branches", "files", "commits", "commitFiles", "stash", "menu"} {
|
||||||
@ -995,29 +1048,14 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
|
|||||||
bindings = append(bindings, &Binding{ViewName: "", Key: rune(i+1) + '0', Modifier: gocui.ModNone, Handler: gui.goToSideView(viewName)})
|
bindings = append(bindings, &Binding{ViewName: "", Key: rune(i+1) + '0', Modifier: gocui.ModNone, Handler: gui.goToSideView(viewName)})
|
||||||
}
|
}
|
||||||
|
|
||||||
listPanelMap := map[string]struct {
|
for _, listView := range gui.getListViews() {
|
||||||
prevLine func(*gocui.Gui, *gocui.View) error
|
|
||||||
nextLine func(*gocui.Gui, *gocui.View) error
|
|
||||||
onClick func(*gocui.Gui, *gocui.View) error
|
|
||||||
}{
|
|
||||||
"menu": {prevLine: gui.handleMenuPrevLine, nextLine: gui.handleMenuNextLine, onClick: gui.handleMenuClick},
|
|
||||||
"files": {prevLine: gui.handleFilesPrevLine, nextLine: gui.handleFilesNextLine, onClick: gui.handleFilesClick},
|
|
||||||
"branches": {prevLine: gui.handleBranchesPrevLine, nextLine: gui.handleBranchesNextLine, onClick: gui.handleBranchesClick},
|
|
||||||
"commits": {prevLine: gui.handleCommitsPrevLine, nextLine: gui.handleCommitsNextLine, onClick: gui.handleCommitsClick},
|
|
||||||
"stash": {prevLine: gui.handleStashPrevLine, nextLine: gui.handleStashNextLine, onClick: gui.handleStashEntrySelect},
|
|
||||||
"status": {onClick: gui.handleStatusClick},
|
|
||||||
"commitFiles": {prevLine: gui.handleCommitFilesPrevLine, nextLine: gui.handleCommitFilesNextLine, onClick: gui.handleCommitFilesClick},
|
|
||||||
}
|
|
||||||
|
|
||||||
for viewName, functions := range listPanelMap {
|
|
||||||
bindings = append(bindings, []*Binding{
|
bindings = append(bindings, []*Binding{
|
||||||
{ViewName: viewName, Key: 'k', Modifier: gocui.ModNone, Handler: functions.prevLine},
|
{ViewName: listView.viewName, Contexts: []string{listView.context}, Key: 'k', Modifier: gocui.ModNone, Handler: listView.handlePrevLine},
|
||||||
{ViewName: viewName, Key: gocui.KeyArrowUp, Modifier: gocui.ModNone, Handler: functions.prevLine},
|
{ViewName: listView.viewName, Contexts: []string{listView.context}, Key: gocui.KeyArrowUp, Modifier: gocui.ModNone, Handler: listView.handlePrevLine},
|
||||||
{ViewName: viewName, Key: gocui.MouseWheelUp, Modifier: gocui.ModNone, Handler: functions.prevLine},
|
{ViewName: listView.viewName, Contexts: []string{listView.context}, Key: gocui.MouseWheelUp, Modifier: gocui.ModNone, Handler: listView.handlePrevLine},
|
||||||
{ViewName: viewName, Key: 'j', Modifier: gocui.ModNone, Handler: functions.nextLine},
|
{ViewName: listView.viewName, Contexts: []string{listView.context}, Key: 'j', Modifier: gocui.ModNone, Handler: listView.handleNextLine},
|
||||||
{ViewName: viewName, Key: gocui.KeyArrowDown, Modifier: gocui.ModNone, Handler: functions.nextLine},
|
{ViewName: listView.viewName, Contexts: []string{listView.context}, Key: gocui.KeyArrowDown, Modifier: gocui.ModNone, Handler: listView.handleNextLine},
|
||||||
{ViewName: viewName, Key: gocui.MouseWheelDown, Modifier: gocui.ModNone, Handler: functions.nextLine},
|
{ViewName: listView.viewName, Contexts: []string{listView.context}, Key: gocui.MouseWheelDown, Modifier: gocui.ModNone, Handler: listView.handleNextLine},
|
||||||
{ViewName: viewName, Key: gocui.MouseLeft, Modifier: gocui.ModNone, Handler: functions.onClick},
|
|
||||||
}...)
|
}...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
94
pkg/gui/list_view.go
Normal file
94
pkg/gui/list_view.go
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
package gui
|
||||||
|
|
||||||
|
import "github.com/jesseduffield/gocui"
|
||||||
|
|
||||||
|
type listView struct {
|
||||||
|
viewName string
|
||||||
|
context string
|
||||||
|
getItemsLength func() int
|
||||||
|
getSelectedLine func() *int
|
||||||
|
handleItemSelect func(g *gocui.Gui, v *gocui.View) error
|
||||||
|
gui *Gui
|
||||||
|
rendersToMainView bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lv *listView) handlePrevLine(g *gocui.Gui, v *gocui.View) error {
|
||||||
|
return lv.handleLineChange(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lv *listView) handleNextLine(g *gocui.Gui, v *gocui.View) error {
|
||||||
|
return lv.handleLineChange(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lv *listView) handleLineChange(change int) error {
|
||||||
|
if !lv.gui.isPopupPanel(lv.viewName) && lv.gui.popupPanelFocused() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
lv.gui.changeSelectedLine(lv.getSelectedLine(), lv.getItemsLength(), change)
|
||||||
|
|
||||||
|
if lv.rendersToMainView {
|
||||||
|
if err := lv.gui.resetOrigin(lv.gui.getMainView()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
view, err := lv.gui.g.View(lv.viewName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return lv.handleItemSelect(lv.gui.g, view)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gui *Gui) getListViews() []*listView {
|
||||||
|
return []*listView{
|
||||||
|
{
|
||||||
|
viewName: "menu",
|
||||||
|
getItemsLength: func() int { return gui.getMenuView().LinesHeight() },
|
||||||
|
getSelectedLine: func() *int { return &gui.State.Panels.Menu.SelectedLine },
|
||||||
|
handleItemSelect: gui.handleMenuSelect,
|
||||||
|
gui: gui,
|
||||||
|
rendersToMainView: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
viewName: "files",
|
||||||
|
getItemsLength: func() int { return len(gui.State.Files) },
|
||||||
|
getSelectedLine: func() *int { return &gui.State.Panels.Files.SelectedLine },
|
||||||
|
handleItemSelect: gui.handleFileSelect,
|
||||||
|
gui: gui,
|
||||||
|
rendersToMainView: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
viewName: "branches",
|
||||||
|
context: "local-branches",
|
||||||
|
getItemsLength: func() int { return len(gui.State.Branches) },
|
||||||
|
getSelectedLine: func() *int { return &gui.State.Panels.Branches.SelectedLine },
|
||||||
|
handleItemSelect: gui.handleBranchSelect,
|
||||||
|
gui: gui,
|
||||||
|
rendersToMainView: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
viewName: "commits",
|
||||||
|
getItemsLength: func() int { return len(gui.State.Commits) },
|
||||||
|
getSelectedLine: func() *int { return &gui.State.Panels.Commits.SelectedLine },
|
||||||
|
handleItemSelect: gui.handleCommitSelect,
|
||||||
|
gui: gui,
|
||||||
|
rendersToMainView: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
viewName: "stash",
|
||||||
|
getItemsLength: func() int { return len(gui.State.StashEntries) },
|
||||||
|
getSelectedLine: func() *int { return &gui.State.Panels.Stash.SelectedLine },
|
||||||
|
handleItemSelect: gui.handleStashEntrySelect,
|
||||||
|
gui: gui,
|
||||||
|
rendersToMainView: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
viewName: "commitFiles",
|
||||||
|
getItemsLength: func() int { return len(gui.State.CommitFiles) },
|
||||||
|
getSelectedLine: func() *int { return &gui.State.Panels.CommitFiles.SelectedLine },
|
||||||
|
handleItemSelect: gui.handleCommitFileSelect,
|
||||||
|
gui: gui,
|
||||||
|
rendersToMainView: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
@ -14,20 +14,6 @@ func (gui *Gui) handleMenuSelect(g *gocui.Gui, v *gocui.View) error {
|
|||||||
return gui.focusPoint(0, gui.State.Panels.Menu.SelectedLine, gui.State.MenuItemCount, v)
|
return gui.focusPoint(0, gui.State.Panels.Menu.SelectedLine, gui.State.MenuItemCount, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleMenuNextLine(g *gocui.Gui, v *gocui.View) error {
|
|
||||||
panelState := gui.State.Panels.Menu
|
|
||||||
gui.changeSelectedLine(&panelState.SelectedLine, v.LinesHeight(), false)
|
|
||||||
|
|
||||||
return gui.handleMenuSelect(g, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gui *Gui) handleMenuPrevLine(g *gocui.Gui, v *gocui.View) error {
|
|
||||||
panelState := gui.State.Panels.Menu
|
|
||||||
gui.changeSelectedLine(&panelState.SelectedLine, v.LinesHeight(), true)
|
|
||||||
|
|
||||||
return gui.handleMenuSelect(g, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// specific functions
|
// specific functions
|
||||||
|
|
||||||
func (gui *Gui) renderMenuOptions() error {
|
func (gui *Gui) renderMenuOptions() error {
|
||||||
|
@ -71,34 +71,6 @@ func (gui *Gui) refreshStashEntries(g *gocui.Gui) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleStashNextLine(g *gocui.Gui, v *gocui.View) error {
|
|
||||||
if gui.popupPanelFocused() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
panelState := gui.State.Panels.Stash
|
|
||||||
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.StashEntries), false)
|
|
||||||
|
|
||||||
if err := gui.resetOrigin(gui.getMainView()); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return gui.handleStashEntrySelect(gui.g, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gui *Gui) handleStashPrevLine(g *gocui.Gui, v *gocui.View) error {
|
|
||||||
if gui.popupPanelFocused() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
panelState := gui.State.Panels.Stash
|
|
||||||
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.StashEntries), true)
|
|
||||||
|
|
||||||
if err := gui.resetOrigin(gui.getMainView()); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return gui.handleStashEntrySelect(gui.g, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// specific functions
|
// specific functions
|
||||||
|
|
||||||
func (gui *Gui) handleStashApply(g *gocui.Gui, v *gocui.View) error {
|
func (gui *Gui) handleStashApply(g *gocui.Gui, v *gocui.View) error {
|
||||||
|
@ -101,7 +101,7 @@ func (gui *Gui) newLineFocused(g *gocui.Gui, v *gocui.View) error {
|
|||||||
case "status":
|
case "status":
|
||||||
return gui.handleStatusSelect(g, v)
|
return gui.handleStatusSelect(g, v)
|
||||||
case "files":
|
case "files":
|
||||||
return gui.handleFileSelect(g, v, false)
|
return gui.handleFileSelect(g, v)
|
||||||
case "branches":
|
case "branches":
|
||||||
return gui.handleBranchSelect(g, v)
|
return gui.handleBranchSelect(g, v)
|
||||||
case "commits":
|
case "commits":
|
||||||
@ -315,6 +315,11 @@ func (gui *Gui) getCommitFilesView() *gocui.View {
|
|||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (gui *Gui) getMenuView() *gocui.View {
|
||||||
|
v, _ := gui.g.View("menu")
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
func (gui *Gui) trimmedContent(v *gocui.View) string {
|
func (gui *Gui) trimmedContent(v *gocui.View) string {
|
||||||
return strings.TrimSpace(v.Buffer())
|
return strings.TrimSpace(v.Buffer())
|
||||||
}
|
}
|
||||||
@ -362,19 +367,17 @@ func (gui *Gui) generalFocusLine(lineNumber int, bottomLine int, v *gocui.View)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) changeSelectedLine(line *int, total int, up bool) {
|
func (gui *Gui) changeSelectedLine(line *int, total int, change int) {
|
||||||
if up {
|
// TODO: find out why we're doing this
|
||||||
if *line == -1 || *line == 0 {
|
if *line == -1 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if *line+change < 0 {
|
||||||
*line--
|
*line = 0
|
||||||
|
} else if *line+change >= total {
|
||||||
|
*line = total - 1
|
||||||
} else {
|
} else {
|
||||||
if *line == -1 || *line == total-1 {
|
*line += change
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
*line++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user