From 9337f63ed2f2446acde94760868cc53e930b94bc Mon Sep 17 00:00:00 2001 From: Himanshu Date: Thu, 22 Apr 2021 07:29:05 +0530 Subject: [PATCH] Add option for custom keybinds for navigation (#222) Co-authored-by: makeworld <25111343+makeworld-the-better-one@users.noreply.github.com> --- config/config.go | 4 ++++ config/default.go | 4 ++++ config/keybindings.go | 14 ++++++++++++++ default-config.toml | 4 ++++ display/help.go | 6 +++++- display/tab.go | 19 +++++++------------ 6 files changed, 38 insertions(+), 13 deletions(-) diff --git a/config/config.go b/config/config.go index da99831..7c56a1b 100644 --- a/config/config.go +++ b/config/config.go @@ -210,6 +210,10 @@ func Init() error { viper.SetDefault("keybindings.bind_sub", "Ctrl-A") viper.SetDefault("keybindings.bind_add_sub", "Ctrl-X") viper.SetDefault("keybindings.bind_save", "Ctrl-S") + viper.SetDefault("keybindings.bind_moveup", "k") + viper.SetDefault("keybindings.bind_movedown", "j") + viper.SetDefault("keybindings.bind_moveleft", "h") + viper.SetDefault("keybindings.bind_moveright", "l") viper.SetDefault("keybindings.bind_pgup", []string{"PgUp", "u"}) viper.SetDefault("keybindings.bind_pgdn", []string{"PgDn", "d"}) viper.SetDefault("keybindings.bind_bottom", "Space") diff --git a/config/default.go b/config/default.go index 410f740..4de5f15 100644 --- a/config/default.go +++ b/config/default.go @@ -148,6 +148,10 @@ scrollbar = "auto" # bind_reload # bind_back # bind_forward +# bind_moveup +# bind_movedown +# bind_moveleft +# bind_moveright # bind_pgup # bind_pgdn # bind_new_tab diff --git a/config/keybindings.go b/config/keybindings.go index ee5a7a9..348a412 100644 --- a/config/keybindings.go +++ b/config/keybindings.go @@ -3,6 +3,7 @@ package config import ( "strings" + "code.rocketnine.space/tslocum/cview" "github.com/gdamore/tcell/v2" "github.com/spf13/viper" ) @@ -42,6 +43,10 @@ const ( CmdReload CmdBack CmdForward + CmdMoveUp + CmdMoveDown + CmdMoveLeft + CmdMoveRight CmdPgup CmdPgdn CmdNewTab @@ -168,6 +173,10 @@ func KeyInit() { CmdReload: "keybindings.bind_reload", CmdBack: "keybindings.bind_back", CmdForward: "keybindings.bind_forward", + CmdMoveUp: "keybindings.bind_moveup", + CmdMoveDown: "keybindings.bind_movedown", + CmdMoveLeft: "keybindings.bind_moveleft", + CmdMoveRight: "keybindings.bind_moveright", CmdPgup: "keybindings.bind_pgup", CmdPgdn: "keybindings.bind_pgdn", CmdNewTab: "keybindings.bind_new_tab", @@ -203,6 +212,11 @@ func KeyInit() { tcellKeys[kname] = k } + cview.Keys.MoveUp2 = viper.GetStringSlice(configBindings[CmdMoveUp]) + cview.Keys.MoveDown2 = viper.GetStringSlice(configBindings[CmdMoveDown]) + cview.Keys.MoveLeft2 = viper.GetStringSlice(configBindings[CmdMoveLeft]) + cview.Keys.MoveRight2 = viper.GetStringSlice(configBindings[CmdMoveRight]) + for c, allb := range configBindings { for _, b := range viper.GetStringSlice(allb) { parseBinding(c, b) diff --git a/default-config.toml b/default-config.toml index f63c917..5cf386f 100644 --- a/default-config.toml +++ b/default-config.toml @@ -145,6 +145,10 @@ scrollbar = "auto" # bind_reload # bind_back # bind_forward +# bind_moveup +# bind_movedown +# bind_moveleft +# bind_moveright # bind_pgup # bind_pgdn # bind_new_tab diff --git a/display/help.go b/display/help.go index 9d1d927..378741b 100644 --- a/display/help.go +++ b/display/help.go @@ -13,7 +13,7 @@ import ( var helpCells = strings.TrimSpace( "?\tBring up this help. You can scroll!\n" + "Esc\tLeave the help\n" + - "Arrow keys, h/j/k/l\tScroll and move a page.\n" + + "Arrow keys, %s(left)/%s(down)/%s(up)/%s(right)\tScroll and move a page.\n" + "%s\tGo up a page in document\n" + "%s\tGo down a page in document\n" + "g\tGo to top of document\n" + @@ -80,6 +80,10 @@ func helpInit() { strings.Split(config.GetKeyBinding(config.CmdLink0), ",")[0]) helpCells = fmt.Sprintf(helpCells, + config.GetKeyBinding(config.CmdMoveLeft), + config.GetKeyBinding(config.CmdMoveDown), + config.GetKeyBinding(config.CmdMoveUp), + config.GetKeyBinding(config.CmdMoveRight), config.GetKeyBinding(config.CmdPgup), config.GetKeyBinding(config.CmdPgdn), config.GetKeyBinding(config.CmdBack), diff --git a/display/tab.go b/display/tab.go index 402f5f6..cbbe593 100644 --- a/display/tab.go +++ b/display/tab.go @@ -122,13 +122,12 @@ func makeNewTab() *tab { } }) t.view.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { - // Capture left/right scrolling and change the left margin size accordingly, see #197 - // Up/down scrolling is saved in this func to keep them in sync, but the keys - // are passed and no extra behaviour happens. + // Capture scrolling and change the left margin size accordingly, see #197 + // This was also touched by #222 key := event.Key() mod := event.Modifiers() - ru := event.Rune() + cmd := config.TranslateKeyEvent(event) height, width := t.view.GetBufferSize() _, _, boxW, boxH := t.view.GetInnerRect() @@ -140,8 +139,7 @@ func makeNewTab() *tab { boxW-- } - if (key == tcell.KeyRight && mod == tcell.ModNone) || - (key == tcell.KeyRune && mod == tcell.ModNone && ru == 'l') { + if cmd == config.CmdMoveRight || (key == tcell.KeyRight && mod == tcell.ModNone) { // Scrolling to the right if t.page.Column >= leftMargin() { @@ -158,23 +156,20 @@ func makeNewTab() *tab { } } t.page.Column++ - } else if (key == tcell.KeyLeft && mod == tcell.ModNone) || - (key == tcell.KeyRune && mod == tcell.ModNone && ru == 'h') { + } else if cmd == config.CmdMoveLeft || (key == tcell.KeyLeft && mod == tcell.ModNone) { // Scrolling to the left if t.page.Column == 0 { // Can't scroll to the left anymore return nil } t.page.Column-- - } else if (key == tcell.KeyUp && mod == tcell.ModNone) || - (key == tcell.KeyRune && mod == tcell.ModNone && ru == 'k') { + } else if cmd == config.CmdMoveUp || (key == tcell.KeyUp && mod == tcell.ModNone) { // Scrolling up if t.page.Row > 0 { t.page.Row-- } return event - } else if (key == tcell.KeyDown && mod == tcell.ModNone) || - (key == tcell.KeyRune && mod == tcell.ModNone && ru == 'j') { + } else if cmd == config.CmdMoveDown || (key == tcell.KeyDown && mod == tcell.ModNone) { // Scrolling down if t.page.Row < height { t.page.Row++