diff options
Diffstat (limited to 'pkg/gui')
-rw-r--r-- | pkg/gui/controllers.go | 180 | ||||
-rw-r--r-- | pkg/gui/controllers/files_controller.go | 11 | ||||
-rw-r--r-- | pkg/gui/controllers/workspace_reset_controller.go | 108 | ||||
-rw-r--r-- | pkg/gui/gui.go | 168 | ||||
-rw-r--r-- | pkg/gui/keybindings.go | 8 | ||||
-rw-r--r-- | pkg/gui/workspace_reset_options_panel.go | 106 |
6 files changed, 296 insertions, 285 deletions
diff --git a/pkg/gui/controllers.go b/pkg/gui/controllers.go new file mode 100644 index 000000000..0755bbe8e --- /dev/null +++ b/pkg/gui/controllers.go @@ -0,0 +1,180 @@ +package gui + +import ( + "strings" + + "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/commands/models" + "github.com/jesseduffield/lazygit/pkg/gui/controllers" + "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers" + "github.com/jesseduffield/lazygit/pkg/gui/modes/cherrypicking" + "github.com/jesseduffield/lazygit/pkg/gui/services/custom_commands" +) + +func (gui *Gui) resetControllers() { + helperCommon := gui.c + osCommand := gui.os + model := gui.State.Model + refsHelper := helpers.NewRefsHelper( + helperCommon, + gui.git, + gui.State.Contexts, + model, + ) + + rebaseHelper := helpers.NewMergeAndRebaseHelper(helperCommon, gui.State.Contexts, gui.git, gui.takeOverMergeConflictScrolling, refsHelper) + gui.helpers = &helpers.Helpers{ + Refs: refsHelper, + PatchBuilding: helpers.NewPatchBuildingHelper(helperCommon, gui.git), + Bisect: helpers.NewBisectHelper(helperCommon, gui.git), + Suggestions: helpers.NewSuggestionsHelper(helperCommon, model, gui.refreshSuggestions), + Files: helpers.NewFilesHelper(helperCommon, gui.git, osCommand), + WorkingTree: helpers.NewWorkingTreeHelper(model), + Tags: helpers.NewTagsHelper(helperCommon, gui.git), + GPG: helpers.NewGpgHelper(helperCommon, gui.os, gui.git), + MergeAndRebase: rebaseHelper, + CherryPick: helpers.NewCherryPickHelper( + helperCommon, + gui.git, + gui.State.Contexts, + func() *cherrypicking.CherryPicking { return gui.State.Modes.CherryPicking }, + rebaseHelper, + ), + } + + gui.CustomCommandsClient = custom_commands.NewClient( + helperCommon, + gui.os, + gui.git, + gui.State.Contexts, + gui.helpers, + gui.getKey, + ) + + common := controllers.NewControllerCommon( + helperCommon, + osCommand, + gui.git, + gui.helpers, + model, + gui.State.Contexts, + gui.State.Modes, + ) + + syncController := controllers.NewSyncController( + common, + gui.getSuggestedRemote, + ) + + submodulesController := controllers.NewSubmodulesController( + common, + gui.enterSubmodule, + ) + + bisectController := controllers.NewBisectController(common) + + reflogController := controllers.NewReflogController(common) + subCommitsController := controllers.NewSubCommitsController(common) + + getSavedCommitMessage := func() string { + return gui.State.savedCommitMessage + } + + getCommitMessage := func() string { + return strings.TrimSpace(gui.Views.CommitMessage.TextArea.GetContent()) + } + + setCommitMessage := gui.getSetTextareaTextFn(func() *gocui.View { return gui.Views.CommitMessage }) + + onCommitAttempt := func(message string) { + gui.State.savedCommitMessage = message + gui.Views.CommitMessage.ClearTextArea() + } + + onCommitSuccess := func() { + gui.State.savedCommitMessage = "" + } + + commitMessageController := controllers.NewCommitMessageController( + common, + getCommitMessage, + onCommitAttempt, + onCommitSuccess, + ) + + remoteBranchesController := controllers.NewRemoteBranchesController(common) + + gui.Controllers = Controllers{ + Submodules: submodulesController, + Global: controllers.NewGlobalController(common), + Files: controllers.NewFilesController( + common, + gui.enterSubmodule, + setCommitMessage, + getSavedCommitMessage, + gui.switchToMerge, + ), + Tags: controllers.NewTagsController(common), + LocalCommits: controllers.NewLocalCommitsController(common, syncController.HandlePull), + Remotes: controllers.NewRemotesController( + common, + func(branches []*models.RemoteBranch) { gui.State.Model.RemoteBranches = branches }, + ), + Menu: controllers.NewMenuController(common), + Undo: controllers.NewUndoController(common), + Sync: syncController, + } + + branchesController := controllers.NewBranchesController(common) + gitFlowController := controllers.NewGitFlowController(common) + filesRemoveController := controllers.NewFilesRemoveController(common) + stashController := controllers.NewStashController(common) + commitFilesController := controllers.NewCommitFilesController(common) + + switchToSubCommitsControllerFactory := controllers.NewSubCommitsSwitchControllerFactory( + common, + func(commits []*models.Commit) { gui.State.Model.SubCommits = commits }, + ) + + for _, context := range []controllers.ContextWithRefName{ + gui.State.Contexts.Branches, + gui.State.Contexts.RemoteBranches, + gui.State.Contexts.Tags, + } { + controllers.AttachControllers(context, switchToSubCommitsControllerFactory.Create(context)) + } + + commitishControllerFactory := controllers.NewCommitishControllerFactory( + common, + gui.SwitchToCommitFilesContext, + ) + + for _, context := range []controllers.Commitish{ + gui.State.Contexts.LocalCommits, + gui.State.Contexts.ReflogCommits, + gui.State.Contexts.SubCommits, + gui.State.Contexts.Stash, + } { + controllers.AttachControllers(context, commitishControllerFactory.Create(context)) + } + + controllers.AttachControllers(gui.State.Contexts.Branches, branchesController, gitFlowController) + controllers.AttachControllers(gui.State.Contexts.Files, gui.Controllers.Files, filesRemoveController) + controllers.AttachControllers(gui.State.Contexts.Tags, gui.Controllers.Tags) + controllers.AttachControllers(gui.State.Contexts.Submodules, gui.Controllers.Submodules) + controllers.AttachControllers(gui.State.Contexts.LocalCommits, gui.Controllers.LocalCommits, bisectController) + controllers.AttachControllers(gui.State.Contexts.ReflogCommits, reflogController) + controllers.AttachControllers(gui.State.Contexts.SubCommits, subCommitsController) + controllers.AttachControllers(gui.State.Contexts.CommitFiles, commitFilesController) + controllers.AttachControllers(gui.State.Contexts.Remotes, gui.Controllers.Remotes) + controllers.AttachControllers(gui.State.Contexts.Stash, stashController) + controllers.AttachControllers(gui.State.Contexts.Menu, gui.Controllers.Menu) + controllers.AttachControllers(gui.State.Contexts.CommitMessage, commitMessageController) + controllers.AttachControllers(gui.State.Contexts.RemoteBranches, remoteBranchesController) + controllers.AttachControllers(gui.State.Contexts.Global, gui.Controllers.Sync, gui.Controllers.Undo, gui.Controllers.Global) + + listControllerFactory := controllers.NewListControllerFactory(gui.c) + for _, context := range gui.getListContexts() { + controllers.AttachControllers(context, listControllerFactory.Create(context)) + } +} diff --git a/pkg/gui/controllers/files_controller.go b/pkg/gui/controllers/files_controller.go index e12554a3d..5f6ccec7e 100644 --- a/pkg/gui/controllers/files_controller.go +++ b/pkg/gui/controllers/files_controller.go @@ -122,11 +122,16 @@ func (self *FilesController) GetKeybindings(opts types.KeybindingsOpts) []*types }, { Key: opts.GetKey(opts.Config.Commits.ViewResetOptions), - Handler: self.createResetMenu, + Handler: self.createResetToUpstreamMenu, Description: self.c.Tr.LcViewResetToUpstreamOptions, OpensMenu: true, }, - // here + { + Key: opts.GetKey(opts.Config.Files.ViewResetOptions), + Handler: self.createResetMenu, + Description: self.c.Tr.LcViewResetOptions, + OpensMenu: true, + }, { Key: opts.GetKey(opts.Config.Files.ToggleTreeView), Handler: self.toggleTreeView, @@ -571,7 +576,7 @@ func (self *FilesController) stash() error { return self.handleStashSave(self.git.Stash.Save) } -func (self *FilesController) createResetMenu() error { +func (self *FilesController) createResetToUpstreamMenu() error { return self.helpers.Refs.CreateGitResetMenu("@{upstream}") } diff --git a/pkg/gui/controllers/workspace_reset_controller.go b/pkg/gui/controllers/workspace_reset_controller.go new file mode 100644 index 000000000..9153e34e8 --- /dev/null +++ b/pkg/gui/controllers/workspace_reset_controller.go @@ -0,0 +1,108 @@ +package controllers + +import ( + "fmt" + + "github.com/jesseduffield/lazygit/pkg/gui/style" + "github.com/jesseduffield/lazygit/pkg/gui/types" +) + +// this is in its own file given that the workspace controller file is already quite long + +func (self *FilesController) createResetMenu() error { + red := style.FgRed + + nukeStr := "reset --hard HEAD && git clean -fd" + if len(self.model.Submodules) > 0 { + nukeStr = fmt.Sprintf("%s (%s)", nukeStr, self.c.Tr.LcAndResetSubmodules) + } + + menuItems := []*types.MenuItem{ + { + DisplayStrings: []string{ + self.c.Tr.LcDiscardAllChangesToAllFiles, + red.Sprint(nukeStr), + }, + OnPress: func() error { + self.c.LogAction(self.c.Tr.Actions.NukeWorkingTree) + if err := self.git.WorkingTree.ResetAndClean(); err != nil { + return self.c.Error(err) + } + + return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}}) + }, + }, + { + DisplayStrings: []string{ + self.c.Tr.LcDiscardAnyUnstagedChanges, + red.Sprint("git checkout -- ."), + }, + OnPress: func() error { + self.c.LogAction(self.c.Tr.Actions.DiscardUnstagedFileChanges) + if err := self.git.WorkingTree.DiscardAnyUnstagedFileChanges(); err != nil { + return self.c.Error(err) + } + + return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}}) + }, + }, + { + DisplayStrings: []string{ + self.c.Tr.LcDiscardUntrackedFiles, + red.Sprint("git clean -fd"), + }, + OnPress: func() error { + self.c.LogAction(self.c.Tr.Actions.RemoveUntrackedFiles) + if err := self.git.WorkingTree.RemoveUntrackedFiles(); err != nil { + return self.c.Error(err) + } + + return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}}) + }, + }, + { + DisplayStrings: []string{ + self.c.Tr.LcSoftReset, + red.Sprint("git reset --soft HEAD"), + }, + OnPress: func() error { + self.c.LogAction(self.c.Tr.Actions.SoftReset) + if err := self.git.WorkingTree.ResetSoft("HEAD"); err != nil { + return self.c.Error(err) + } + + return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}}) + }, + }, + { + DisplayStrings: []string{ + "mixed reset", + red.Sprint("git reset --mixed HEAD"), + }, + OnPress: func() error { + self.c.LogAction(self.c.Tr.Actions.MixedReset) + if err := self.git.WorkingTree.ResetMixed("HEAD"); err != nil { + return self.c.Error(err) + } + + return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}}) + }, + }, + { + DisplayStrings: []string{ + self.c.Tr.LcHardReset, + red.Sprint("git reset --hard HEAD"), + }, + OnPress: func() error { + self.c.LogAction(self.c.Tr.Actions.HardReset) + if err := self.git.WorkingTree.ResetHard("HEAD"); err != nil { + return self.c.Error(err) + } + + return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}}) + }, + }, + } + + return self.c.Menu(types.CreateMenuOptions{Title: "", Items: menuItems}) +} diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 1cd2229f5..b65493d49 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -498,174 +498,6 @@ func NewGui( return gui, nil } -func (gui *Gui) resetControllers() { - helperCommon := gui.c - osCommand := gui.os - model := gui.State.Model - refsHelper := helpers.NewRefsHelper( - helperCommon, - gui.git, - gui.State.Contexts, - model, - ) - - rebaseHelper := helpers.NewMergeAndRebaseHelper(helperCommon, gui.State.Contexts, gui.git, gui.takeOverMergeConflictScrolling, refsHelper) - gui.helpers = &helpers.Helpers{ - Refs: refsHelper, - PatchBuilding: helpers.NewPatchBuildingHelper(helperCommon, gui.git), - Bisect: helpers.NewBisectHelper(helperCommon, gui.git), - Suggestions: helpers.NewSuggestionsHelper(helperCommon, model, gui.refreshSuggestions), - Files: helpers.NewFilesHelper(helperCommon, gui.git, osCommand), - WorkingTree: helpers.NewWorkingTreeHelper(model), - Tags: helpers.NewTagsHelper(helperCommon, gui.git), - GPG: helpers.NewGpgHelper(helperCommon, gui.os, gui.git), - MergeAndRebase: rebaseHelper, - CherryPick: helpers.NewCherryPickHelper( - helperCommon, - gui.git, - gui.State.Contexts, - func() *cherrypicking.CherryPicking { return gui.State.Modes.CherryPicking }, - rebaseHelper, - ), - } - - gui.CustomCommandsClient = custom_commands.NewClient( - helperCommon, - gui.os, - gui.git, - gui.State.Contexts, - gui.helpers, - gui.getKey, - ) - - common := controllers.NewControllerCommon( - helperCommon, - osCommand, - gui.git, - gui.helpers, - model, - gui.State.Contexts, - gui.State.Modes, - ) - - syncController := controllers.NewSyncController( - common, - gui.getSuggestedRemote, - ) - - submodulesController := controllers.NewSubmodulesController( - common, - gui.enterSubmodule, - ) - - bisectController := controllers.NewBisectController(common) - - reflogController := controllers.NewReflogController(common) - subCommitsController := controllers.NewSubCommitsController(common) - - getSavedCommitMessage := func() string { - return gui.State.savedCommitMessage - } - - getCommitMessage := func() string { - return strings.TrimSpace(gui.Views.CommitMessage.TextArea.GetContent()) - } - - setCommitMessage := gui.getSetTextareaTextFn(func() *gocui.View { return gui.Views.CommitMessage }) - - onCommitAttempt := func(message string) { - gui.State.savedCommitMessage = message - gui.Views.CommitMessage.ClearTextArea() - } - - onCommitSuccess := func() { - gui.State.savedCommitMessage = "" - } - - commitMessageController := controllers.NewCommitMessageController( - common, - getCommitMessage, - onCommitAttempt, - onCommitSuccess, - ) - - remoteBranchesController := controllers.NewRemoteBranchesController(common) - - gui.Controllers = Controllers{ - Submodules: submodulesController, - Global: controllers.NewGlobalController(common), - Files: controllers.NewFilesController( - common, - gui.enterSubmodule, - setCommitMessage, - getSavedCommitMessage, - gui.switchToMerge, - ), - Tags: controllers.NewTagsController(common), - LocalCommits: controllers.NewLocalCommitsController(common, syncController.HandlePull), - Remotes: controllers.NewRemotesController( - common, - func(branches []*models.RemoteBranch) { gui.State.Model.RemoteBranches = branches }, - ), - Menu: controllers.NewMenuController(common), - Undo: controllers.NewUndoController(common), - Sync: syncController, - } - - branchesController := controllers.NewBranchesController(common) - gitFlowController := controllers.NewGitFlowController(common) - filesRemoveController := controllers.NewFilesRemoveController(common) - stashController := controllers.NewStashController(common) - commitFilesController := controllers.NewCommitFilesController(common) - - switchToSubCommitsControllerFactory := controllers.NewSubCommitsSwitchControllerFactory( - common, - func(commits []*models.Commit) { gui.State.Model.SubCommits = commits }, - ) - - for _, context := range []controllers.ContextWithRefName{ - gui.State.Contexts.Branches, - gui.State.Contexts.RemoteBranches, - gui.State.Contexts.Tags, - } { - controllers.AttachControllers(context, switchToSubCommitsControllerFactory.Create(context)) - } - - commitishControllerFactory := controllers.NewCommitishControllerFactory( - common, - gui.SwitchToCommitFilesContext, - ) - - for _, context := range []controllers.Commitish{ - gui.State.Contexts.LocalCommits, - gui.State.Contexts.ReflogCommits, - gui.State.Contexts.SubCommits, - gui.State.Contexts.Stash, - } { - controllers.AttachControllers(context, commitishControllerFactory.Create(context)) - } - - controllers.AttachControllers(gui.State.Contexts.Branches, branchesController, gitFlowController) - controllers.AttachControllers(gui.State.Contexts.Files, gui.Controllers.Files, filesRemoveController) - controllers.AttachControllers(gui.State.Contexts.Tags, gui.Controllers.Tags) - controllers.AttachControllers(gui.State.Contexts.Submodules, gui.Controllers.Submodules) - controllers.AttachControllers(gui.State.Contexts.LocalCommits, gui.Controllers.LocalCommits, bisectController) - controllers.AttachControllers(gui.State.Contexts.ReflogCommits, reflogController) - controllers.AttachControllers(gui.State.Contexts.SubCommits, subCommitsController) - controllers.AttachControllers(gui.State.Contexts.CommitFiles, commitFilesController) - controllers.AttachControllers(gui.State.Contexts.Remotes, gui.Controllers.Remotes) - controllers.AttachControllers(gui.State.Contexts.Stash, stashController) - controllers.AttachControllers(gui.State.Contexts.Menu, gui.Controllers.Menu) - controllers.AttachControllers(gui.State.Contexts.CommitMessage, commitMessageController) - controllers.AttachControllers(gui.State.Contexts.RemoteBranches, remoteBranchesController) - controllers.AttachControllers(gui.State.Contexts.Global, gui.Controllers.Sync, gui.Controllers.Undo, gui.Controllers.Global) - - listControllerFactory := controllers.NewListControllerFactory(gui.c) - for _, context := range gui.getListContexts() { - controllers.AttachControllers(context, listControllerFactory.Create(context)) - } -} - var RuneReplacements = map[rune]string{ // for the commit graph graph.MergeSymbol: "M", diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 4b0fcd99a..12ceff538 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -368,14 +368,6 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi { ViewName: "files", Contexts: []string{string(context.FILES_CONTEXT_KEY)}, - Key: opts.GetKey(opts.Config.Files.ViewResetOptions), - Handler: self.handleCreateResetMenu, - Description: self.c.Tr.LcViewResetOptions, - OpensMenu: true, - }, - { - ViewName: "files", - Contexts: []string{string(context.FILES_CONTEXT_KEY)}, Key: opts.GetKey(opts.Config.Universal.CopyToClipboard), Handler: self.handleCopySelectedSideContextItemToClipboard, Description: self.c.Tr.LcCopyFileNameToClipboard, diff --git a/pkg/gui/workspace_reset_options_panel.go b/pkg/gui/workspace_reset_options_panel.go deleted file mode 100644 index 97984029f..000000000 --- a/pkg/gui/workspace_reset_options_panel.go +++ /dev/null @@ -1,106 +0,0 @@ -package gui - -import ( - "fmt" - - "github.com/jesseduffield/lazygit/pkg/gui/style" - "github.com/jesseduffield/lazygit/pkg/gui/types" -) - -func (gui *Gui) handleCreateResetMenu() error { - red := style.FgRed - - nukeStr := "reset --hard HEAD && git clean -fd" - if len(gui.State.Model.Submodules) > 0 { - nukeStr = fmt.Sprintf("%s (%s)", nukeStr, gui.c.Tr.LcAndResetSubmodules) - } - - menuItems := []*types.MenuItem{ - { - DisplayStrings: []string{ - gui.c.Tr.LcDiscardAllChangesToAllFiles, - red.Sprint(nukeStr), - }, - OnPress: func() error { - gui.c.LogAction(gui.c.Tr.Actions.NukeWorkingTree) - if err := gui.git.WorkingTree.ResetAndClean(); err != nil { - return gui.c.Error(err) - } - - return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}}) - }, - }, - { - DisplayStrings: []string{ - gui.c.Tr.LcDiscardAnyUnstagedChanges, - red.Sprint("git checkout -- ."), - }, - OnPress: func() error { - gui.c.LogAction(gui.c.Tr.Actions.DiscardUnstagedFileChanges) - if err := gui.git.WorkingTree.DiscardAnyUnstagedFileChanges(); err != nil { - return gui.c.Error(err) - } - - return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}}) - }, - }, - { - DisplayStrings: []string{ - gui.c.Tr.LcDiscardUntrackedFiles, - red.Sprint("git clean -fd"), - }, - OnPress: func() error { - gui.c.LogAction(gui.c.Tr.Actions.RemoveUntrackedFiles) - if err := gui.git.WorkingTree.RemoveUntrackedFiles(); err != nil { - return gui.c.Error(err) - } - - return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}}) - }, - }, - { - DisplayStrings: []string{ - gui.c.Tr.LcSoftReset, - red.Sprint("git reset --soft HEAD"), - }, - OnPress: func() error { - gui.c.LogAction(gui.c.Tr.Actions.SoftReset) - if err := gui.git.WorkingTree.ResetSoft("HEAD"); err != nil { - return gui.c.Error(err) - } - - return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}}) - }, - }, - { - DisplayStrings: []string{ - "mixed reset", - red.Sprint("git reset --mixed HEAD"), - }, - OnPress: func() error { - gui.c.LogAction(gui.c.Tr.Actions.MixedReset) - if err := gui.git.WorkingTree.ResetMixed("HEAD"); err != nil { - return gui.c.Error(err) - } - - return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}}) - }, - }, - { - DisplayStrings: []string{ - gui.c.Tr.LcHardReset, - red.Sprint("git reset --hard HEAD"), - }, - OnPress: func() error { - gui.c.LogAction(gui.c.Tr.Actions.HardReset) - if err := gui.git.WorkingTree.ResetHard("HEAD"); err != nil { - return gui.c.Error(err) - } - - return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}}) - }, - }, - } - - return gui.c.Menu(types.CreateMenuOptions{Title: "", Items: menuItems}) -} |