1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-15 11:56:37 +02:00

cleaning up

This commit is contained in:
Jesse Duffield 2022-01-30 11:22:47 +11:00
parent e187293456
commit 09dc160da9
8 changed files with 34 additions and 54 deletions

View File

@ -445,3 +445,12 @@ func (gui *Gui) handleNewBranchOffCurrentItem() error {
func sanitizedBranchName(input string) string { func sanitizedBranchName(input string) string {
return strings.Replace(input, " ", "-", -1) return strings.Replace(input, " ", "-", -1)
} }
func (gui *Gui) handleEnterBranch() error {
branch := gui.getSelectedBranch()
if branch == nil {
return nil
}
return gui.switchToSubCommitsContext(branch.RefName())
}

View File

@ -9,14 +9,8 @@ import (
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
) )
type Thing interface {
// the boolean here tells us whether the item is nil. This is needed because you can't work it out on the calling end once the pointer is wrapped in an interface (unless you want to use reflection)
GetSelectedItem() (types.ListItem, bool)
}
type ListContextTrait struct { type ListContextTrait struct {
base types.IBaseContext base types.IBaseContext
thing Thing
listTrait *ListTrait listTrait *ListTrait
viewTrait *ViewTrait viewTrait *ViewTrait
@ -55,16 +49,6 @@ func formatListFooter(selectedLineIdx int, length int) string {
return fmt.Sprintf("%d of %d", selectedLineIdx+1, length) return fmt.Sprintf("%d of %d", selectedLineIdx+1, length)
} }
func (self *ListContextTrait) GetSelectedItemId() string {
item, ok := self.thing.GetSelectedItem()
if !ok {
return ""
}
return item.ID()
}
// 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 // 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 (self *ListContextTrait) HandleRender() error { func (self *ListContextTrait) HandleRender() error {
if self.GetDisplayStrings != nil { if self.GetDisplayStrings != nil {

View File

@ -7,7 +7,7 @@ import (
) )
type TagsContext struct { type TagsContext struct {
*TagsContextAux *TagsList
*BaseContext *BaseContext
*ListContextTrait *ListContextTrait
} }
@ -35,12 +35,11 @@ func NewTagsContext(
self := &TagsContext{} self := &TagsContext{}
takeFocus := func() error { return c.PushContext(self) } takeFocus := func() error { return c.PushContext(self) }
aux := NewTagsContextAux(getModel) list := NewTagsList(getModel)
viewTrait := NewViewTrait(getView) viewTrait := NewViewTrait(getView)
listContextTrait := &ListContextTrait{ listContextTrait := &ListContextTrait{
base: baseContext, base: baseContext,
thing: aux, listTrait: list.ListTrait,
listTrait: aux.list,
viewTrait: viewTrait, viewTrait: viewTrait,
GetDisplayStrings: getDisplayStrings, GetDisplayStrings: getDisplayStrings,
@ -57,39 +56,39 @@ func NewTagsContext(
self.BaseContext = baseContext self.BaseContext = baseContext
self.ListContextTrait = listContextTrait self.ListContextTrait = listContextTrait
self.TagsContextAux = aux self.TagsList = list
return self return self
} }
type TagsContextAux struct { type TagsList struct {
list *ListTrait *ListTrait
getModel func() []*models.Tag getModel func() []*models.Tag
} }
func (self *TagsContextAux) GetItemsLength() int { func (self *TagsList) GetItemsLength() int {
return len(self.getModel()) return len(self.getModel())
} }
func (self *TagsContextAux) GetSelectedTag() *models.Tag { func (self *TagsList) GetSelectedTag() *models.Tag {
if self.GetItemsLength() == 0 { if self.GetItemsLength() == 0 {
return nil return nil
} }
return self.getModel()[self.list.GetSelectedLineIdx()] return self.getModel()[self.GetSelectedLineIdx()]
} }
func (self *TagsContextAux) GetSelectedItem() (types.ListItem, bool) { func (self *TagsList) GetSelectedItem() (types.ListItem, bool) {
tag := self.GetSelectedTag() tag := self.GetSelectedTag()
return tag, tag != nil return tag, tag != nil
} }
func NewTagsContextAux(getModel func() []*models.Tag) *TagsContextAux { func NewTagsList(getModel func() []*models.Tag) *TagsList {
self := &TagsContextAux{ self := &TagsList{
getModel: getModel, getModel: getModel,
} }
self.list = &ListTrait{ self.ListTrait = &ListTrait{
selectedIdx: 0, selectedIdx: 0,
HasLength: self, HasLength: self,
} }

View File

@ -488,7 +488,7 @@ func (gui *Gui) GetInitialKeybindings() []*types.Binding {
ViewName: "branches", ViewName: "branches",
Contexts: []string{string(context.LOCAL_BRANCHES_CONTEXT_KEY)}, Contexts: []string{string(context.LOCAL_BRANCHES_CONTEXT_KEY)},
Key: gui.getKey(config.Universal.GoInto), Key: gui.getKey(config.Universal.GoInto),
Handler: gui.handleSwitchToSubCommits, Handler: gui.handleEnterBranch,
Description: gui.c.Tr.LcViewCommits, Description: gui.c.Tr.LcViewCommits,
}, },
{ {
@ -510,7 +510,7 @@ func (gui *Gui) GetInitialKeybindings() []*types.Binding {
ViewName: "branches", ViewName: "branches",
Contexts: []string{string(context.REMOTE_BRANCHES_CONTEXT_KEY)}, Contexts: []string{string(context.REMOTE_BRANCHES_CONTEXT_KEY)},
Key: gui.getKey(config.Universal.GoInto), Key: gui.getKey(config.Universal.GoInto),
Handler: gui.handleSwitchToSubCommits, Handler: gui.handleEnterRemoteBranch,
Description: gui.c.Tr.LcViewCommits, Description: gui.c.Tr.LcViewCommits,
}, },
{ {

View File

@ -60,16 +60,6 @@ func (self *ListContext) GetSelectedItem() (types.ListItem, bool) {
return self.SelectedItem() return self.SelectedItem()
} }
func (self *ListContext) GetSelectedItemId() string {
item, ok := self.GetSelectedItem()
if !ok {
return ""
}
return item.ID()
}
// 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 // 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 (self *ListContext) HandleRender() error { func (self *ListContext) HandleRender() error {
view, err := self.Gui.g.View(self.ViewName) view, err := self.Gui.g.View(self.ViewName)

View File

@ -109,3 +109,12 @@ func (gui *Gui) handleCreateResetToRemoteBranchMenu() error {
return gui.helpers.refs.CreateGitResetMenu(selectedBranch.FullName()) return gui.helpers.refs.CreateGitResetMenu(selectedBranch.FullName())
} }
func (gui *Gui) handleEnterRemoteBranch() error {
selectedBranch := gui.getSelectedRemoteBranch()
if selectedBranch == nil {
return nil
}
return gui.switchToSubCommitsContext(selectedBranch.RefName())
}

View File

@ -102,12 +102,3 @@ func (gui *Gui) switchToSubCommitsContext(refName string) error {
return gui.c.PushContext(gui.State.Contexts.SubCommits) return gui.c.PushContext(gui.State.Contexts.SubCommits)
} }
func (gui *Gui) handleSwitchToSubCommits() error {
currentContext := gui.currentSideListContext()
if currentContext == nil {
return nil
}
return gui.switchToSubCommitsContext(currentContext.GetSelectedItemId())
}

View File

@ -62,21 +62,19 @@ type IController interface {
type IListContext interface { type IListContext interface {
HasKeybindings HasKeybindings
GetSelectedItem() (ListItem, bool) GetSelectedItem() (ListItem, bool)
GetSelectedItemId() string
HandlePrevLine() error HandlePrevLine() error
HandleNextLine() error HandleNextLine() error
HandleScrollLeft() error HandleScrollLeft() error
HandleScrollRight() error HandleScrollRight() error
HandlePrevPage() error
HandleNextPage() error HandleNextPage() error
HandleGotoTop() error HandleGotoTop() error
HandleGotoBottom() error HandleGotoBottom() error
HandlePrevPage() error
HandleClick(onClick func() error) error HandleClick(onClick func() error) error
OnSearchSelect(selectedLineIdx int) error OnSearchSelect(selectedLineIdx int) error
FocusLine() FocusLine()
HandleRenderToMain() error
GetPanelState() IListPanelState GetPanelState() IListPanelState