diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-01-16 14:46:53 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-17 19:13:40 +1100 |
commit | 1dd7307fde033dae5fececac15810a99e26c3d91 (patch) | |
tree | 4e851c9e3229a6fe3b4191f6311d05d7a9142960 /pkg/gui/list_context.go | |
parent | a90b6efded49abcfa2516db794d7875b0396f558 (diff) |
start moving commit panel handlers into controller
more
and more
move rebase commit refreshing into existing abstraction
and more
and more
WIP
and more
handling clicks
properly fix merge conflicts
update cheatsheet
lots more preparation to start moving things into controllers
WIP
better typing
expand on remotes controller
moving more code into controllers
Diffstat (limited to 'pkg/gui/list_context.go')
-rw-r--r-- | pkg/gui/list_context.go | 116 |
1 files changed, 58 insertions, 58 deletions
diff --git a/pkg/gui/list_context.go b/pkg/gui/list_context.go index 9f0d86372..3d779cabd 100644 --- a/pkg/gui/list_context.go +++ b/pkg/gui/list_context.go @@ -4,19 +4,20 @@ import ( "fmt" "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/config" + "github.com/jesseduffield/lazygit/pkg/gui/types" ) type ListContext struct { - GetItemsLength func() int - GetDisplayStrings func(startIdx int, length int) [][]string - OnFocus func(...OnFocusOpts) error - OnRenderToMain func(...OnFocusOpts) error - OnFocusLost func() error - OnClickSelectedItem func() error + GetItemsLength func() int + GetDisplayStrings func(startIdx int, length int) [][]string + OnFocus func(...types.OnFocusOpts) error + OnRenderToMain func(...types.OnFocusOpts) error + OnFocusLost func() error // 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) - SelectedItem func() (ListItem, bool) - OnGetPanelState func() IListPanelState + SelectedItem func() (types.ListItem, bool) + OnGetPanelState func() types.IListPanelState // if this is true, we'll call GetDisplayStrings for just the visible part of the // view and re-render that. This is useful when you need to render different // content based on the selection (e.g. for showing the selected commit) @@ -27,45 +28,12 @@ type ListContext struct { *BasicContext } -type IListContext interface { - GetSelectedItem() (ListItem, bool) - GetSelectedItemId() string - handlePrevLine() error - handleNextLine() error - handleScrollLeft() error - handleScrollRight() error - handleLineChange(change int) error - handleNextPage() error - handleGotoTop() error - handleGotoBottom() error - handlePrevPage() error - handleClick() error - onSearchSelect(selectedLineIdx int) error - FocusLine() - HandleRenderToMain() error - - GetPanelState() IListPanelState - - Context -} +var _ types.IListContext = &ListContext{} -func (self *ListContext) GetPanelState() IListPanelState { +func (self *ListContext) GetPanelState() types.IListPanelState { return self.OnGetPanelState() } -type IListPanelState interface { - SetSelectedLineIdx(int) - GetSelectedLineIdx() int -} - -type ListItem interface { - // ID is a SHA when the item is a commit, a filename when the item is a file, 'stash@{4}' when it's a stash entry, 'my_branch' when it's a branch - ID() string - - // Description is something we would show in a message e.g. '123as14: push blah' for a commit - Description() string -} - func (self *ListContext) FocusLine() { view, err := self.Gui.g.View(self.ViewName) if err != nil { @@ -87,7 +55,7 @@ func formatListFooter(selectedLineIdx int, length int) string { return fmt.Sprintf("%d of %d", selectedLineIdx+1, length) } -func (self *ListContext) GetSelectedItem() (ListItem, bool) { +func (self *ListContext) GetSelectedItem() (types.ListItem, bool) { return self.SelectedItem() } @@ -132,7 +100,7 @@ func (self *ListContext) HandleFocusLost() error { return nil } -func (self *ListContext) HandleFocus(opts ...OnFocusOpts) error { +func (self *ListContext) HandleFocus(opts ...types.OnFocusOpts) error { if self.Gui.popupPanelFocused() { return nil } @@ -158,19 +126,19 @@ func (self *ListContext) HandleFocus(opts ...OnFocusOpts) error { return nil } -func (self *ListContext) handlePrevLine() error { +func (self *ListContext) HandlePrevLine() error { return self.handleLineChange(-1) } -func (self *ListContext) handleNextLine() error { +func (self *ListContext) HandleNextLine() error { return self.handleLineChange(1) } -func (self *ListContext) handleScrollLeft() error { +func (self *ListContext) HandleScrollLeft() error { return self.scroll(self.Gui.scrollLeft) } -func (self *ListContext) handleScrollRight() error { +func (self *ListContext) HandleScrollRight() error { return self.scroll(self.Gui.scrollRight) } @@ -209,7 +177,7 @@ func (self *ListContext) handleLineChange(change int) error { return self.HandleFocus() } -func (self *ListContext) handleNextPage() error { +func (self *ListContext) HandleNextPage() error { view, err := self.Gui.g.View(self.ViewName) if err != nil { return nil @@ -219,15 +187,15 @@ func (self *ListContext) handleNextPage() error { return self.handleLineChange(delta) } -func (self *ListContext) handleGotoTop() error { +func (self *ListContext) HandleGotoTop() error { return self.handleLineChange(-self.GetItemsLength()) } -func (self *ListContext) handleGotoBottom() error { +func (self *ListContext) HandleGotoBottom() error { return self.handleLineChange(self.GetItemsLength()) } -func (self *ListContext) handlePrevPage() error { +func (self *ListContext) HandlePrevPage() error { view, err := self.Gui.g.View(self.ViewName) if err != nil { return nil @@ -238,7 +206,7 @@ func (self *ListContext) handlePrevPage() error { return self.handleLineChange(-delta) } -func (self *ListContext) handleClick() error { +func (self *ListContext) HandleClick(onClick func() error) error { if self.ignoreKeybinding() { return nil } @@ -252,7 +220,7 @@ func (self *ListContext) handleClick() error { newSelectedLineIdx := view.SelectedLineIdx() // we need to focus the view - if err := self.Gui.pushContext(self); err != nil { + if err := self.Gui.c.PushContext(self); err != nil { return err } @@ -263,13 +231,13 @@ func (self *ListContext) handleClick() error { self.GetPanelState().SetSelectedLineIdx(newSelectedLineIdx) prevViewName := self.Gui.currentViewName() - if prevSelectedLineIdx == newSelectedLineIdx && prevViewName == self.ViewName && self.OnClickSelectedItem != nil { - return self.OnClickSelectedItem() + if prevSelectedLineIdx == newSelectedLineIdx && prevViewName == self.ViewName && onClick != nil { + return onClick() } return self.HandleFocus() } -func (self *ListContext) onSearchSelect(selectedLineIdx int) error { +func (self *ListContext) OnSearchSelect(selectedLineIdx int) error { self.GetPanelState().SetSelectedLineIdx(selectedLineIdx) return self.HandleFocus() } @@ -281,3 +249,35 @@ func (self *ListContext) HandleRenderToMain() error { return nil } + +func (self *ListContext) Keybindings( + getKey func(key string) interface{}, + config config.KeybindingConfig, + guards types.KeybindingGuards, +) []*types.Binding { + return []*types.Binding{ + {Tag: "navigation", Key: getKey(config.Universal.PrevItemAlt), Modifier: gocui.ModNone, Handler: self.HandlePrevLine}, + {Tag: "navigation", Key: getKey(config.Universal.PrevItem), Modifier: gocui.ModNone, Handler: self.HandlePrevLine}, + {Tag: "navigation", Key: gocui.MouseWheelUp, Modifier: gocui.ModNone, Handler: self.HandlePrevLine}, + {Tag: "navigation", Key: getKey(config.Universal.NextItemAlt), Modifier: gocui.ModNone, Handler: self.HandleNextLine}, + {Tag: "navigation", Key: getKey(config.Universal.NextItem), Modifier: gocui.ModNone, Handler: self.HandleNextLine}, + {Tag: "navigation", Key: getKey(config.Universal.PrevPage), Modifier: gocui.ModNone, Handler: self.HandlePrevPage, Description: self.Gui.c.Tr.LcPrevPage}, + {Tag: "navigation", Key: getKey(config.Universal.NextPage), Modifier: gocui.ModNone, Handler: self.HandleNextPage, Description: self.Gui.c.Tr.LcNextPage}, + {Tag: "navigation", Key: getKey(config.Universal.GotoTop), Modifier: gocui.ModNone, Handler: self.HandleGotoTop, Description: self.Gui.c.Tr.LcGotoTop}, + {Key: gocui.MouseLeft, Modifier: gocui.ModNone, Handler: func() error { return self.HandleClick(nil) }}, + {Tag: "navigation", Key: gocui.MouseWheelDown, Modifier: gocui.ModNone, Handler: self.HandleNextLine}, + {Tag: "navigation", Key: getKey(config.Universal.ScrollLeft), Modifier: gocui.ModNone, Handler: self.HandleScrollLeft}, + {Tag: "navigation", Key: getKey(config.Universal.ScrollRight), Modifier: gocui.ModNone, Handler: self.HandleScrollRight}, + { + Key: getKey(config.Universal.StartSearch), + Handler: func() error { return self.Gui.handleOpenSearch(self.GetViewName()) }, + Description: self.Gui.c.Tr.LcStartSearch, + Tag: "navigation", + }, + { + Key: getKey(config.Universal.GotoBottom), + Description: self.Gui.c.Tr.LcGotoBottom, + Tag: "navigation", + }, + } +} |