From b02fd42df4fd075d0ce8ea5abdd157827468920c Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Sat, 25 Feb 2023 14:00:54 -0800 Subject: [PATCH] `invert-below` command to substitute for visual mode (#1101) This command inverts the selection for the current file and all the files below it. Using this command twice substitutes for having a "visual" mode. This is described in more detail in the docs and in https://github.com/gokcehan/lf/issues/477#issuecomment-1414563834. Fixes #477. While that's not exactly what was asked for, I hope it's a suitable replacement and seems more in the spirit of `lf`, at least until a better replacement is implemented. --- complete.go | 1 + doc.go | 12 ++++++++++++ docstring.go | 15 +++++++++++++++ eval.go | 5 +++++ lf.1 | 11 +++++++++++ nav.go | 12 ++++++++++-- 6 files changed, 54 insertions(+), 2 deletions(-) diff --git a/complete.go b/complete.go index 294ad2b4..c98aac16 100644 --- a/complete.go +++ b/complete.go @@ -35,6 +35,7 @@ var ( "low", "toggle", "invert", + "invert-below", "unselect", "glob-select", "glob-unselect", diff --git a/doc.go b/doc.go index 164bcf48..27f090af 100644 --- a/doc.go +++ b/doc.go @@ -36,6 +36,7 @@ The following commands are provided by lf: low (default 'L') toggle invert (default 'v') + invert-below unselect (default 'u') glob-select glob-unselect @@ -337,6 +338,17 @@ Reverse the selection of all files in the current directory (i.e. 'toggle' all f Selections in other directories are not effected by this command. You can define a new command to select all files in the directory by combining 'invert' with 'unselect' (i.e. 'cmd select-all :unselect; invert'), though this will also remove selections in other directories. + invert-below + +Reverse the selection (i.e. 'toggle') of all files at or after the current file in the current directory. + +To select a contiguous block of files, use this command on the first file you want to select. +Then, move down to the first file you do *not* want to select (the one after the end of the desired selection) and use this command again. +This achieves an effect similar to the visual mode in vim. + +This command is experimental and may be removed once a better replacement for the visual mode is implemented in 'lf'. +If you'd like to experiment with using this command, you should bind it to a key (e.g. 'V') for a better experience. + unselect (default 'u') Remove the selection of all files in all directories. diff --git a/docstring.go b/docstring.go index 31d419a0..9c0186e7 100644 --- a/docstring.go +++ b/docstring.go @@ -39,6 +39,7 @@ The following commands are provided by lf: low (default 'L') toggle invert (default 'v') + invert-below unselect (default 'u') glob-select glob-unselect @@ -348,6 +349,20 @@ You can define a new command to select all files in the directory by combining 'invert' with 'unselect' (i.e. 'cmd select-all :unselect; invert'), though this will also remove selections in other directories. + invert-below + +Reverse the selection (i.e. 'toggle') of all files at or after the current file +in the current directory. + +To select a contiguous block of files, use this command on the first file you +want to select. Then, move down to the first file you do *not* want to select +(the one after the end of the desired selection) and use this command again. +This achieves an effect similar to the visual mode in vim. + +This command is experimental and may be removed once a better replacement for +the visual mode is implemented in 'lf'. If you'd like to experiment with using +this command, you should bind it to a key (e.g. 'V') for a better experience. + unselect (default 'u') Remove the selection of all files in all directories. diff --git a/eval.go b/eval.go index e5121c0c..f8713ba1 100644 --- a/eval.go +++ b/eval.go @@ -1154,6 +1154,11 @@ func (e *callExpr) eval(app *app, args []string) { return } app.nav.invert() + case "invert-below": + if !app.nav.init { + return + } + app.nav.invertBelow() case "unselect": app.nav.unselect() case "calcdirsize": diff --git a/lf.1 b/lf.1 index da529e95..0a38ff6c 100644 --- a/lf.1 +++ b/lf.1 @@ -51,6 +51,7 @@ The following commands are provided by lf: low (default 'L') toggle invert (default 'v') + invert-below unselect (default 'u') glob-select glob-unselect @@ -395,6 +396,16 @@ Toggle the selection of the current file or files given as arguments. .PP Reverse the selection of all files in the current directory (i.e. 'toggle' all files). Selections in other directories are not effected by this command. You can define a new command to select all files in the directory by combining 'invert' with 'unselect' (i.e. 'cmd select-all :unselect; invert'), though this will also remove selections in other directories. .PP +.EX + invert-below +.EE +.PP +Reverse the selection (i.e. 'toggle') of all files at or after the current file in the current directory. +.PP +To select a contiguous block of files, use this command on the first file you want to select. Then, move down to the first file you do *not* want to select (the one after the end of the desired selection) and use this command again. This achieves an effect similar to the visual mode in vim. +.PP +This command is experimental and may be removed once a better replacement for the visual mode is implemented in 'lf'. If you'd like to experiment with using this command, you should bind it to a key (e.g. 'V') for a better experience. +.PP .EX unselect (default 'u') .EE diff --git a/nav.go b/nav.go index b941ff6e..a08238d7 100644 --- a/nav.go +++ b/nav.go @@ -1149,14 +1149,22 @@ func (nav *nav) tag(tag string) error { return nil } -func (nav *nav) invert() { +func (nav *nav) invertAfter(ix int) { dir := nav.currDir() - for _, f := range dir.files { + for _, f := range dir.files[ix:] { path := filepath.Join(dir.path, f.Name()) nav.toggleSelection(path) } } +func (nav *nav) invert() { + nav.invertAfter(0) +} + +func (nav *nav) invertBelow() { + nav.invertAfter(nav.currDir().ind) +} + func (nav *nav) unselect() { nav.selections = make(map[string]int) nav.selectionInd = 0