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

189 lines
4.3 KiB
Go
Raw Normal View History

2019-11-16 05:00:27 +02:00
package gui
2020-08-17 13:58:30 +02:00
type ListContext struct {
GetItemsLength func() int
GetDisplayStrings func() [][]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)
GetPanelState 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
}
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 (lc *ListContext) GetSelectedItem() (ListItem, bool) {
return lc.SelectedItem()
}
2020-08-20 00:24:35 +02:00
func (lc *ListContext) GetSelectedItemId() string {
item, ok := lc.SelectedItem()
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 (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.GetPanelState(), lc.GetItemsLength())
2020-08-19 13:51:50 +02:00
lc.Gui.renderDisplayStrings(view, lc.GetDisplayStrings())
}
return nil
}
2020-08-17 13:58:30 +02:00
func (lc *ListContext) HandleFocusLost() error {
if lc.OnFocusLost != nil {
return lc.OnFocusLost()
2020-08-16 05:58:29 +02:00
}
return nil
}
2020-08-17 13:58:30 +02:00
func (lc *ListContext) HandleFocus() error {
2020-08-19 14:27:31 +02:00
if lc.Gui.popupPanelFocused() {
return nil
}
2020-08-22 00:49:02 +02:00
view, err := lc.Gui.g.View(lc.ViewName)
if err != nil {
return nil
}
view.FocusPoint(0, lc.GetPanelState().GetSelectedLineIdx())
if lc.Gui.State.Modes.Diffing.Active() {
2020-08-19 14:27:31 +02:00
return lc.Gui.renderDiff()
}
if lc.OnFocus != nil {
return lc.OnFocus()
}
return nil
2020-08-16 05:58:29 +02:00
}
2020-08-19 10:06:51 +02:00
func (lc *ListContext) HandleRender() error {
return lc.OnRender()
}
func (lc *ListContext) handlePrevLine() error {
2020-08-17 13:58:30 +02:00
return lc.handleLineChange(-1)
2019-11-16 05:00:27 +02:00
}
func (lc *ListContext) handleNextLine() error {
2020-08-17 13:58:30 +02:00
return lc.handleLineChange(1)
2019-11-16 05:00:27 +02:00
}
2020-08-17 13:58:30 +02:00
func (lc *ListContext) handleLineChange(change int) error {
if !lc.Gui.isPopupPanel(lc.ViewName) && lc.Gui.popupPanelFocused() {
2019-11-16 05:00:27 +02:00
return nil
}
2020-08-17 13:58:30 +02:00
view, err := lc.Gui.g.View(lc.ViewName)
2020-08-16 01:18:57 +02:00
if err != nil {
return err
}
selectedLineIdx := lc.GetPanelState().GetSelectedLineIdx()
if (change < 0 && selectedLineIdx == 0) || (change > 0 && selectedLineIdx == lc.GetItemsLength()-1) {
return nil
}
lc.Gui.changeSelectedLine(lc.GetPanelState(), lc.GetItemsLength(), change)
view.FocusPoint(0, lc.GetPanelState().GetSelectedLineIdx())
2019-11-16 05:00:27 +02:00
2020-08-19 14:27:31 +02:00
return lc.HandleFocus()
2019-11-16 05:00:27 +02:00
}
func (lc *ListContext) handleNextPage() error {
2020-08-17 13:58:30 +02:00
view, err := lc.Gui.g.View(lc.ViewName)
if err != nil {
return nil
}
delta := lc.Gui.pageDelta(view)
2020-08-17 13:58:30 +02:00
return lc.handleLineChange(delta)
}
func (lc *ListContext) handleGotoTop() error {
2020-08-17 13:58:30 +02:00
return lc.handleLineChange(-lc.GetItemsLength())
}
func (lc *ListContext) handleGotoBottom() error {
2020-08-17 13:58:30 +02:00
return lc.handleLineChange(lc.GetItemsLength())
}
func (lc *ListContext) handlePrevPage() error {
2020-08-17 13:58:30 +02:00
view, err := lc.Gui.g.View(lc.ViewName)
if err != nil {
return nil
}
delta := lc.Gui.pageDelta(view)
2020-08-17 13:58:30 +02:00
return lc.handleLineChange(-delta)
}
func (lc *ListContext) handleClick() error {
2020-08-17 13:58:30 +02:00
if !lc.Gui.isPopupPanel(lc.ViewName) && lc.Gui.popupPanelFocused() {
return nil
}
view, err := lc.Gui.g.View(lc.ViewName)
if err != nil {
return nil
}
prevSelectedLineIdx := lc.GetPanelState().GetSelectedLineIdx()
newSelectedLineIdx := view.SelectedLineIdx()
2020-08-16 01:18:57 +02:00
// we need to focus the view
if err := lc.Gui.pushContext(lc); err != nil {
2020-08-16 01:18:57 +02:00
return err
}
2020-08-17 13:58:30 +02:00
if newSelectedLineIdx > lc.GetItemsLength()-1 {
2020-08-16 05:58:29 +02:00
return nil
}
lc.GetPanelState().SetSelectedLineIdx(newSelectedLineIdx)
2020-08-17 13:58:30 +02:00
prevViewName := lc.Gui.currentViewName()
if prevSelectedLineIdx == newSelectedLineIdx && prevViewName == lc.ViewName && lc.OnClickSelectedItem != nil {
return lc.OnClickSelectedItem()
}
2020-08-19 14:27:31 +02:00
return lc.HandleFocus()
2020-08-16 01:18:57 +02:00
}
2020-08-17 13:58:30 +02:00
func (lc *ListContext) onSearchSelect(selectedLineIdx int) error {
lc.GetPanelState().SetSelectedLineIdx(selectedLineIdx)
2020-08-19 14:27:31 +02:00
return lc.HandleFocus()
2020-08-16 01:18:57 +02:00
}