summaryrefslogtreecommitdiffstats
path: root/pkg/gui/context/traits/list_cursor.go
blob: 9423ad89cea13565ca22052bc5a39f17cc8b5612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package traits

import (
	"github.com/jesseduffield/lazygit/pkg/gui/types"
	"github.com/jesseduffield/lazygit/pkg/utils"
)

type HasLength interface {
	GetItemsLength() int
}

type ListCursor struct {
	selectedIdx int
	list        HasLength
}

func NewListCursor(list HasLength) *ListCursor {
	return &ListCursor{selectedIdx: 0, list: list}
}

var _ types.IListCursor = (*ListCursor)(nil)

func (self *ListCursor) GetSelectedLineIdx() int {
	return self.selectedIdx
}

func (self *ListCursor) SetSelectedLineIdx(value int) {
	self.selectedIdx = utils.Clamp(value, 0, self.list.GetItemsLength()-1)
}

// moves the cursor up or down by the given amount
func (self *ListCursor) MoveSelectedLine(delta int) {
	self.SetSelectedLineIdx(self.selectedIdx + delta)
}

// to be called when the model might have shrunk so that our selection is not not out of bounds
func (self *ListCursor) RefreshSelectedIdx() {
	self.SetSelectedLineIdx(self.selectedIdx)
}

func (self *ListCursor) GetItemsLength() int {
	return self.list.GetItemsLength()
}