1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/gui/context/working_tree_context.go

66 lines
1.7 KiB
Go
Raw Normal View History

2022-01-30 04:08:09 +02:00
package context
import (
2023-03-21 12:16:30 +02:00
"github.com/jesseduffield/generics/slices"
2022-01-30 04:08:09 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/filetree"
2023-03-21 12:16:30 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
2022-01-30 04:08:09 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
type WorkingTreeContext struct {
*filetree.FileTreeViewModel
2022-01-30 04:08:09 +02:00
*ListContextTrait
*SearchTrait
2022-01-30 04:08:09 +02:00
}
var _ types.IListContext = (*WorkingTreeContext)(nil)
2022-01-30 04:08:09 +02:00
func NewWorkingTreeContext(c *ContextCommon) *WorkingTreeContext {
2023-05-27 11:58:48 +02:00
viewModel := filetree.NewFileTreeViewModel(
func() []*models.File { return c.Model().Files },
2023-03-21 12:16:30 +02:00
c.Log,
c.UserConfig.Gui.ShowFileTree,
)
getDisplayStrings := func(startIdx int, length int) [][]string {
lines := presentation.RenderFileTree(viewModel, c.Modes().Diffing.Ref, c.Model().Submodules)
return slices.Map(lines, func(line string) []string {
return []string{line}
})
}
2022-01-30 04:08:09 +02:00
ctx := &WorkingTreeContext{
SearchTrait: NewSearchTrait(c),
2022-02-05 08:04:10 +02:00
FileTreeViewModel: viewModel,
ListContextTrait: &ListContextTrait{
Context: NewSimpleContext(NewBaseContext(NewBaseContextOpts{
2023-03-21 12:06:39 +02:00
View: c.Views().Files,
2022-02-05 08:04:10 +02:00
WindowName: "files",
Key: FILES_CONTEXT_KEY,
Kind: types.SIDE_CONTEXT,
Focusable: true,
2023-03-21 12:01:58 +02:00
})),
2022-02-05 08:04:10 +02:00
list: viewModel,
getDisplayStrings: getDisplayStrings,
c: c,
},
2022-01-30 04:08:09 +02:00
}
ctx.GetView().SetOnSelectItem(ctx.SearchTrait.onSelectItemWrapper(func(selectedLineIdx int) error {
ctx.GetList().SetSelectedLineIdx(selectedLineIdx)
return ctx.HandleFocus(types.OnFocusOpts{})
}))
return ctx
2022-01-30 04:08:09 +02:00
}
2022-01-30 11:03:08 +02:00
func (self *WorkingTreeContext) GetSelectedItemId() string {
2022-03-19 00:31:52 +02:00
item := self.GetSelected()
2022-01-30 11:03:08 +02:00
if item == nil {
return ""
}
return item.ID()
2022-01-30 04:08:09 +02:00
}