1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-03 13:21:56 +02:00

more standardisation

This commit is contained in:
Jesse Duffield 2020-08-19 21:51:50 +10:00
parent dbf6bb5f27
commit 419cb9feb8
11 changed files with 82 additions and 141 deletions

View File

@ -6,7 +6,6 @@ import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
)
// list panel functions
@ -82,16 +81,6 @@ func (gui *Gui) refreshBranches() {
gui.refreshStatus()
}
func (gui *Gui) renderLocalBranchesContext() error {
branchesView := gui.getBranchesView()
gui.refreshSelectedLine(&gui.State.Panels.Branches.SelectedLine, len(gui.State.Branches))
displayStrings := presentation.GetBranchListDisplayStrings(gui.State.Branches, gui.State.ScreenMode != SCREEN_NORMAL, gui.State.Diff.Ref)
gui.renderDisplayStrings(branchesView, displayStrings)
return nil
}
// specific functions
func (gui *Gui) handleBranchPress(g *gocui.Gui, v *gocui.View) error {

View File

@ -4,7 +4,6 @@ import (
"github.com/go-errors/errors"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
)
func (gui *Gui) getSelectedCommitFile() *commands.CommitFile {
@ -105,16 +104,6 @@ func (gui *Gui) refreshCommitFilesView() error {
return gui.postRefreshUpdate(gui.Contexts.BranchCommits.Files.Context)
}
func (gui *Gui) renderCommitFiles() error {
gui.refreshSelectedLine(&gui.State.Panels.CommitFiles.SelectedLine, len(gui.State.CommitFiles))
commitsFileView := gui.getCommitFilesView()
displayStrings := presentation.GetCommitFileListDisplayStrings(gui.State.CommitFiles, gui.State.Diff.Ref)
gui.renderDisplayStrings(commitsFileView, displayStrings)
return nil
}
func (gui *Gui) handleOpenOldCommitFile(g *gocui.Gui, v *gocui.View) error {
file := gui.getSelectedCommitFile()
if file == nil {

View File

@ -6,7 +6,6 @@ import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
)
// list panel functions
@ -626,16 +625,6 @@ func (gui *Gui) handleCheckoutCommit(g *gocui.Gui, v *gocui.View) error {
})
}
func (gui *Gui) renderBranchCommitsContext() error {
commitsView := gui.getCommitsView()
gui.refreshSelectedLine(&gui.State.Panels.Commits.SelectedLine, len(gui.State.Commits))
displayStrings := presentation.GetCommitListDisplayStrings(gui.State.Commits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Diff.Ref)
gui.renderDisplayStrings(commitsView, displayStrings)
return nil
}
func (gui *Gui) handleCreateCommitResetMenu(g *gocui.Gui, v *gocui.View) error {
commit := gui.getSelectedCommit()
if commit == nil {

View File

@ -13,7 +13,6 @@ import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/utils"
)
@ -102,7 +101,7 @@ func (gui *Gui) refreshFiles() error {
}
gui.g.Update(func(g *gocui.Gui) error {
if err := gui.renderFiles(); err != nil {
if err := gui.Contexts.Files.Context.HandleRender(); err != nil {
return err
}
@ -117,18 +116,6 @@ func (gui *Gui) refreshFiles() error {
return nil
}
func (gui *Gui) renderFiles() error {
filesView := gui.getFilesView()
if filesView == nil {
// if the filesView hasn't been instantiated yet we just return
return nil
}
displayStrings := presentation.GetFileListDisplayStrings(gui.State.Files, gui.State.Diff.Ref)
gui.renderDisplayStrings(filesView, displayStrings)
return nil
}
// specific functions
func (gui *Gui) stagedFiles() []*commands.File {

View File

@ -101,6 +101,10 @@ type Gui struct {
ViewTabContextMap map[string][]tabContext
}
type hasSelectedLine struct {
SelectedLine int
}
// for now the staging panel state, unlike the other panel states, is going to be
// non-mutative, so that we don't accidentally end up
// with mismatches of data. We might change this in the future
@ -126,46 +130,47 @@ type mergingPanelState struct {
}
type filePanelState struct {
SelectedLine int
hasSelectedLine
}
// TODO: consider splitting this out into the window and the branches view
type branchPanelState struct {
SelectedLine int
hasSelectedLine
}
type remotePanelState struct {
SelectedLine int
hasSelectedLine
}
type remoteBranchesState struct {
SelectedLine int
hasSelectedLine
}
type tagsPanelState struct {
SelectedLine int
hasSelectedLine
}
type commitPanelState struct {
SelectedLine int
hasSelectedLine
LimitCommits bool
}
type reflogCommitPanelState struct {
SelectedLine int
hasSelectedLine
}
type stashPanelState struct {
SelectedLine int
hasSelectedLine
}
type menuPanelState struct {
SelectedLine int
OnPress func(g *gocui.Gui, v *gocui.View) error
hasSelectedLine
OnPress func(g *gocui.Gui, v *gocui.View) error
}
type commitFilesPanelState struct {
SelectedLine int
hasSelectedLine
}
type panelStates struct {
@ -266,16 +271,16 @@ func (gui *Gui) resetState() {
CherryPickedCommits: make([]*commands.Commit, 0),
StashEntries: make([]*commands.StashEntry, 0),
Panels: &panelStates{
Files: &filePanelState{SelectedLine: -1},
Branches: &branchPanelState{SelectedLine: 0},
Remotes: &remotePanelState{SelectedLine: 0},
RemoteBranches: &remoteBranchesState{SelectedLine: -1},
Tags: &tagsPanelState{SelectedLine: -1},
Commits: &commitPanelState{SelectedLine: -1, LimitCommits: true},
ReflogCommits: &reflogCommitPanelState{SelectedLine: 0}, // TODO: might need to make -1
CommitFiles: &commitFilesPanelState{SelectedLine: -1},
Stash: &stashPanelState{SelectedLine: -1},
Menu: &menuPanelState{SelectedLine: 0},
Files: &filePanelState{hasSelectedLine{SelectedLine: -1}},
Branches: &branchPanelState{hasSelectedLine{SelectedLine: 0}},
Remotes: &remotePanelState{hasSelectedLine{SelectedLine: 0}},
RemoteBranches: &remoteBranchesState{hasSelectedLine{SelectedLine: -1}},
Tags: &tagsPanelState{hasSelectedLine{SelectedLine: -1}},
Commits: &commitPanelState{hasSelectedLine: hasSelectedLine{SelectedLine: -1}, LimitCommits: true},
ReflogCommits: &reflogCommitPanelState{hasSelectedLine{SelectedLine: 0}}, // TODO: might need to make -1
CommitFiles: &commitFilesPanelState{hasSelectedLine{SelectedLine: -1}},
Stash: &stashPanelState{hasSelectedLine{SelectedLine: -1}},
Menu: &menuPanelState{hasSelectedLine: hasSelectedLine{SelectedLine: 0}, OnPress: nil},
Merging: &mergingPanelState{
ConflictIndex: 0,
ConflictTop: true,

View File

@ -1,24 +1,41 @@
package gui
import "github.com/jesseduffield/gocui"
import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
)
type ListContext struct {
ViewName string
ContextKey string
GetItemsLength func() int
GetSelectedLineIdxPtr func() *int
GetDisplayStrings func() [][]string
OnFocus func() error
OnFocusLost func() error
OnItemSelect func() error
OnClickSelectedItem func() error
// OnFocus assumes that the content of the context has already been rendered to the view. OnRender is the function which actually renders the content to the view
OnRender func() error
Gui *Gui
RendersToMainView bool
Kind int
}
// OnFocus assumes that the content of the context has already been rendered to the view. OnRender is the function which actually renders the content to the view
func (lc *ListContext) OnRender() error {
view, err := lc.Gui.g.View(lc.ViewName)
if err != nil {
return nil
}
if lc.GetDisplayStrings != nil {
lc.Gui.refreshSelectedLine(lc.GetSelectedLineIdxPtr(), lc.GetItemsLength())
lc.Gui.renderDisplayStrings(view, lc.GetDisplayStrings())
}
return nil
}
func (lc *ListContext) GetKey() string {
return lc.ContextKey
}
@ -163,13 +180,13 @@ func (gui *Gui) menuListContext() *ListContext {
GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Menu.SelectedLine },
OnFocus: gui.handleMenuSelect,
OnItemSelect: gui.handleMenuSelect,
// rendering the menu happens on creation
OnRender: func() error { return nil },
// need to add a layer of indirection here because the callback changes during runtime
OnClickSelectedItem: func() error { return gui.State.Panels.Menu.OnPress(gui.g, nil) },
Gui: gui,
RendersToMainView: false,
Kind: PERSISTENT_POPUP,
// no GetDisplayStrings field because we do a custom render on menu creation
}
}
@ -182,10 +199,12 @@ func (gui *Gui) filesListContext() *ListContext {
OnFocus: gui.focusAndSelectFile,
OnItemSelect: gui.focusAndSelectFile,
OnClickSelectedItem: gui.handleFilePress,
OnRender: gui.renderFiles,
Gui: gui,
RendersToMainView: false,
Kind: SIDE_CONTEXT,
GetDisplayStrings: func() [][]string {
return presentation.GetFileListDisplayStrings(gui.State.Files, gui.State.Diff.Ref)
},
}
}
@ -197,10 +216,12 @@ func (gui *Gui) branchesListContext() *ListContext {
GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Branches.SelectedLine },
OnFocus: gui.handleBranchSelect,
OnItemSelect: gui.handleBranchSelect,
OnRender: gui.renderLocalBranchesContext,
Gui: gui,
RendersToMainView: true,
Kind: SIDE_CONTEXT,
GetDisplayStrings: func() [][]string {
return presentation.GetBranchListDisplayStrings(gui.State.Branches, gui.State.ScreenMode != SCREEN_NORMAL, gui.State.Diff.Ref)
},
}
}
@ -213,10 +234,12 @@ func (gui *Gui) remotesListContext() *ListContext {
OnFocus: gui.handleRemoteSelect,
OnItemSelect: gui.handleRemoteSelect,
OnClickSelectedItem: gui.handleRemoteEnter,
OnRender: gui.renderRemotesContext,
Gui: gui,
RendersToMainView: true,
Kind: SIDE_CONTEXT,
GetDisplayStrings: func() [][]string {
return presentation.GetRemoteListDisplayStrings(gui.State.Remotes, gui.State.Diff.Ref)
},
}
}
@ -228,10 +251,12 @@ func (gui *Gui) remoteBranchesListContext() *ListContext {
GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.RemoteBranches.SelectedLine },
OnFocus: gui.handleRemoteBranchSelect,
OnItemSelect: gui.handleRemoteBranchSelect,
OnRender: gui.renderRemoteBranchesContext,
Gui: gui,
RendersToMainView: true,
Kind: SIDE_CONTEXT,
GetDisplayStrings: func() [][]string {
return presentation.GetRemoteBranchListDisplayStrings(gui.State.RemoteBranches, gui.State.Diff.Ref)
},
}
}
@ -243,10 +268,12 @@ func (gui *Gui) tagsListContext() *ListContext {
GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Tags.SelectedLine },
OnFocus: gui.handleTagSelect,
OnItemSelect: gui.handleTagSelect,
OnRender: gui.renderTagsContext,
Gui: gui,
RendersToMainView: true,
Kind: SIDE_CONTEXT,
GetDisplayStrings: func() [][]string {
return presentation.GetTagListDisplayStrings(gui.State.Tags, gui.State.Diff.Ref)
},
}
}
@ -259,10 +286,12 @@ func (gui *Gui) branchCommitsListContext() *ListContext {
OnFocus: gui.handleCommitSelect,
OnItemSelect: gui.handleCommitSelect,
OnClickSelectedItem: gui.handleSwitchToCommitFilesPanel,
OnRender: gui.renderBranchCommitsContext,
Gui: gui,
RendersToMainView: true,
Kind: SIDE_CONTEXT,
GetDisplayStrings: func() [][]string {
return presentation.GetCommitListDisplayStrings(gui.State.Commits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Diff.Ref)
},
}
}
@ -274,10 +303,12 @@ func (gui *Gui) reflogCommitsListContext() *ListContext {
GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.ReflogCommits.SelectedLine },
OnFocus: gui.handleReflogCommitSelect,
OnItemSelect: gui.handleReflogCommitSelect,
OnRender: gui.renderReflogCommitsContext,
Gui: gui,
RendersToMainView: true,
Kind: SIDE_CONTEXT,
GetDisplayStrings: func() [][]string {
return presentation.GetReflogCommitListDisplayStrings(gui.State.FilteredReflogCommits, gui.State.ScreenMode != SCREEN_NORMAL, gui.State.Diff.Ref)
},
}
}
@ -289,10 +320,13 @@ func (gui *Gui) stashListContext() *ListContext {
GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Stash.SelectedLine },
OnFocus: gui.handleStashEntrySelect,
OnItemSelect: gui.handleStashEntrySelect,
OnRender: gui.renderStash,
Gui: gui,
RendersToMainView: true,
Kind: SIDE_CONTEXT,
GetDisplayStrings: func() [][]string {
// TODO :see if we still need to reset the origin here
return presentation.GetStashEntryListDisplayStrings(gui.State.StashEntries, gui.State.Diff.Ref)
},
}
}
@ -304,10 +338,12 @@ func (gui *Gui) commitFilesListContext() *ListContext {
GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.CommitFiles.SelectedLine },
OnFocus: gui.handleCommitFileSelect,
OnItemSelect: gui.handleCommitFileSelect,
OnRender: gui.renderCommitFiles,
Gui: gui,
RendersToMainView: true,
Kind: SIDE_CONTEXT,
GetDisplayStrings: func() [][]string {
return presentation.GetCommitFileListDisplayStrings(gui.State.CommitFiles, gui.State.Diff.Ref)
},
}
}

View File

@ -3,7 +3,6 @@ package gui
import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
)
// list panel functions
@ -88,16 +87,6 @@ func (gui *Gui) refreshReflogCommits() error {
return gui.postRefreshUpdate(gui.Contexts.ReflogCommits.Context)
}
func (gui *Gui) renderReflogCommitsContext() error {
commitsView := gui.getCommitsView()
gui.refreshSelectedLine(&gui.State.Panels.ReflogCommits.SelectedLine, len(gui.State.FilteredReflogCommits))
displayStrings := presentation.GetReflogCommitListDisplayStrings(gui.State.FilteredReflogCommits, gui.State.ScreenMode != SCREEN_NORMAL, gui.State.Diff.Ref)
gui.renderDisplayStrings(commitsView, displayStrings)
return nil
}
func (gui *Gui) handleCheckoutReflogCommit(g *gocui.Gui, v *gocui.View) error {
commit := gui.getSelectedReflogCommit()
if commit == nil {

View File

@ -5,7 +5,6 @@ import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
)
// list panel functions
@ -51,16 +50,6 @@ func (gui *Gui) handleRemoteBranchesEscape(g *gocui.Gui, v *gocui.View) error {
return gui.switchContext(gui.Contexts.Remotes.Context)
}
func (gui *Gui) renderRemoteBranchesContext() error {
branchesView := gui.getBranchesView()
gui.refreshSelectedLine(&gui.State.Panels.RemoteBranches.SelectedLine, len(gui.State.RemoteBranches))
displayStrings := presentation.GetRemoteBranchListDisplayStrings(gui.State.RemoteBranches, gui.State.Diff.Ref)
gui.renderDisplayStrings(branchesView, displayStrings)
return nil
}
func (gui *Gui) handleCheckoutRemoteBranch(g *gocui.Gui, v *gocui.View) error {
remoteBranch := gui.getSelectedRemoteBranch()
if remoteBranch == nil {

View File

@ -7,7 +7,6 @@ import (
"github.com/fatih/color"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/utils"
)
@ -70,17 +69,6 @@ func (gui *Gui) refreshRemotes() error {
return gui.postRefreshUpdate(gui.contextForContextKey(gui.getBranchesView().Context))
}
func (gui *Gui) renderRemotesContext() error {
branchesView := gui.getBranchesView()
gui.refreshSelectedLine(&gui.State.Panels.Remotes.SelectedLine, len(gui.State.Remotes))
displayStrings := presentation.GetRemoteListDisplayStrings(gui.State.Remotes, gui.State.Diff.Ref)
gui.renderDisplayStrings(branchesView, displayStrings)
return nil
}
func (gui *Gui) handleRemoteEnter() error {
// naive implementation: get the branches and render them to the list, change the context
remote := gui.getSelectedRemote()

View File

@ -3,7 +3,6 @@ package gui
import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
)
// list panel functions
@ -48,18 +47,7 @@ func (gui *Gui) handleStashEntrySelect() error {
func (gui *Gui) refreshStashEntries() error {
gui.State.StashEntries = gui.GitCommand.GetStashEntries(gui.State.FilterPath)
return gui.renderStash()
}
func (gui *Gui) renderStash() error {
gui.refreshSelectedLine(&gui.State.Panels.Stash.SelectedLine, len(gui.State.StashEntries))
stashView := gui.getStashView()
displayStrings := presentation.GetStashEntryListDisplayStrings(gui.State.StashEntries, gui.State.Diff.Ref)
gui.renderDisplayStrings(stashView, displayStrings)
return gui.resetOrigin(stashView)
return gui.Contexts.Stash.Context.HandleRender()
}
// specific functions

View File

@ -3,7 +3,6 @@ package gui
import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
)
// list panel functions
@ -56,16 +55,6 @@ func (gui *Gui) refreshTags() error {
return gui.postRefreshUpdate(gui.Contexts.Tags.Context)
}
func (gui *Gui) renderTagsContext() error {
branchesView := gui.getBranchesView()
gui.refreshSelectedLine(&gui.State.Panels.Tags.SelectedLine, len(gui.State.Tags))
displayStrings := presentation.GetTagListDisplayStrings(gui.State.Tags, gui.State.Diff.Ref)
gui.renderDisplayStrings(branchesView, displayStrings)
return nil
}
func (gui *Gui) handleCheckoutTag(g *gocui.Gui, v *gocui.View) error {
tag := gui.getSelectedTag()
if tag == nil {
@ -136,7 +125,10 @@ func (gui *Gui) handleCreateTag(g *gocui.Gui, v *gocui.View) error {
for i, tag := range gui.State.Tags {
if tag.Name == tagName {
gui.State.Panels.Tags.SelectedLine = i
_ = gui.renderTagsContext()
if err := gui.Contexts.Tags.Context.HandleRender(); err != nil {
gui.Log.Error(err)
}
return
}
}