From 6e0573e14964d2a12a6a14a15653fc19c169eb20 Mon Sep 17 00:00:00 2001 From: Christian Zangl Date: Wed, 4 May 2022 00:15:15 +0200 Subject: [PATCH 1/2] add hi/mid/lo-screen commands and default keybindings --- doc.go | 3 +++ docstring.go | 3 +++ eval.go | 24 ++++++++++++++++++++++++ lf.1 | 3 +++ nav.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ opts.go | 3 +++ 6 files changed, 83 insertions(+) diff --git a/doc.go b/doc.go index f0e044fe..77a7f642 100644 --- a/doc.go +++ b/doc.go @@ -31,6 +31,9 @@ The following commands are provided by lf: jump-prev (default '[') top (default 'gg' and '') bottom (default 'G' and '') + hi-screen (default 'H') + mid-screen (default 'M') + lo-screen (default 'L') toggle invert (default 'v') unselect (default 'u') diff --git a/docstring.go b/docstring.go index a8829ace..2aa85b05 100644 --- a/docstring.go +++ b/docstring.go @@ -35,6 +35,9 @@ The following commands are provided by lf: jump-prev (default '[') top (default 'gg' and '') bottom (default 'G' and '') + hi-screen (default 'H') + mid-screen (default 'M') + lo-screen (default 'L') toggle invert (default 'v') unselect (default 'u') diff --git a/eval.go b/eval.go index 54f2dcb1..00ddb672 100644 --- a/eval.go +++ b/eval.go @@ -963,6 +963,30 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.loadFile(app.nav, true) app.ui.loadFileInfo(app.nav) } + case "hi-screen": + if !app.nav.init { + return + } + if app.nav.hiScreen() { + app.ui.loadFile(app.nav, true) + app.ui.loadFileInfo(app.nav) + } + case "mid-screen": + if !app.nav.init { + return + } + if app.nav.midScreen() { + app.ui.loadFile(app.nav, true) + app.ui.loadFileInfo(app.nav) + } + case "lo-screen": + if !app.nav.init { + return + } + if app.nav.loScreen() { + app.ui.loadFile(app.nav, true) + app.ui.loadFileInfo(app.nav) + } case "toggle": if !app.nav.init { return diff --git a/lf.1 b/lf.1 index 16ae2d6e..25852e87 100644 --- a/lf.1 +++ b/lf.1 @@ -46,6 +46,9 @@ The following commands are provided by lf: jump-prev (default '[') top (default 'gg' and '') bottom (default 'G' and '') + hi-screen (default 'H') + mid-screen (default 'M') + lo-screen (default 'L') toggle invert (default 'v') unselect (default 'u') diff --git a/nav.go b/nav.go index 52a1e407..bb539d28 100644 --- a/nav.go +++ b/nav.go @@ -943,6 +943,53 @@ func (nav *nav) bottom() bool { return old != dir.ind } +func (nav *nav) hiScreen() bool { + dir := nav.currDir() + + old := dir.ind + beg := max(dir.ind-dir.pos, 0) + offs := gOpts.scrolloff + if beg == 0 { + offs = 0 + } + + dir.ind = beg + offs + dir.pos = offs + + return old != dir.ind +} + +func (nav *nav) midScreen() bool { + dir := nav.currDir() + + old := dir.ind + beg := max(dir.ind-dir.pos, 0) + end := min(beg+nav.height, len(dir.files)) + + half := (end - beg) / 2 + dir.ind = beg + half + dir.pos = half + + return old != dir.ind +} + +func (nav *nav) loScreen() bool { + dir := nav.currDir() + + old := dir.ind + beg := max(dir.ind-dir.pos, 0) + end := min(beg+nav.height, len(dir.files)) + offs := gOpts.scrolloff + if end == len(dir.files) { + offs = 0 + } + + dir.ind = end - 1 - offs + dir.pos = end - beg - 1 - offs + + return old != dir.ind +} + func (nav *nav) toggleSelection(path string) { if _, ok := nav.selections[path]; ok { delete(nav.selections, path) diff --git a/opts.go b/opts.go index dae3983c..261a235f 100644 --- a/opts.go +++ b/opts.go @@ -146,6 +146,9 @@ func init() { gOpts.keys[""] = &callExpr{"top", nil, 1} gOpts.keys["G"] = &callExpr{"bottom", nil, 1} gOpts.keys[""] = &callExpr{"bottom", nil, 1} + gOpts.keys["H"] = &callExpr{"hi-screen", nil, 1} + gOpts.keys["M"] = &callExpr{"mid-screen", nil, 1} + gOpts.keys["L"] = &callExpr{"lo-screen", nil, 1} gOpts.keys["["] = &callExpr{"jump-prev", nil, 1} gOpts.keys["]"] = &callExpr{"jump-next", nil, 1} gOpts.keys[""] = &listExpr{[]expr{&callExpr{"toggle", nil, 1}, &callExpr{"down", nil, 1}}, 1} From b4fe6b7261d78081586318a39ab53ebb193a4e6c Mon Sep 17 00:00:00 2001 From: Christian Zangl Date: Wed, 4 May 2022 08:47:21 +0200 Subject: [PATCH 2/2] doc --- doc.go | 6 ++++++ docstring.go | 6 ++++++ lf.1 | 8 ++++++++ 3 files changed, 20 insertions(+) diff --git a/doc.go b/doc.go index 77a7f642..3d3f7589 100644 --- a/doc.go +++ b/doc.go @@ -298,6 +298,12 @@ Change the current working directory to the next/previous jumplist item. Move the current file selection to the top/bottom of the directory. + hi-screen (default 'H') + mid-screen (default 'M') + lo-screen (default 'L') + +Move the current file selection to the top/middle/bottom of the screen. + toggle Toggle the selection of the current file or files given as arguments. diff --git a/docstring.go b/docstring.go index 2aa85b05..2627f0a2 100644 --- a/docstring.go +++ b/docstring.go @@ -310,6 +310,12 @@ Change the current working directory to the next/previous jumplist item. Move the current file selection to the top/bottom of the directory. + hi-screen (default 'H') + mid-screen (default 'M') + lo-screen (default 'L') + +Move the current file selection to the top/middle/bottom of the screen. + toggle Toggle the selection of the current file or files given as arguments. diff --git a/lf.1 b/lf.1 index 25852e87..487b8fda 100644 --- a/lf.1 +++ b/lf.1 @@ -346,6 +346,14 @@ Change the current working directory to the next/previous jumplist item. .PP Move the current file selection to the top/bottom of the directory. .PP +.EX + hi-screen (default 'H') + mid-screen (default 'M') + lo-screen (default 'L') +.EE +.PP +Move the current file selection to the top/middle/bottom of the screen. +.PP .EX toggle .EE