move more view model logic into the files view model

This commit is contained in:
Jesse Duffield
2022-01-30 14:46:46 +11:00
parent 8ea7b7a62e
commit c084abb378
17 changed files with 392 additions and 323 deletions

View File

@@ -0,0 +1,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()
}