summaryrefslogtreecommitdiffstats
path: root/pkg/gui/context/list_context_trait.go
blob: a10f0e3e9dad9920473423b51b808277941b2277 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package context

import (
	"fmt"

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

type ListContextTrait struct {
	types.Context

	c                 *types.HelperCommon
	list              types.IList
	viewTrait         *ViewTrait
	getDisplayStrings func(startIdx int, length int) [][]string
}

func (self *ListContextTrait) GetList() types.IList {
	return self.list
}

func (self *ListContextTrait) GetViewTrait() types.IViewTrait {
	return self.viewTrait
}

func (self *ListContextTrait) FocusLine() {
	// we need a way of knowing whether we've rendered to the view yet.
	self.viewTrait.FocusPoint(self.list.GetSelectedLineIdx())
	self.viewTrait.SetFooter(formatListFooter(self.list.GetSelectedLineIdx(), self.list.Len()))
}

func formatListFooter(selectedLineIdx int, length int) string {
	return fmt.Sprintf("%d of %d", selectedLineIdx+1, length)
}

func (self *ListContextTrait) HandleFocus(opts ...types.OnFocusOpts) error {
	self.FocusLine()

	return self.Context.HandleFocus(opts...)
}

func (self *ListContextTrait) HandleFocusLost() error {
	self.viewTrait.SetOriginX(0)

	return self.Context.HandleFocusLost()
}

// 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 *ListContextTrait) HandleRender() error {
	self.list.RefreshSelectedIdx()
	content := utils.RenderDisplayStrings(self.getDisplayStrings(0, self.list.Len()))
	self.viewTrait.SetContent(content)
	self.c.Render()

	return nil
}

func (self *ListContextTrait) OnSearchSelect(selectedLineIdx int) error {
	self.GetList().SetSelectedLineIdx(selectedLineIdx)
	return self.HandleFocus()
}