From b4637f91aa5f1fa7df4472fa6253592eb1dda34e Mon Sep 17 00:00:00 2001 From: sbinnee <58146755+sbinnee@users.noreply.github.com> Date: Sun, 27 Feb 2022 10:37:27 +0100 Subject: [PATCH] add scrollup scrolldown builtin cmd (#764) * add scrollup scrolldown builtin cmd Scrollup/down move window pane without moving current cursor position. Default keybinding would be for scrollup and for scrolldown just like in vim. * limit scrolldown when reached bottom scrolodown will not allow empty space Fix typo --- complete.go | 2 ++ doc.go | 4 ++++ docstring.go | 4 ++++ eval.go | 10 +++++++++ lf.1 | 4 ++++ nav.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ opts.go | 2 ++ 7 files changed, 84 insertions(+) diff --git a/complete.go b/complete.go index 5bd6dab..8feecfb 100644 --- a/complete.go +++ b/complete.go @@ -17,9 +17,11 @@ var ( "up", "half-up", "page-up", + "scrollup", "down", "half-down", "page-down", + "scrolldown", "updir", "open", "quit", diff --git a/doc.go b/doc.go index d60e5a0..133e36d 100644 --- a/doc.go +++ b/doc.go @@ -19,9 +19,11 @@ The following commands are provided by lf: up (default 'k' and '') half-up (default '') page-up (default '' and '') + scrollup (default '') down (default 'j' and '') half-down (default '') page-down (default '' and '') + scrolldown (default '') updir (default 'h' and '') open (default 'l' and '') top (default 'gg' and '') @@ -242,9 +244,11 @@ Quit lf and return to the shell. up (default 'k' and '') half-up (default '') page-up (default '' and '') + scrollup (default '') down (default 'j' and '') half-down (default '') page-down (default '' and '') + scrolldown (default '') Move the current file selection upwards/downwards by one/half a page/full page. diff --git a/docstring.go b/docstring.go index 90c1bb2..2fe4fa7 100644 --- a/docstring.go +++ b/docstring.go @@ -23,9 +23,11 @@ The following commands are provided by lf: up (default 'k' and '') half-up (default '') page-up (default '' and '') + scrollup (default '') down (default 'j' and '') half-down (default '') page-down (default '' and '') + scrolldown (default '') updir (default 'h' and '') open (default 'l' and '') top (default 'gg' and '') @@ -249,9 +251,11 @@ Quit lf and return to the shell. up (default 'k' and '') half-up (default '') page-up (default '' and '') + scrollup (default '') down (default 'j' and '') half-down (default '') page-down (default '' and '') + scrolldown (default '') Move the current file selection upwards/downwards by one/half a page/full page. diff --git a/eval.go b/eval.go index ba828cb..bb9fe62 100644 --- a/eval.go +++ b/eval.go @@ -807,6 +807,11 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.loadFile(app.nav, true) app.ui.loadFileInfo(app.nav) } + case "scrollup": + if app.nav.scrollup(e.count) { + app.ui.loadFile(app.nav, true) + app.ui.loadFileInfo(app.nav) + } case "down": if app.nav.down(e.count) { app.ui.loadFile(app.nav, true) @@ -822,6 +827,11 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.loadFile(app.nav, true) app.ui.loadFileInfo(app.nav) } + case "scrolldown": + if app.nav.scrolldown(e.count) { + app.ui.loadFile(app.nav, true) + app.ui.loadFileInfo(app.nav) + } case "updir": resetIncCmd(app) preChdir(app) diff --git a/lf.1 b/lf.1 index c6780d0..c22b473 100644 --- a/lf.1 +++ b/lf.1 @@ -35,9 +35,11 @@ The following commands are provided by lf: up (default 'k' and '') half-up (default '') page-up (default '' and '') + scrollup (default '') down (default 'j' and '') half-down (default '') page-down (default '' and '') + scrolldown (default '') updir (default 'h' and '') open (default 'l' and '') top (default 'gg' and '') @@ -279,9 +281,11 @@ Quit lf and return to the shell. up (default 'k' and '') half-up (default '') page-up (default '' and '') + scrollup (default '') down (default 'j' and '') half-down (default '') page-down (default '' and '') + scrolldown (default '') .EE .PP Move the current file selection upwards/downwards by one/half a page/full page. diff --git a/nav.go b/nav.go index 886cabc..12c7568 100644 --- a/nav.go +++ b/nav.go @@ -805,6 +805,64 @@ func (nav *nav) down(dist int) bool { return old != dir.ind } +func (nav *nav) scrollup(dist int) bool { + dir := nav.currDir() + + // when reached top do nothing + if istop := dir.ind == dir.pos; istop { + return false + } + + old := dir.ind + + minedge := min(nav.height/2, gOpts.scrolloff) + + dir.pos += dist + + // jump to ensure minedge when edge < minedge + edge := nav.height - dir.pos + delta := min(0, edge-minedge-1) + dir.pos = min(dir.pos, nav.height-minedge-1) + // update dir.ind accordingly + dir.ind = dir.ind + delta + + dir.ind = min(dir.ind, dir.ind-(dir.pos-nav.height+1)) + + // prevent cursor disappearing downwards + dir.pos = min(dir.pos, nav.height-1) + + return old != dir.ind +} + +func (nav *nav) scrolldown(dist int) bool { + dir := nav.currDir() + maxind := len(dir.files) - 1 + + // reached bottom + if dir.ind-dir.pos > maxind-nav.height { + return false + } + + old := dir.ind + + minedge := min(nav.height/2, gOpts.scrolloff) + + dir.pos -= dist + + // jump to ensure minedge when edge < minedge + delta := min(0, dir.pos-minedge) + dir.pos = max(dir.pos, minedge) + // update dir.ind accordingly + dir.ind = dir.ind - delta + dir.ind = max(dir.ind, dir.ind-(dir.pos-minedge)) + + dir.ind = min(maxind, dir.ind) + // prevent disappearing + dir.pos = max(dir.pos, 0) + + return old != dir.ind +} + func (nav *nav) updir() error { if len(nav.dirs) <= 1 { return nil diff --git a/opts.go b/opts.go index 4db50b1..3834ca7 100644 --- a/opts.go +++ b/opts.go @@ -128,11 +128,13 @@ func init() { gOpts.keys[""] = &callExpr{"half-up", nil, 1} gOpts.keys[""] = &callExpr{"page-up", nil, 1} gOpts.keys[""] = &callExpr{"page-up", nil, 1} + gOpts.keys[""] = &callExpr{"scrollup", nil, 1} gOpts.keys["j"] = &callExpr{"down", nil, 1} gOpts.keys[""] = &callExpr{"down", nil, 1} gOpts.keys[""] = &callExpr{"half-down", nil, 1} gOpts.keys[""] = &callExpr{"page-down", nil, 1} gOpts.keys[""] = &callExpr{"page-down", nil, 1} + gOpts.keys[""] = &callExpr{"scrolldown", nil, 1} gOpts.keys["h"] = &callExpr{"updir", nil, 1} gOpts.keys[""] = &callExpr{"updir", nil, 1} gOpts.keys["l"] = &callExpr{"open", nil, 1}