diff options
-rw-r--r-- | pkg/gui/controllers.go | 22 | ||||
-rw-r--r-- | pkg/gui/controllers/helpers/window_helper.go | 4 | ||||
-rw-r--r-- | pkg/gui/controllers/jump_to_side_window_controller.go | 53 | ||||
-rw-r--r-- | pkg/gui/controllers/side_window_controller.go | 96 | ||||
-rw-r--r-- | pkg/gui/keybindings.go | 29 | ||||
-rw-r--r-- | pkg/gui/side_window.go | 61 |
6 files changed, 175 insertions, 90 deletions
diff --git a/pkg/gui/controllers.go b/pkg/gui/controllers.go index b4af4db7a..88b9dee2e 100644 --- a/pkg/gui/controllers.go +++ b/pkg/gui/controllers.go @@ -161,6 +161,27 @@ func (gui *Gui) resetControllers() { commandLogController := controllers.NewCommandLogController(common) confirmationController := controllers.NewConfirmationController(common) suggestionsController := controllers.NewSuggestionsController(common) + jumpToSideWindowController := controllers.NewJumpToSideWindowController(common) + + sideWindowControllerFactory := controllers.NewSideWindowControllerFactory(common) + + // allow for navigating between side window contexts + for _, context := range []types.Context{ + gui.State.Contexts.Status, + gui.State.Contexts.Remotes, + gui.State.Contexts.Tags, + gui.State.Contexts.Branches, + gui.State.Contexts.RemoteBranches, + gui.State.Contexts.Files, + gui.State.Contexts.Submodules, + gui.State.Contexts.ReflogCommits, + gui.State.Contexts.LocalCommits, + gui.State.Contexts.CommitFiles, + gui.State.Contexts.SubCommits, + gui.State.Contexts.Stash, + } { + controllers.AttachControllers(context, sideWindowControllerFactory.Create(context)) + } setSubCommits := func(commits []*models.Commit) { gui.Mutexes.SubCommitsMutex.Lock() @@ -306,6 +327,7 @@ func (gui *Gui) resetControllers() { undoController, globalController, contextLinesController, + jumpToSideWindowController, ) controllers.AttachControllers(gui.State.Contexts.Snake, diff --git a/pkg/gui/controllers/helpers/window_helper.go b/pkg/gui/controllers/helpers/window_helper.go index 7c4edf35b..efe84c398 100644 --- a/pkg/gui/controllers/helpers/window_helper.go +++ b/pkg/gui/controllers/helpers/window_helper.go @@ -133,3 +133,7 @@ func (self *WindowHelper) WindowForView(viewName string) string { return context.GetWindowName() } + +func (self *WindowHelper) SideWindows() []string { + return []string{"status", "files", "branches", "commits", "stash"} +} diff --git a/pkg/gui/controllers/jump_to_side_window_controller.go b/pkg/gui/controllers/jump_to_side_window_controller.go new file mode 100644 index 000000000..7ac407ab4 --- /dev/null +++ b/pkg/gui/controllers/jump_to_side_window_controller.go @@ -0,0 +1,53 @@ +package controllers + +import ( + "log" + + "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/gui/types" + "github.com/samber/lo" +) + +type JumpToSideWindowController struct { + baseController + c *ControllerCommon +} + +func NewJumpToSideWindowController( + common *ControllerCommon, +) *JumpToSideWindowController { + return &JumpToSideWindowController{ + baseController: baseController{}, + c: common, + } +} + +func (self *JumpToSideWindowController) Context() types.Context { + return nil +} + +func (self *JumpToSideWindowController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding { + windows := self.c.Helpers().Window.SideWindows() + + if len(opts.Config.Universal.JumpToBlock) != len(windows) { + log.Fatal("Jump to block keybindings cannot be set. Exactly 5 keybindings must be supplied.") + } + + return lo.Map(windows, func(window string, index int) *types.Binding { + return &types.Binding{ + ViewName: "", + // by default the keys are 1, 2, 3, etc + Key: opts.GetKey(opts.Config.Universal.JumpToBlock[index]), + Modifier: gocui.ModNone, + Handler: self.goToSideWindow(window), + } + }) +} + +func (self *JumpToSideWindowController) goToSideWindow(window string) func() error { + return func() error { + context := self.c.Helpers().Window.GetContextForWindow(window) + + return self.c.PushContext(context) + } +} diff --git a/pkg/gui/controllers/side_window_controller.go b/pkg/gui/controllers/side_window_controller.go new file mode 100644 index 000000000..a2325c54d --- /dev/null +++ b/pkg/gui/controllers/side_window_controller.go @@ -0,0 +1,96 @@ +package controllers + +import ( + "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/gui/types" +) + +type SideWindowControllerFactory struct { + c *ControllerCommon +} + +func NewSideWindowControllerFactory(common *ControllerCommon) *SideWindowControllerFactory { + return &SideWindowControllerFactory{c: common} +} + +func (self *SideWindowControllerFactory) Create(context types.Context) types.IController { + return NewSideWindowController(self.c, context) +} + +type SideWindowController struct { + baseController + c *ControllerCommon + context types.Context +} + +func NewSideWindowController( + common *ControllerCommon, + context types.Context, +) *SideWindowController { + return &SideWindowController{ + baseController: baseController{}, + c: common, + context: context, + } +} + +func (self *SideWindowController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding { + return []*types.Binding{ + {Key: opts.GetKey(opts.Config.Universal.PrevBlock), Modifier: gocui.ModNone, Handler: self.previousSideWindow}, + {Key: opts.GetKey(opts.Config.Universal.NextBlock), Modifier: gocui.ModNone, Handler: self.nextSideWindow}, + {Key: opts.GetKey(opts.Config.Universal.PrevBlockAlt), Modifier: gocui.ModNone, Handler: self.previousSideWindow}, + {Key: opts.GetKey(opts.Config.Universal.NextBlockAlt), Modifier: gocui.ModNone, Handler: self.nextSideWindow}, + {Key: opts.GetKey(opts.Config.Universal.PrevBlockAlt2), Modifier: gocui.ModNone, Handler: self.previousSideWindow}, + {Key: opts.GetKey(opts.Config.Universal.NextBlockAlt2), Modifier: gocui.ModNone, Handler: self.nextSideWindow}, + } +} + +func (self *SideWindowController) Context() types.Context { + return nil +} + +func (self *SideWindowController) previousSideWindow() error { + windows := self.c.Helpers().Window.SideWindows() + currentWindow := self.c.Helpers().Window.CurrentWindow() + var newWindow string + if currentWindow == "" || currentWindow == windows[0] { + newWindow = windows[len(windows)-1] + } else { + for i := range windows { + if currentWindow == windows[i] { + newWindow = windows[i-1] + break + } + if i == len(windows)-1 { + return nil + } + } + } + + context := self.c.Helpers().Window.GetContextForWindow(newWindow) + + return self.c.PushContext(context) +} + +func (self *SideWindowController) nextSideWindow() error { + windows := self.c.Helpers().Window.SideWindows() + currentWindow := self.c.Helpers().Window.CurrentWindow() + var newWindow string + if currentWindow == "" || currentWindow == windows[len(windows)-1] { + newWindow = windows[0] + } else { + for i := range windows { + if currentWindow == windows[i] { + newWindow = windows[i+1] + break + } + if i == len(windows)-1 { + return nil + } + } + } + + context := self.c.Helpers().Window.GetContextForWindow(newWindow) + + return self.c.PushContext(context) +} diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index fa536b65d..d7fa758fa 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -74,8 +74,6 @@ func (self *Gui) keybindingOpts() types.KeybindingsOpts { // renaming receiver to 'self' to aid refactoring. Will probably end up moving all Gui handlers to this pattern eventually. func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBinding) { - config := self.c.UserConfig.Keybinding - opts := self.c.KeybindingsOpts() bindings := []*types.Binding{ @@ -317,33 +315,6 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi mouseKeybindings = append(mouseKeybindings, c.GetMouseKeybindings(opts)...) } - for _, viewName := range []string{"status", "remotes", "tags", "localBranches", "remoteBranches", "files", "submodules", "reflogCommits", "commits", "commitFiles", "subCommits", "stash"} { - bindings = append(bindings, []*types.Binding{ - {ViewName: viewName, Key: opts.GetKey(opts.Config.Universal.PrevBlock), Modifier: gocui.ModNone, Handler: self.previousSideWindow}, - {ViewName: viewName, Key: opts.GetKey(opts.Config.Universal.NextBlock), Modifier: gocui.ModNone, Handler: self.nextSideWindow}, - {ViewName: viewName, Key: opts.GetKey(opts.Config.Universal.PrevBlockAlt), Modifier: gocui.ModNone, Handler: self.previousSideWindow}, - {ViewName: viewName, Key: opts.GetKey(opts.Config.Universal.NextBlockAlt), Modifier: gocui.ModNone, Handler: self.nextSideWindow}, - {ViewName: viewName, Key: opts.GetKey(opts.Config.Universal.PrevBlockAlt2), Modifier: gocui.ModNone, Handler: self.previousSideWindow}, - {ViewName: viewName, Key: opts.GetKey(opts.Config.Universal.NextBlockAlt2), Modifier: gocui.ModNone, Handler: self.nextSideWindow}, - }...) - } - - // Appends keybindings to jump to a particular sideView using numbers - windows := []string{"status", "files", "branches", "commits", "stash"} - - if len(config.Universal.JumpToBlock) != len(windows) { - log.Fatal("Jump to block keybindings cannot be set. Exactly 5 keybindings must be supplied.") - } else { - for i, window := range windows { - bindings = append(bindings, &types.Binding{ - ViewName: "", - Key: opts.GetKey(opts.Config.Universal.JumpToBlock[i]), - Modifier: gocui.ModNone, - Handler: self.goToSideWindow(window), - }) - } - } - bindings = append(bindings, []*types.Binding{ { ViewName: "", diff --git a/pkg/gui/side_window.go b/pkg/gui/side_window.go deleted file mode 100644 index a1eff7737..000000000 --- a/pkg/gui/side_window.go +++ /dev/null @@ -1,61 +0,0 @@ -package gui - -func (gui *Gui) nextSideWindow() error { - windows := gui.getCyclableWindows() - currentWindow := gui.helpers.Window.CurrentWindow() - var newWindow string - if currentWindow == "" || currentWindow == windows[len(windows)-1] { - newWindow = windows[0] - } else { - for i := range windows { - if currentWindow == windows[i] { - newWindow = windows[i+1] - break - } - if i == len(windows)-1 { - return nil - } - } - } - gui.c.ResetViewOrigin(gui.Views.Main) - - context := gui.helpers.Window.GetContextForWindow(newWindow) - - return gui.c.PushContext(context) -} - -func (gui *Gui) previousSideWindow() error { - windows := gui.getCyclableWindows() - currentWindow := gui.helpers.Window.CurrentWindow() - var newWindow string - if currentWindow == "" || currentWindow == windows[0] { - newWindow = windows[len(windows)-1] - } else { - for i := range windows { - if currentWindow == windows[i] { - newWindow = windows[i-1] - break - } - if i == len(windows)-1 { - return nil - } - } - } - gui.c.ResetViewOrigin(gui.Views.Main) - - context := gui.helpers.Window.GetContextForWindow(newWindow) - - return gui.c.PushContext(context) -} - -func (gui *Gui) goToSideWindow(window string) func() error { - return func() error { - context := gui.helpers.Window.GetContextForWindow(window) - - return gui.c.PushContext(context) - } -} - -func (gui *Gui) getCyclableWindows() []string { - return []string{"status", "files", "branches", "commits", "stash"} -} |