1
0
mirror of https://github.com/makew0rld/amfora.git synced 2024-06-23 19:45:24 +00:00

Add option for custom keybinds for navigation (#222)

Co-authored-by: makeworld <25111343+makeworld-the-better-one@users.noreply.github.com>
This commit is contained in:
Himanshu 2021-04-22 07:29:05 +05:30 committed by GitHub
parent 29996d2051
commit 9337f63ed2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 13 deletions

View File

@ -210,6 +210,10 @@ func Init() error {
viper.SetDefault("keybindings.bind_sub", "Ctrl-A") viper.SetDefault("keybindings.bind_sub", "Ctrl-A")
viper.SetDefault("keybindings.bind_add_sub", "Ctrl-X") viper.SetDefault("keybindings.bind_add_sub", "Ctrl-X")
viper.SetDefault("keybindings.bind_save", "Ctrl-S") 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_pgup", []string{"PgUp", "u"})
viper.SetDefault("keybindings.bind_pgdn", []string{"PgDn", "d"}) viper.SetDefault("keybindings.bind_pgdn", []string{"PgDn", "d"})
viper.SetDefault("keybindings.bind_bottom", "Space") viper.SetDefault("keybindings.bind_bottom", "Space")

View File

@ -148,6 +148,10 @@ scrollbar = "auto"
# bind_reload # bind_reload
# bind_back # bind_back
# bind_forward # bind_forward
# bind_moveup
# bind_movedown
# bind_moveleft
# bind_moveright
# bind_pgup # bind_pgup
# bind_pgdn # bind_pgdn
# bind_new_tab # bind_new_tab

View File

@ -3,6 +3,7 @@ package config
import ( import (
"strings" "strings"
"code.rocketnine.space/tslocum/cview"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -42,6 +43,10 @@ const (
CmdReload CmdReload
CmdBack CmdBack
CmdForward CmdForward
CmdMoveUp
CmdMoveDown
CmdMoveLeft
CmdMoveRight
CmdPgup CmdPgup
CmdPgdn CmdPgdn
CmdNewTab CmdNewTab
@ -168,6 +173,10 @@ func KeyInit() {
CmdReload: "keybindings.bind_reload", CmdReload: "keybindings.bind_reload",
CmdBack: "keybindings.bind_back", CmdBack: "keybindings.bind_back",
CmdForward: "keybindings.bind_forward", CmdForward: "keybindings.bind_forward",
CmdMoveUp: "keybindings.bind_moveup",
CmdMoveDown: "keybindings.bind_movedown",
CmdMoveLeft: "keybindings.bind_moveleft",
CmdMoveRight: "keybindings.bind_moveright",
CmdPgup: "keybindings.bind_pgup", CmdPgup: "keybindings.bind_pgup",
CmdPgdn: "keybindings.bind_pgdn", CmdPgdn: "keybindings.bind_pgdn",
CmdNewTab: "keybindings.bind_new_tab", CmdNewTab: "keybindings.bind_new_tab",
@ -203,6 +212,11 @@ func KeyInit() {
tcellKeys[kname] = k 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 c, allb := range configBindings {
for _, b := range viper.GetStringSlice(allb) { for _, b := range viper.GetStringSlice(allb) {
parseBinding(c, b) parseBinding(c, b)

View File

@ -145,6 +145,10 @@ scrollbar = "auto"
# bind_reload # bind_reload
# bind_back # bind_back
# bind_forward # bind_forward
# bind_moveup
# bind_movedown
# bind_moveleft
# bind_moveright
# bind_pgup # bind_pgup
# bind_pgdn # bind_pgdn
# bind_new_tab # bind_new_tab

View File

@ -13,7 +13,7 @@ import (
var helpCells = strings.TrimSpace( var helpCells = strings.TrimSpace(
"?\tBring up this help. You can scroll!\n" + "?\tBring up this help. You can scroll!\n" +
"Esc\tLeave the help\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 up a page in document\n" +
"%s\tGo down a page in document\n" + "%s\tGo down a page in document\n" +
"g\tGo to top of document\n" + "g\tGo to top of document\n" +
@ -80,6 +80,10 @@ func helpInit() {
strings.Split(config.GetKeyBinding(config.CmdLink0), ",")[0]) strings.Split(config.GetKeyBinding(config.CmdLink0), ",")[0])
helpCells = fmt.Sprintf(helpCells, 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.CmdPgup),
config.GetKeyBinding(config.CmdPgdn), config.GetKeyBinding(config.CmdPgdn),
config.GetKeyBinding(config.CmdBack), config.GetKeyBinding(config.CmdBack),

View File

@ -122,13 +122,12 @@ func makeNewTab() *tab {
} }
}) })
t.view.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { t.view.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
// Capture left/right scrolling and change the left margin size accordingly, see #197 // Capture 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 // This was also touched by #222
// are passed and no extra behaviour happens.
key := event.Key() key := event.Key()
mod := event.Modifiers() mod := event.Modifiers()
ru := event.Rune() cmd := config.TranslateKeyEvent(event)
height, width := t.view.GetBufferSize() height, width := t.view.GetBufferSize()
_, _, boxW, boxH := t.view.GetInnerRect() _, _, boxW, boxH := t.view.GetInnerRect()
@ -140,8 +139,7 @@ func makeNewTab() *tab {
boxW-- boxW--
} }
if (key == tcell.KeyRight && mod == tcell.ModNone) || if cmd == config.CmdMoveRight || (key == tcell.KeyRight && mod == tcell.ModNone) {
(key == tcell.KeyRune && mod == tcell.ModNone && ru == 'l') {
// Scrolling to the right // Scrolling to the right
if t.page.Column >= leftMargin() { if t.page.Column >= leftMargin() {
@ -158,23 +156,20 @@ func makeNewTab() *tab {
} }
} }
t.page.Column++ t.page.Column++
} else if (key == tcell.KeyLeft && mod == tcell.ModNone) || } else if cmd == config.CmdMoveLeft || (key == tcell.KeyLeft && mod == tcell.ModNone) {
(key == tcell.KeyRune && mod == tcell.ModNone && ru == 'h') {
// Scrolling to the left // Scrolling to the left
if t.page.Column == 0 { if t.page.Column == 0 {
// Can't scroll to the left anymore // Can't scroll to the left anymore
return nil return nil
} }
t.page.Column-- t.page.Column--
} else if (key == tcell.KeyUp && mod == tcell.ModNone) || } else if cmd == config.CmdMoveUp || (key == tcell.KeyUp && mod == tcell.ModNone) {
(key == tcell.KeyRune && mod == tcell.ModNone && ru == 'k') {
// Scrolling up // Scrolling up
if t.page.Row > 0 { if t.page.Row > 0 {
t.page.Row-- t.page.Row--
} }
return event return event
} else if (key == tcell.KeyDown && mod == tcell.ModNone) || } else if cmd == config.CmdMoveDown || (key == tcell.KeyDown && mod == tcell.ModNone) {
(key == tcell.KeyRune && mod == tcell.ModNone && ru == 'j') {
// Scrolling down // Scrolling down
if t.page.Row < height { if t.page.Row < height {
t.page.Row++ t.page.Row++