Mapping number keys to jump between side panels

This commit is contained in:
kohath 2024-11-24 22:37:36 +08:00
parent be05115352
commit e58d5b963d
2 changed files with 28 additions and 0 deletions

View file

@ -522,6 +522,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
}...) }...)
} }
// Mapping number keys to jump between side panels
for _, panel := range gui.allSidePanels() {
for idx := 1; idx <= len(gui.allSidePanels()); idx++ {
bindings = append(bindings, &Binding{ViewName: panel.GetView().Name(), Key: rune('0' + idx), Modifier: gocui.ModNone, Handler: wrappedHandlerWithKey(gui.jumpToSideView, idx)})
}
}
setUpDownClickBindings := func(viewName string, onUp func() error, onDown func() error, onClick func() error) { setUpDownClickBindings := func(viewName string, onUp func() error, onDown func() error, onClick func() error) {
bindings = append(bindings, []*Binding{ bindings = append(bindings, []*Binding{
{ViewName: viewName, Key: 'k', Modifier: gocui.ModNone, Handler: wrappedHandler(onUp)}, {ViewName: viewName, Key: 'k', Modifier: gocui.ModNone, Handler: wrappedHandler(onUp)},
@ -602,3 +609,9 @@ func wrappedHandler(f func() error) func(*gocui.Gui, *gocui.View) error {
return f() return f()
} }
} }
func wrappedHandlerWithKey(f func(g *gocui.Gui, key int) error, key int) func(*gocui.Gui, *gocui.View) error {
return func(g *gocui.Gui, v *gocui.View) error {
return f(g, key)
}
}

View file

@ -64,6 +64,21 @@ func (gui *Gui) previousView(g *gocui.Gui, v *gocui.View) error {
return gui.switchFocus(focusedView) return gui.switchFocus(focusedView)
} }
func (gui *Gui) jumpToSideView(g *gocui.Gui, idx int) error {
sideViewNames := gui.sideViewNames()
if idx < 1 || len(sideViewNames) < idx {
gui.Log.Info("not in list of views")
return nil
}
focusedViewName := sideViewNames[idx-1]
focusedView, err := g.View(focusedViewName)
if err != nil {
panic(err)
}
gui.resetMainView()
return gui.switchFocus(focusedView)
}
func (gui *Gui) resetMainView() { func (gui *Gui) resetMainView() {
gui.State.Panels.Main.ObjectKey = "" gui.State.Panels.Main.ObjectKey = ""
gui.Views.Main.Wrap = gui.Config.UserConfig.Gui.WrapMainPanel gui.Views.Main.Wrap = gui.Config.UserConfig.Gui.WrapMainPanel