1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-03 13:21:56 +02:00
lazygit/pkg/gui/context/working_tree_context.go

68 lines
1.7 KiB
Go
Raw Normal View History

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