1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-22 05:29:44 +02:00

168 lines
4.3 KiB
Go
Raw Normal View History

2022-12-26 11:12:56 +11:00
package components
import (
"fmt"
"strings"
2022-12-26 11:12:56 +11:00
"github.com/go-errors/errors"
2022-12-26 11:12:56 +11:00
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/context"
2022-12-26 11:12:56 +11:00
)
2022-12-27 15:22:31 +11:00
type Views struct {
2022-12-27 21:35:36 +11:00
t *TestDriver
2022-12-27 15:56:02 +11:00
}
// not exporting this because I want the test to always be explicit about what
// view it's dealing with.
2022-12-28 11:27:48 +11:00
func (self *Views) current() *ViewDriver {
return &ViewDriver{
2022-12-27 15:56:02 +11:00
context: "current view",
2022-12-27 21:35:36 +11:00
getView: func() *gocui.View { return self.t.gui.CurrentContext().GetView() },
t: self.t,
2022-12-27 15:56:02 +11:00
}
}
2022-12-28 11:27:48 +11:00
func (self *Views) Main() *ViewDriver {
return &ViewDriver{
2022-12-27 15:56:02 +11:00
context: "main view",
2022-12-27 21:35:36 +11:00
getView: func() *gocui.View { return self.t.gui.MainView() },
t: self.t,
2022-12-27 15:56:02 +11:00
}
}
2022-12-28 11:27:48 +11:00
func (self *Views) Secondary() *ViewDriver {
return &ViewDriver{
2022-12-27 15:56:02 +11:00
context: "secondary view",
2022-12-27 21:35:36 +11:00
getView: func() *gocui.View { return self.t.gui.SecondaryView() },
t: self.t,
2022-12-27 15:56:02 +11:00
}
}
func (self *Views) regularView(viewName string) *ViewDriver {
return self.newStaticViewDriver(viewName, nil)
}
func (self *Views) patchExplorerViewByName(viewName string) *ViewDriver {
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
})
}
// '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.
func (self *Views) newStaticViewDriver(viewName string, getSelectedLinesFn func() ([]string, error)) *ViewDriver {
2022-12-28 11:27:48 +11:00
return &ViewDriver{
context: fmt.Sprintf("%s view", viewName),
getView: func() *gocui.View { return self.t.gui.View(viewName) },
getSelectedLinesFn: getSelectedLinesFn,
t: self.t,
2022-12-27 15:56:02 +11:00
}
}
2022-12-28 11:27:48 +11:00
func (self *Views) Commits() *ViewDriver {
return self.regularView("commits")
2022-12-26 11:12:56 +11:00
}
2022-12-28 11:27:48 +11:00
func (self *Views) Files() *ViewDriver {
return self.regularView("files")
2022-12-26 11:12:56 +11:00
}
2022-12-28 11:27:48 +11:00
func (self *Views) Status() *ViewDriver {
return self.regularView("status")
2022-12-26 11:12:56 +11:00
}
2022-12-28 11:27:48 +11:00
func (self *Views) Submodules() *ViewDriver {
return self.regularView("submodules")
2022-12-26 11:12:56 +11:00
}
2022-12-28 11:27:48 +11:00
func (self *Views) Information() *ViewDriver {
return self.regularView("information")
}
2023-01-26 13:25:56 +11:00
func (self *Views) AppStatus() *ViewDriver {
return self.regularView("appStatus")
2023-01-26 13:25:56 +11:00
}
2022-12-28 11:27:48 +11:00
func (self *Views) Branches() *ViewDriver {
return self.regularView("localBranches")
}
2022-12-26 11:12:56 +11:00
2023-02-19 13:38:15 +11:00
func (self *Views) Remotes() *ViewDriver {
return self.regularView("remotes")
2023-02-19 13:38:15 +11:00
}
2022-12-28 11:27:48 +11:00
func (self *Views) RemoteBranches() *ViewDriver {
return self.regularView("remoteBranches")
2022-12-26 11:12:56 +11:00
}
2022-12-28 11:27:48 +11:00
func (self *Views) Tags() *ViewDriver {
return self.regularView("tags")
}
2022-12-26 11:12:56 +11:00
2022-12-28 11:27:48 +11:00
func (self *Views) ReflogCommits() *ViewDriver {
return self.regularView("reflogCommits")
2022-12-26 11:12:56 +11:00
}
2022-12-28 11:27:48 +11:00
func (self *Views) SubCommits() *ViewDriver {
return self.regularView("subCommits")
}
2022-12-26 11:12:56 +11:00
2022-12-28 11:27:48 +11:00
func (self *Views) CommitFiles() *ViewDriver {
return self.regularView("commitFiles")
2022-12-26 11:12:56 +11:00
}
2022-12-28 11:27:48 +11:00
func (self *Views) Stash() *ViewDriver {
return self.regularView("stash")
}
2022-12-26 11:12:56 +11:00
2022-12-28 11:27:48 +11:00
func (self *Views) Staging() *ViewDriver {
return self.patchExplorerViewByName("staging")
2022-12-26 11:12:56 +11:00
}
2022-12-27 15:22:31 +11:00
2022-12-28 11:27:48 +11: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 15:22:31 +11:00
2022-12-28 11:27:48 +11:00
func (self *Views) Menu() *ViewDriver {
return self.regularView("menu")
}
2022-12-27 15:22:31 +11:00
2022-12-28 11:27:48 +11:00
func (self *Views) Confirmation() *ViewDriver {
return self.regularView("confirmation")
}
2022-12-27 15:22:31 +11:00
2022-12-28 11:27:48 +11:00
func (self *Views) CommitMessage() *ViewDriver {
return self.regularView("commitMessage")
}
2022-12-27 15:22:31 +11:00
2022-12-28 11:27:48 +11:00
func (self *Views) Suggestions() *ViewDriver {
return self.regularView("suggestions")
}
2022-12-27 15:56:02 +11:00
2022-12-28 11:27:48 +11:00
func (self *Views) MergeConflicts() *ViewDriver {
return self.regularView("mergeConflicts")
}
2023-02-22 21:57:32 +11:00
func (self *Views) Search() *ViewDriver {
return self.regularView("search")
2023-02-22 21:57:32 +11:00
}