1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-04 03:48:07 +02:00
lazygit/pkg/integration/components/views.go

225 lines
6.0 KiB
Go
Raw Normal View History

2022-12-26 02:12:56 +02:00
package components
import (
"fmt"
"strings"
2022-12-26 02:12:56 +02:00
"github.com/go-errors/errors"
2022-12-26 02:12:56 +02:00
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/context"
2022-12-26 02:12:56 +02:00
)
2022-12-27 06:22:31 +02:00
type Views struct {
2022-12-27 12:35:36 +02:00
t *TestDriver
2022-12-27 06:56:02 +02:00
}
2022-12-28 02:27:48 +02:00
func (self *Views) Main() *ViewDriver {
return &ViewDriver{
2022-12-27 06:56:02 +02:00
context: "main view",
2022-12-27 12:35:36 +02:00
getView: func() *gocui.View { return self.t.gui.MainView() },
t: self.t,
2022-12-27 06:56:02 +02:00
}
}
2022-12-28 02:27:48 +02:00
func (self *Views) Secondary() *ViewDriver {
return &ViewDriver{
2022-12-27 06:56:02 +02:00
context: "secondary view",
2022-12-27 12:35:36 +02:00
getView: func() *gocui.View { return self.t.gui.SecondaryView() },
t: self.t,
2022-12-27 06:56:02 +02:00
}
}
func (self *Views) regularView(viewName string) *ViewDriver {
2023-02-25 04:08:45 +02:00
return self.newStaticViewDriver(viewName, nil, nil, nil)
}
func (self *Views) patchExplorerViewByName(viewName string) *ViewDriver {
2023-02-25 04:08:45 +02:00
return self.newStaticViewDriver(
viewName,
func() ([]string, error) {
ctx := self.t.gui.ContextForView(viewName).(*context.PatchExplorerContext)
state := ctx.GetState()
if state == nil {
return nil, errors.New("Expected patch explorer to be activated")
}
selectedContent := state.PlainRenderSelected()
// the above method returns a string with a trailing newline so we need to remove that before splitting
selectedLines := strings.Split(strings.TrimSuffix(selectedContent, "\n"), "\n")
return selectedLines, nil
},
func() (int, int, error) {
ctx := self.t.gui.ContextForView(viewName).(*context.PatchExplorerContext)
state := ctx.GetState()
if state == nil {
return 0, 0, errors.New("Expected patch explorer to be activated")
}
startIdx, endIdx := state.SelectedRange()
return startIdx, endIdx, nil
},
func() (int, error) {
ctx := self.t.gui.ContextForView(viewName).(*context.PatchExplorerContext)
state := ctx.GetState()
if state == nil {
return 0, errors.New("Expected patch explorer to be activated")
}
return state.GetSelectedLineIdx(), nil
},
)
}
// 'static' because it'll always refer to the same view, as opposed to the 'main' view which could actually be
// one of several views, or the 'current' view which depends on focus.
2023-02-25 04:08:45 +02:00
func (self *Views) newStaticViewDriver(
viewName string,
getSelectedLinesFn func() ([]string, error),
getSelectedLineRangeFn func() (int, int, error),
getSelectedLineIdxFn func() (int, error),
) *ViewDriver {
2022-12-28 02:27:48 +02:00
return &ViewDriver{
2023-02-25 04:08:45 +02:00
context: fmt.Sprintf("%s view", viewName),
getView: func() *gocui.View { return self.t.gui.View(viewName) },
getSelectedLinesFn: getSelectedLinesFn,
getSelectedRangeFn: getSelectedLineRangeFn,
getSelectedLineIdxFn: getSelectedLineIdxFn,
t: self.t,
2022-12-27 06:56:02 +02:00
}
}
2023-02-25 04:08:45 +02:00
func (self *Views) MergeConflicts() *ViewDriver {
viewName := "mergeConflicts"
return self.newStaticViewDriver(
viewName,
func() ([]string, error) {
ctx := self.t.gui.ContextForView(viewName).(*context.MergeConflictsContext)
state := ctx.GetState()
if state == nil {
return nil, errors.New("Expected patch explorer to be activated")
}
selectedContent := strings.Split(state.PlainRenderSelected(), "\n")
return selectedContent, nil
},
func() (int, int, error) {
ctx := self.t.gui.ContextForView(viewName).(*context.MergeConflictsContext)
state := ctx.GetState()
if state == nil {
return 0, 0, errors.New("Expected patch explorer to be activated")
}
startIdx, endIdx := state.GetSelectedRange()
return startIdx, endIdx, nil
},
// there is no concept of a cursor in the merge conflicts panel so we just return the start of the selection
func() (int, error) {
ctx := self.t.gui.ContextForView(viewName).(*context.MergeConflictsContext)
state := ctx.GetState()
if state == nil {
return 0, errors.New("Expected patch explorer to be activated")
}
startIdx, _ := state.GetSelectedRange()
return startIdx, nil
},
)
}
2022-12-28 02:27:48 +02:00
func (self *Views) Commits() *ViewDriver {
return self.regularView("commits")
2022-12-26 02:12:56 +02:00
}
2022-12-28 02:27:48 +02:00
func (self *Views) Files() *ViewDriver {
return self.regularView("files")
2022-12-26 02:12:56 +02:00
}
2023-07-17 14:03:51 +02:00
func (self *Views) Worktrees() *ViewDriver {
return self.regularView("worktrees")
}
2022-12-28 02:27:48 +02:00
func (self *Views) Status() *ViewDriver {
return self.regularView("status")
2022-12-26 02:12:56 +02:00
}
2022-12-28 02:27:48 +02:00
func (self *Views) Submodules() *ViewDriver {
return self.regularView("submodules")
2022-12-26 02:12:56 +02:00
}
2022-12-28 02:27:48 +02:00
func (self *Views) Information() *ViewDriver {
return self.regularView("information")
}
2023-01-26 04:25:56 +02:00
func (self *Views) AppStatus() *ViewDriver {
return self.regularView("appStatus")
2023-01-26 04:25:56 +02:00
}
2022-12-28 02:27:48 +02:00
func (self *Views) Branches() *ViewDriver {
return self.regularView("localBranches")
}
2022-12-26 02:12:56 +02:00
2023-02-19 04:38:15 +02:00
func (self *Views) Remotes() *ViewDriver {
return self.regularView("remotes")
2023-02-19 04:38:15 +02:00
}
2022-12-28 02:27:48 +02:00
func (self *Views) RemoteBranches() *ViewDriver {
return self.regularView("remoteBranches")
2022-12-26 02:12:56 +02:00
}
2022-12-28 02:27:48 +02:00
func (self *Views) Tags() *ViewDriver {
return self.regularView("tags")
}
2022-12-26 02:12:56 +02:00
2022-12-28 02:27:48 +02:00
func (self *Views) ReflogCommits() *ViewDriver {
return self.regularView("reflogCommits")
2022-12-26 02:12:56 +02:00
}
2022-12-28 02:27:48 +02:00
func (self *Views) SubCommits() *ViewDriver {
return self.regularView("subCommits")
}
2022-12-26 02:12:56 +02:00
2022-12-28 02:27:48 +02:00
func (self *Views) CommitFiles() *ViewDriver {
return self.regularView("commitFiles")
2022-12-26 02:12:56 +02:00
}
2022-12-28 02:27:48 +02:00
func (self *Views) Stash() *ViewDriver {
return self.regularView("stash")
}
2022-12-26 02:12:56 +02:00
2022-12-28 02:27:48 +02:00
func (self *Views) Staging() *ViewDriver {
return self.patchExplorerViewByName("staging")
2022-12-26 02:12:56 +02:00
}
2022-12-27 06:22:31 +02:00
2022-12-28 02:27:48 +02:00
func (self *Views) StagingSecondary() *ViewDriver {
return self.patchExplorerViewByName("stagingSecondary")
}
func (self *Views) PatchBuilding() *ViewDriver {
return self.patchExplorerViewByName("patchBuilding")
}
func (self *Views) PatchBuildingSecondary() *ViewDriver {
// this is not a patch explorer view because you can't actually focus it: it
// just renders content
return self.regularView("patchBuildingSecondary")
}
2022-12-27 06:22:31 +02:00
2022-12-28 02:27:48 +02:00
func (self *Views) Menu() *ViewDriver {
return self.regularView("menu")
}
2022-12-27 06:22:31 +02:00
2022-12-28 02:27:48 +02:00
func (self *Views) Confirmation() *ViewDriver {
return self.regularView("confirmation")
}
2022-12-27 06:22:31 +02:00
2022-12-28 02:27:48 +02:00
func (self *Views) CommitMessage() *ViewDriver {
return self.regularView("commitMessage")
}
2022-12-27 06:22:31 +02:00
func (self *Views) CommitDescription() *ViewDriver {
return self.regularView("commitDescription")
}
2022-12-28 02:27:48 +02:00
func (self *Views) Suggestions() *ViewDriver {
return self.regularView("suggestions")
}
2022-12-27 06:56:02 +02:00
2023-02-22 12:57:32 +02:00
func (self *Views) Search() *ViewDriver {
return self.regularView("search")
2023-02-22 12:57:32 +02:00
}