1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-26 05:37:18 +02:00
lazygit/pkg/gui/context/list_trait.go
2022-03-17 19:13:40 +11:00

33 lines
772 B
Go

package context
import "github.com/jesseduffield/lazygit/pkg/gui/types"
type HasLength interface {
GetItemsLength() int
}
type ListTrait struct {
selectedIdx int
HasLength
}
var _ types.IListPanelState = (*ListTrait)(nil)
func (self *ListTrait) GetSelectedLineIdx() int {
return self.selectedIdx
}
func (self *ListTrait) SetSelectedLineIdx(value int) {
self.selectedIdx = clamp(value, 0, self.GetItemsLength()-1)
}
// moves the cursor up or down by the given amount
func (self *ListTrait) MoveSelectedLine(value int) {
self.SetSelectedLineIdx(self.selectedIdx + value)
}
// to be called when the model might have shrunk so that our selection is not not out of bounds
func (self *ListTrait) RefreshSelectedIdx() {
self.SetSelectedLineIdx(self.selectedIdx)
}