1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/list_context.go

221 lines
5.2 KiB
Go
Raw Normal View History

2019-11-16 05:00:27 +02:00
package gui
import (
"fmt"
)
2020-08-17 13:58:30 +02:00
type ListContext struct {
GetItemsLength func() int
GetDisplayStrings func(startIdx int, length int) [][]string
OnFocus func() error
OnFocusLost func() error
OnClickSelectedItem func() error
2020-08-23 01:42:30 +02:00
// 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
2020-08-19 10:06:51 +02:00
2021-10-17 10:01:02 +02:00
Gui *Gui
*BasicContext
2019-11-16 05:00:27 +02:00
}
type IListContext interface {
GetSelectedItem() (ListItem, bool)
GetSelectedItemId() string
OnRender() error
handlePrevLine() error
handleNextLine() error
handleLineChange(change int) error
handleNextPage() error
handleGotoTop() error
handleGotoBottom() error
handlePrevPage() error
handleClick() error
onSearchSelect(selectedLineIdx int) error
FocusLine()
GetPanelState() IListPanelState
Context
}
func (self *ListContext) GetPanelState() IListPanelState {
return self.OnGetPanelState()
}
2021-03-31 15:24:41 +02:00
type IListPanelState interface {
SetSelectedLineIdx(int)
GetSelectedLineIdx() int
}
2020-08-20 00:24:35 +02:00
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
2020-08-20 00:24:35 +02:00
ID() string
// Description is something we would show in a message e.g. '123as14: push blah' for a commit
Description() string
2020-08-20 00:24:35 +02:00
}
func (self *ListContext) FocusLine() {
view, err := self.Gui.g.View(self.ViewName)
if err != nil {
return
}
view.FocusPoint(0, self.GetPanelState().GetSelectedLineIdx())
view.Footer = formatListFooter(self.GetPanelState().GetSelectedLineIdx(), self.GetItemsLength())
}
func formatListFooter(selectedLineIdx int, length int) string {
return fmt.Sprintf("%d of %d", selectedLineIdx+1, length)
}
func (self *ListContext) GetSelectedItem() (ListItem, bool) {
return self.SelectedItem()
}
func (self *ListContext) GetSelectedItemId() string {
item, ok := self.GetSelectedItem()
2020-08-20 00:24:35 +02:00
if !ok {
2020-08-20 00:24:35 +02:00
return ""
}
return item.ID()
}
2020-08-19 13:51:50 +02:00
// 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) OnRender() error {
view, err := self.Gui.g.View(self.ViewName)
2020-08-19 13:51:50 +02:00
if err != nil {
return nil
}
if self.GetDisplayStrings != nil {
self.Gui.refreshSelectedLine(self.GetPanelState(), self.GetItemsLength())
self.Gui.renderDisplayStrings(view, self.GetDisplayStrings(0, self.GetItemsLength()))
self.Gui.render()
2020-08-19 13:51:50 +02:00
}
return nil
}
func (self *ListContext) HandleFocusLost() error {
if self.OnFocusLost != nil {
return self.OnFocusLost()
2020-08-16 05:58:29 +02:00
}
return nil
}
func (self *ListContext) HandleFocus() error {
if self.Gui.popupPanelFocused() {
2020-08-19 14:27:31 +02:00
return nil
}
self.FocusLine()
2020-08-22 00:49:02 +02:00
if self.Gui.State.Modes.Diffing.Active() {
return self.Gui.renderDiff()
2020-08-19 14:27:31 +02:00
}
if self.OnFocus != nil {
return self.OnFocus()
2020-08-19 14:27:31 +02:00
}
return nil
2020-08-16 05:58:29 +02:00
}
func (self *ListContext) HandleRender() error {
return self.OnRender()
2020-08-19 10:06:51 +02:00
}
func (self *ListContext) handlePrevLine() error {
return self.handleLineChange(-1)
2019-11-16 05:00:27 +02:00
}
func (self *ListContext) handleNextLine() error {
return self.handleLineChange(1)
2019-11-16 05:00:27 +02:00
}
func (self *ListContext) handleLineChange(change int) error {
if !self.Gui.isPopupPanel(self.ViewName) && self.Gui.popupPanelFocused() {
2019-11-16 05:00:27 +02:00
return nil
}
selectedLineIdx := self.GetPanelState().GetSelectedLineIdx()
if (change < 0 && selectedLineIdx == 0) || (change > 0 && selectedLineIdx == self.GetItemsLength()-1) {
return nil
}
self.Gui.changeSelectedLine(self.GetPanelState(), self.GetItemsLength(), change)
2019-11-16 05:00:27 +02:00
return self.HandleFocus()
2019-11-16 05:00:27 +02:00
}
func (self *ListContext) handleNextPage() error {
view, err := self.Gui.g.View(self.ViewName)
if err != nil {
return nil
}
delta := self.Gui.pageDelta(view)
return self.handleLineChange(delta)
}
func (self *ListContext) handleGotoTop() error {
return self.handleLineChange(-self.GetItemsLength())
}
func (self *ListContext) handleGotoBottom() error {
return self.handleLineChange(self.GetItemsLength())
}
func (self *ListContext) handlePrevPage() error {
view, err := self.Gui.g.View(self.ViewName)
if err != nil {
return nil
}
delta := self.Gui.pageDelta(view)
return self.handleLineChange(-delta)
}
func (self *ListContext) handleClick() error {
if !self.Gui.isPopupPanel(self.ViewName) && self.Gui.popupPanelFocused() {
return nil
}
view, err := self.Gui.g.View(self.ViewName)
if err != nil {
return nil
}
prevSelectedLineIdx := self.GetPanelState().GetSelectedLineIdx()
newSelectedLineIdx := view.SelectedLineIdx()
2020-08-16 01:18:57 +02:00
// we need to focus the view
if err := self.Gui.pushContext(self); err != nil {
2020-08-16 01:18:57 +02:00
return err
}
if newSelectedLineIdx > self.GetItemsLength()-1 {
2020-08-16 05:58:29 +02:00
return nil
}
self.GetPanelState().SetSelectedLineIdx(newSelectedLineIdx)
prevViewName := self.Gui.currentViewName()
if prevSelectedLineIdx == newSelectedLineIdx && prevViewName == self.ViewName && self.OnClickSelectedItem != nil {
return self.OnClickSelectedItem()
}
return self.HandleFocus()
2020-08-16 01:18:57 +02:00
}
func (self *ListContext) onSearchSelect(selectedLineIdx int) error {
self.GetPanelState().SetSelectedLineIdx(selectedLineIdx)
return self.HandleFocus()
2020-08-16 01:18:57 +02:00
}