Add option for preloading file previews (#2206)
* Initial commit * Add documentation * Fix nav volatile preview state * Use `previewer` for both preloading and previewing * Generate documentation * Update `CHANGELOG.md`
This commit is contained in:
1 parent
56fd6c27ba
commit
d8a23be926
9 files changed
+226
-131
No files matched your search
@@ -24,6 +24,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||
- A new command `cmd-menu-discard` is added to allow exiting the completion menu with completions discarded (#2146).
|
||||
- The `lf_mode` environment variable will now be set to `compmenu` if the completion menu is active (#2146).
|
||||
- A `ruler` config file is added as an alternate method for customizing the ruler (#2186). This is intended to eventually replace the existing `rulerfmt`/`statfmt` options and must be enabled using the new `rulerfile` option. **This feature is currently experimental.**
|
||||
- A new option `preload` is added to enable calling the `previewer` to generate previews in advance (#2206).
|
||||
|
||||
### Fixed
|
||||
|
||||
|
||||
@@ -251,6 +251,7 @@ func (app *app) writeHistory() error {
|
||||
// for evaluation. Similarly directories and regular files are also read in
|
||||
// separate goroutines and sent here for update.
|
||||
func (app *app) loop() {
|
||||
go app.nav.preloadLoop(app.ui)
|
||||
go app.nav.previewLoop(app.ui)
|
||||
|
||||
var serverChan <-chan expr
|
||||
@@ -426,6 +427,10 @@ func (app *app) loop() {
|
||||
}
|
||||
onLoad(app, paths)
|
||||
|
||||
if d.path == app.nav.currDir().path {
|
||||
app.nav.preload()
|
||||
}
|
||||
|
||||
app.ui.draw(app.nav)
|
||||
case r := <-app.nav.regChan:
|
||||
app.nav.regCache[r.path] = r
|
||||
@@ -435,6 +440,9 @@ func (app *app) loop() {
|
||||
if r.path == curr.path {
|
||||
app.ui.regPrev = r
|
||||
app.ui.sxScreen.forceClear = true
|
||||
if gOpts.preload && r.volatile {
|
||||
app.ui.loadFile(app, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -457,6 +465,7 @@ func (app *app) loop() {
|
||||
dir.sel(name, app.nav.height)
|
||||
}
|
||||
|
||||
delete(app.nav.regCache, f.path)
|
||||
app.ui.loadFile(app, false)
|
||||
onLoad(app, []string{f.path})
|
||||
app.ui.draw(app.nav)
|
||||
|
||||
+2
-2
@@ -391,6 +391,8 @@ func completeCmd(acc []rune) (matches []compMatch, result string) {
|
||||
break
|
||||
}
|
||||
switch f[1] {
|
||||
case "cleaner", "previewer":
|
||||
matches, result = matchCmdFile(f[2], false)
|
||||
case "filtermethod", "searchmethod":
|
||||
matches, result = matchWord(f[2], []string{"glob", "regex", "text"})
|
||||
case "info":
|
||||
@@ -403,8 +405,6 @@ func completeCmd(acc []rune) (matches []compMatch, result string) {
|
||||
matches, result = matchWord(f[2], []string{"binary", "decimal"})
|
||||
case "sortby":
|
||||
matches, result = matchWord(f[2], []string{"atime", "btime", "ctime", "custom", "ext", "name", "natural", "size", "time"})
|
||||
case "cleaner", "previewer":
|
||||
matches, result = matchCmdFile(f[2], false)
|
||||
default:
|
||||
if slices.Contains(gOptWords, f[1]+"!") {
|
||||
matches, result = matchWord(f[2], []string{"false", "true"})
|
||||
|
||||
@@ -291,6 +291,7 @@ The following options can be used to customize the behavior of lf:
|
||||
number bool (default false)
|
||||
numberfmt string (default "\033[33m")
|
||||
period int (default 0)
|
||||
preload bool (default false)
|
||||
preserve []string (default "mode")
|
||||
preview bool (default true)
|
||||
previewer string (default '')
|
||||
@@ -1038,6 +1039,10 @@ Note that directories are already updated automatically in many cases.
|
||||
This option can be useful when there is an external process changing the displayed directory and you are not doing anything in lf.
|
||||
Periodic checks are disabled when the value of this option is set to zero.
|
||||
|
||||
## preload (bool) (default false)
|
||||
|
||||
Allow previews to be generated in advance using the `previewer` script as the user navigates through the filesystem.
|
||||
|
||||
## preserve ([]string) (default `mode`)
|
||||
|
||||
List of attributes that are preserved when copying files.
|
||||
@@ -1054,11 +1059,13 @@ Files containing the null character (U+0000) in the read portion are considered
|
||||
|
||||
Set the path of a previewer file to filter the content of regular files for previewing.
|
||||
The file should be executable.
|
||||
The following arguments are passed to the file, (1) current filename, (2) width, (3) height, (4) horizontal position, and (5) vertical position of preview pane respectively.
|
||||
The following arguments are passed to the file, (1) current filename, (2) width, (3) height, (4) horizontal position, (5) vertical position, and (6) mode ("preview" or "preload").
|
||||
SIGPIPE signal is sent when enough lines are read.
|
||||
If the previewer returns a non-zero exit code, then the preview cache for the given file is disabled.
|
||||
This means that if the file is selected in the future, the previewer is called once again.
|
||||
Preview filtering is disabled and files are displayed as they are when the value of this option is left empty.
|
||||
If the `preload` option is enabled, then this will be called with `preload` as the mode when preloading file previews.
|
||||
Refer to the [PREVIEWING FILES section](https://github.com/gokcehan/lf/blob/master/doc.md#previewing-files) for more information about how to configure custom previews.
|
||||
|
||||
## promptfmt (string) (default `\033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f\033[0m`)
|
||||
|
||||
@@ -1904,21 +1911,10 @@ This can be used to highlight source code, list contents of archive files or vie
|
||||
For coloring lf recognizes ANSI escape codes.
|
||||
|
||||
To use this feature, you need to set the value of `previewer` option to the path of an executable file.
|
||||
Five arguments are passed to the file, (1) current filename, (2) width, (3) height, (4) horizontal position, and (5) vertical position of preview pane respectively.
|
||||
The following arguments are passed to the file, (1) current filename, (2) width, (3) height, (4) horizontal position, (5) vertical position, and (6) mode ("preview" or "preload").
|
||||
The output of the execution is printed in the preview pane.
|
||||
You may also want to use the same script in your pager mapping as well:
|
||||
|
||||
set previewer ~/.config/lf/pv.sh
|
||||
map i $~/.config/lf/pv.sh $f | less -R
|
||||
|
||||
For `less` pager, you may instead utilize `LESSOPEN` mechanism so that useful information about the file such as the full path of the file can still be displayed in the statusline below:
|
||||
|
||||
set previewer ~/.config/lf/pv.sh
|
||||
map i $LESSOPEN='| ~/.config/lf/pv.sh %s' less -R $f
|
||||
|
||||
Since this script is called for each file selection change it needs to be as efficient as possible and this responsibility is left to the user.
|
||||
You may use file extensions to determine the type of file more efficiently compared to obtaining MIME types from `file` command.
|
||||
Extensions can then be used to match cleanly within a conditional:
|
||||
Different types of files can be handled by matching by extension (or MIME type from the `file` command):
|
||||
|
||||
#!/bin/sh
|
||||
|
||||
@@ -1931,21 +1927,27 @@ Extensions can then be used to match cleanly within a conditional:
|
||||
*) highlight -O ansi "$1";;
|
||||
esac
|
||||
|
||||
Another important consideration for efficiency is the use of programs with short startup times for preview.
|
||||
For this reason, `highlight` is recommended over `pygmentize` for syntax highlighting.
|
||||
Besides, it is also important that the application processes the file on the fly rather than first reading it to the memory and then doing the processing afterwards.
|
||||
This is especially relevant for big files.
|
||||
lf automatically closes the previewer script output pipe with a SIGPIPE when enough lines are read.
|
||||
When everything else fails, you can make use of the height argument to only feed the first portion of the file to a program for preview.
|
||||
Note that some programs may not respond well to SIGPIPE to exit with a non-zero return code and avoid caching.
|
||||
Because files can be large, lf automatically closes the previewer script output pipe with a SIGPIPE when enough lines are read.
|
||||
Note that some programs may not respond well to SIGPIPE and will exit with a non-zero return code, which avoids caching.
|
||||
You may add a trailing `|| true` command to avoid such errors:
|
||||
|
||||
highlight -O ansi "$1" || true
|
||||
|
||||
You may also use an existing preview filter as you like.
|
||||
Your system may already come with a preview filter named `lesspipe`.
|
||||
These filters may have a mechanism to add user customizations as well.
|
||||
See the related documentation for more information.
|
||||
You may also want to use the same script in your pager mapping as well:
|
||||
|
||||
set previewer ~/.config/lf/pv.sh
|
||||
map i $~/.config/lf/pv.sh $f | less -R
|
||||
|
||||
For `less` pager, you may instead utilize `LESSOPEN` mechanism so that useful information about the file such as the full path of the file can still be displayed in the statusline below:
|
||||
|
||||
set previewer ~/.config/lf/pv.sh
|
||||
map i $LESSOPEN='| ~/.config/lf/pv.sh %s' less -R $f
|
||||
|
||||
Since the preview script is called for each file selection change, it may not generate previews fast enough if the user scrolls through files quickly.
|
||||
To deal with this, the `preload` option can be set to enable file previews to be preloaded in advance.
|
||||
If enabled, the preview script will be run on files in advance as the user navigates through them.
|
||||
In this case, if the exit code of the preview script is zero, then the output will be cached in memory and displayed by lf (useful for text or sixel previews).
|
||||
Otherwise, it will fallback to calling the preview script again when the file is actually selected (useful for previews managed by an external program).
|
||||
|
||||
# CHANGING DIRECTORY
|
||||
|
||||
|
||||
@@ -303,6 +303,7 @@ The following options can be used to customize the behavior of lf:
|
||||
number bool (default false)
|
||||
numberfmt string (default "\033[33m")
|
||||
period int (default 0)
|
||||
preload bool (default false)
|
||||
preserve []string (default "mode")
|
||||
preview bool (default true)
|
||||
previewer string (default '')
|
||||
@@ -1121,6 +1122,11 @@ can be useful when there is an external process changing the displayed
|
||||
directory and you are not doing anything in lf. Periodic checks are
|
||||
disabled when the value of this option is set to zero.
|
||||
|
||||
preload (bool) (default false)
|
||||
|
||||
Allow previews to be generated in advance using the previewer script as
|
||||
the user navigates through the filesystem.
|
||||
|
||||
preserve ([]string) (default mode)
|
||||
|
||||
List of attributes that are preserved when copying files. Currently
|
||||
@@ -1141,13 +1147,16 @@ previewer (string) (default ``) (not filtered if empty)
|
||||
Set the path of a previewer file to filter the content of regular files
|
||||
for previewing. The file should be executable. The following arguments
|
||||
are passed to the file, (1) current filename, (2) width, (3) height, (4)
|
||||
horizontal position, and (5) vertical position of preview pane
|
||||
respectively. SIGPIPE signal is sent when enough lines are read. If the
|
||||
horizontal position, (5) vertical position, and (6) mode ("preview" or
|
||||
"preload"). SIGPIPE signal is sent when enough lines are read. If the
|
||||
previewer returns a non-zero exit code, then the preview cache for the
|
||||
given file is disabled. This means that if the file is selected in the
|
||||
future, the previewer is called once again. Preview filtering is
|
||||
disabled and files are displayed as they are when the value of this
|
||||
option is left empty.
|
||||
option is left empty. If the preload option is enabled, then this will
|
||||
be called with preload as the mode when preloading file previews. Refer
|
||||
to the PREVIEWING FILES section for more information about how to
|
||||
configure custom previews.
|
||||
|
||||
promptfmt (string) (default \033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f\033[0m)
|
||||
|
||||
@@ -2120,27 +2129,13 @@ highlight source code, list contents of archive files or view PDF or
|
||||
image files to name a few. For coloring lf recognizes ANSI escape codes.
|
||||
|
||||
To use this feature, you need to set the value of previewer option to
|
||||
the path of an executable file. Five arguments are passed to the file,
|
||||
(1) current filename, (2) width, (3) height, (4) horizontal position,
|
||||
and (5) vertical position of preview pane respectively. The output of
|
||||
the execution is printed in the preview pane. You may also want to use
|
||||
the same script in your pager mapping as well:
|
||||
the path of an executable file. The following arguments are passed to
|
||||
the file, (1) current filename, (2) width, (3) height, (4) horizontal
|
||||
position, (5) vertical position, and (6) mode ("preview" or "preload").
|
||||
The output of the execution is printed in the preview pane.
|
||||
|
||||
set previewer ~/.config/lf/pv.sh
|
||||
map i $~/.config/lf/pv.sh $f | less -R
|
||||
|
||||
For less pager, you may instead utilize LESSOPEN mechanism so that
|
||||
useful information about the file such as the full path of the file can
|
||||
still be displayed in the statusline below:
|
||||
|
||||
set previewer ~/.config/lf/pv.sh
|
||||
map i $LESSOPEN='| ~/.config/lf/pv.sh %s' less -R $f
|
||||
|
||||
Since this script is called for each file selection change it needs to
|
||||
be as efficient as possible and this responsibility is left to the user.
|
||||
You may use file extensions to determine the type of file more
|
||||
efficiently compared to obtaining MIME types from file command.
|
||||
Extensions can then be used to match cleanly within a conditional:
|
||||
Different types of files can be handled by matching by extension (or
|
||||
MIME type from the file command):
|
||||
|
||||
#!/bin/sh
|
||||
|
||||
@@ -2153,25 +2148,36 @@ Extensions can then be used to match cleanly within a conditional:
|
||||
*) highlight -O ansi "$1";;
|
||||
esac
|
||||
|
||||
Another important consideration for efficiency is the use of programs
|
||||
with short startup times for preview. For this reason, highlight is
|
||||
recommended over pygmentize for syntax highlighting. Besides, it is also
|
||||
important that the application processes the file on the fly rather than
|
||||
first reading it to the memory and then doing the processing afterwards.
|
||||
This is especially relevant for big files. lf automatically closes the
|
||||
previewer script output pipe with a SIGPIPE when enough lines are read.
|
||||
When everything else fails, you can make use of the height argument to
|
||||
only feed the first portion of the file to a program for preview. Note
|
||||
that some programs may not respond well to SIGPIPE to exit with a
|
||||
non-zero return code and avoid caching. You may add a trailing || true
|
||||
Because files can be large, lf automatically closes the previewer script
|
||||
output pipe with a SIGPIPE when enough lines are read. Note that some
|
||||
programs may not respond well to SIGPIPE and will exit with a non-zero
|
||||
return code, which avoids caching. You may add a trailing || true
|
||||
command to avoid such errors:
|
||||
|
||||
highlight -O ansi "$1" || true
|
||||
|
||||
You may also use an existing preview filter as you like. Your system may
|
||||
already come with a preview filter named lesspipe. These filters may
|
||||
have a mechanism to add user customizations as well. See the related
|
||||
documentation for more information.
|
||||
You may also want to use the same script in your pager mapping as well:
|
||||
|
||||
set previewer ~/.config/lf/pv.sh
|
||||
map i $~/.config/lf/pv.sh $f | less -R
|
||||
|
||||
For less pager, you may instead utilize LESSOPEN mechanism so that
|
||||
useful information about the file such as the full path of the file can
|
||||
still be displayed in the statusline below:
|
||||
|
||||
set previewer ~/.config/lf/pv.sh
|
||||
map i $LESSOPEN='| ~/.config/lf/pv.sh %s' less -R $f
|
||||
|
||||
Since the preview script is called for each file selection change, it
|
||||
may not generate previews fast enough if the user scrolls through files
|
||||
quickly. To deal with this, the preload option can be set to enable file
|
||||
previews to be preloaded in advance. If enabled, the preview script will
|
||||
be run on files in advance as the user navigates through them. In this
|
||||
case, if the exit code of the preview script is zero, then the output
|
||||
will be cached in memory and displayed by lf (useful for text or sixel
|
||||
previews). Otherwise, it will fallback to calling the preview script
|
||||
again when the file is actually selected (useful for previews managed by
|
||||
an external program).
|
||||
|
||||
CHANGING DIRECTORY
|
||||
|
||||
|
||||
@@ -136,6 +136,8 @@ func (e *setExpr) eval(app *app, args []string) {
|
||||
}
|
||||
case "number", "nonumber", "number!":
|
||||
err = applyBoolOpt(&gOpts.number, e)
|
||||
case "preload", "nopreload", "preload!":
|
||||
err = applyBoolOpt(&gOpts.preload, e)
|
||||
case "preview", "nopreview", "preview!":
|
||||
preview := gOpts.preview
|
||||
err = applyBoolOpt(&preview, e)
|
||||
@@ -643,6 +645,7 @@ func onRedraw(app *app) {
|
||||
}
|
||||
|
||||
func onSelect(app *app) {
|
||||
app.nav.preload()
|
||||
if cmd, ok := gOpts.cmds["on-select"]; ok {
|
||||
cmd.eval(app, nil)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
.\" Automatically generated by Pandoc 3.7.0.2
|
||||
.\"
|
||||
.TH "LF" "1" "2025\-10\-17" "r39" "DOCUMENTATION"
|
||||
.TH "LF" "1" "2025\-10\-24" "r39" "DOCUMENTATION"
|
||||
.SH NAME
|
||||
lf \- terminal file manager
|
||||
.SH SYNOPSIS
|
||||
@@ -314,6 +314,7 @@ mouse bool (default false)
|
||||
number bool (default false)
|
||||
numberfmt string (default \(dq\(rs033[33m\(dq)
|
||||
period int (default 0)
|
||||
preload bool (default false)
|
||||
preserve []string (default \(dqmode\(dq)
|
||||
preview bool (default true)
|
||||
previewer string (default \(aq\(aq)
|
||||
@@ -988,6 +989,9 @@ This option can be useful when there is an external process changing the
|
||||
displayed directory and you are not doing anything in lf.
|
||||
Periodic checks are disabled when the value of this option is set to
|
||||
zero.
|
||||
.SS preload (bool) (default false)
|
||||
Allow previews to be generated in advance using the \f[CR]previewer\f[R]
|
||||
script as the user navigates through the filesystem.
|
||||
.SS preserve ([]string) (default \f[CR]mode\f[R])
|
||||
List of attributes that are preserved when copying files.
|
||||
Currently supported attributes are \f[CR]mode\f[R] (i.e.
|
||||
@@ -1006,8 +1010,8 @@ Set the path of a previewer file to filter the content of regular files
|
||||
for previewing.
|
||||
The file should be executable.
|
||||
The following arguments are passed to the file, (1) current filename,
|
||||
(2) width, (3) height, (4) horizontal position, and (5) vertical
|
||||
position of preview pane respectively.
|
||||
(2) width, (3) height, (4) horizontal position, (5) vertical position,
|
||||
and (6) mode (\(dqpreview\(dq or \(dqpreload\(dq).
|
||||
SIGPIPE signal is sent when enough lines are read.
|
||||
If the previewer returns a non\-zero exit code, then the preview cache
|
||||
for the given file is disabled.
|
||||
@@ -1015,6 +1019,13 @@ This means that if the file is selected in the future, the previewer is
|
||||
called once again.
|
||||
Preview filtering is disabled and files are displayed as they are when
|
||||
the value of this option is left empty.
|
||||
If the \f[CR]preload\f[R] option is enabled, then this will be called
|
||||
with \f[CR]preload\f[R] as the mode when preloading file previews.
|
||||
Refer to the \c
|
||||
.UR https://github.com/gokcehan/lf/blob/master/doc.md#previewing-files
|
||||
PREVIEWING FILES section
|
||||
.UE \c
|
||||
\ for more information about how to configure custom previews.
|
||||
.SS promptfmt (string) (default \f[CR]\(rs033[32;1m%u\(at%h\(rs033[0m:\(rs033[34;1m%d\(rs033[0m\(rs033[1m%f\(rs033[0m\f[R])
|
||||
Format string of the prompt shown in the top line.
|
||||
.PP
|
||||
@@ -2052,10 +2063,37 @@ For coloring lf recognizes ANSI escape codes.
|
||||
.PP
|
||||
To use this feature, you need to set the value of \f[CR]previewer\f[R]
|
||||
option to the path of an executable file.
|
||||
Five arguments are passed to the file, (1) current filename, (2) width,
|
||||
(3) height, (4) horizontal position, and (5) vertical position of
|
||||
preview pane respectively.
|
||||
The following arguments are passed to the file, (1) current filename,
|
||||
(2) width, (3) height, (4) horizontal position, (5) vertical position,
|
||||
and (6) mode (\(dqpreview\(dq or \(dqpreload\(dq).
|
||||
The output of the execution is printed in the preview pane.
|
||||
.PP
|
||||
Different types of files can be handled by matching by extension (or
|
||||
MIME type from the \f[CR]file\f[R] command):
|
||||
.IP
|
||||
.EX
|
||||
#!/bin/sh
|
||||
|
||||
case \(dq$1\(dq in
|
||||
*.tar*) tar tf \(dq$1\(dq;;
|
||||
*.zip) unzip \-l \(dq$1\(dq;;
|
||||
*.rar) unrar l \(dq$1\(dq;;
|
||||
*.7z) 7z l \(dq$1\(dq;;
|
||||
*.pdf) pdftotext \(dq$1\(dq \-;;
|
||||
*) highlight \-O ansi \(dq$1\(dq;;
|
||||
esac
|
||||
.EE
|
||||
.PP
|
||||
Because files can be large, lf automatically closes the previewer script
|
||||
output pipe with a SIGPIPE when enough lines are read.
|
||||
Note that some programs may not respond well to SIGPIPE and will exit
|
||||
with a non\-zero return code, which avoids caching.
|
||||
You may add a trailing \f[CR]|| true\f[R] command to avoid such errors:
|
||||
.IP
|
||||
.EX
|
||||
highlight \-O ansi \(dq$1\(dq || true
|
||||
.EE
|
||||
.PP
|
||||
You may also want to use the same script in your pager mapping as well:
|
||||
.IP
|
||||
.EX
|
||||
@@ -2072,51 +2110,19 @@ set previewer \(ti/.config/lf/pv.sh
|
||||
map i $LESSOPEN=\(aq| \(ti/.config/lf/pv.sh %s\(aq less \-R $f
|
||||
.EE
|
||||
.PP
|
||||
Since this script is called for each file selection change it needs to
|
||||
be as efficient as possible and this responsibility is left to the user.
|
||||
You may use file extensions to determine the type of file more
|
||||
efficiently compared to obtaining MIME types from \f[CR]file\f[R]
|
||||
command.
|
||||
Extensions can then be used to match cleanly within a conditional:
|
||||
.IP
|
||||
.EX
|
||||
#!/bin/sh
|
||||
|
||||
case \(dq$1\(dq in
|
||||
*.tar*) tar tf \(dq$1\(dq;;
|
||||
*.zip) unzip \-l \(dq$1\(dq;;
|
||||
*.rar) unrar l \(dq$1\(dq;;
|
||||
*.7z) 7z l \(dq$1\(dq;;
|
||||
*.pdf) pdftotext \(dq$1\(dq \-;;
|
||||
*) highlight \-O ansi \(dq$1\(dq;;
|
||||
esac
|
||||
.EE
|
||||
.PP
|
||||
Another important consideration for efficiency is the use of programs
|
||||
with short startup times for preview.
|
||||
For this reason, \f[CR]highlight\f[R] is recommended over
|
||||
\f[CR]pygmentize\f[R] for syntax highlighting.
|
||||
Besides, it is also important that the application processes the file on
|
||||
the fly rather than first reading it to the memory and then doing the
|
||||
processing afterwards.
|
||||
This is especially relevant for big files.
|
||||
lf automatically closes the previewer script output pipe with a SIGPIPE
|
||||
when enough lines are read.
|
||||
When everything else fails, you can make use of the height argument to
|
||||
only feed the first portion of the file to a program for preview.
|
||||
Note that some programs may not respond well to SIGPIPE to exit with a
|
||||
non\-zero return code and avoid caching.
|
||||
You may add a trailing \f[CR]|| true\f[R] command to avoid such errors:
|
||||
.IP
|
||||
.EX
|
||||
highlight \-O ansi \(dq$1\(dq || true
|
||||
.EE
|
||||
.PP
|
||||
You may also use an existing preview filter as you like.
|
||||
Your system may already come with a preview filter named
|
||||
\f[CR]lesspipe\f[R].
|
||||
These filters may have a mechanism to add user customizations as well.
|
||||
See the related documentation for more information.
|
||||
Since the preview script is called for each file selection change, it
|
||||
may not generate previews fast enough if the user scrolls through files
|
||||
quickly.
|
||||
To deal with this, the \f[CR]preload\f[R] option can be set to enable
|
||||
file previews to be preloaded in advance.
|
||||
If enabled, the preview script will be run on files in advance as the
|
||||
user navigates through them.
|
||||
In this case, if the exit code of the preview script is zero, then the
|
||||
output will be cached in memory and displayed by lf (useful for text or
|
||||
sixel previews).
|
||||
Otherwise, it will fallback to calling the preview script again when the
|
||||
file is actually selected (useful for previews managed by an external
|
||||
program).
|
||||
.SH CHANGING DIRECTORY
|
||||
lf changes the working directory of the process to the current directory
|
||||
so that shell commands always work in the displayed directory.
|
||||
|
||||
@@ -455,6 +455,7 @@ type nav struct {
|
||||
moveTotalChan chan int
|
||||
deleteCountChan chan int
|
||||
deleteTotalChan chan int
|
||||
preloadChan chan string
|
||||
previewChan chan string
|
||||
dirChan chan *dir
|
||||
regChan chan *reg
|
||||
@@ -592,6 +593,7 @@ func newNav(ui *ui) *nav {
|
||||
moveTotalChan: make(chan int, 1024),
|
||||
deleteCountChan: make(chan int, 1024),
|
||||
deleteTotalChan: make(chan int, 1024),
|
||||
preloadChan: make(chan string, 1024),
|
||||
previewChan: make(chan string, 1024),
|
||||
dirChan: make(chan *dir),
|
||||
regChan: make(chan *reg),
|
||||
@@ -684,7 +686,14 @@ func (nav *nav) resize(ui *ui) {
|
||||
dir.boundPos(nav.height)
|
||||
}
|
||||
|
||||
clear(nav.regCache)
|
||||
for path, r := range nav.regCache {
|
||||
// keep volatile previews to prevent unnecessary preloads
|
||||
if !r.volatile {
|
||||
delete(nav.regCache, path)
|
||||
}
|
||||
}
|
||||
|
||||
nav.preload()
|
||||
}
|
||||
|
||||
func (nav *nav) position() {
|
||||
@@ -733,6 +742,12 @@ func (nav *nav) exportFiles() {
|
||||
}
|
||||
}
|
||||
|
||||
func (nav *nav) preloadLoop(ui *ui) {
|
||||
for path := range nav.preloadChan {
|
||||
nav.preview(path, ui.wins[len(ui.wins)-1], "preload")
|
||||
}
|
||||
}
|
||||
|
||||
func (nav *nav) previewLoop(ui *ui) {
|
||||
var prev string
|
||||
for path := range nav.previewChan {
|
||||
@@ -760,7 +775,7 @@ func (nav *nav) previewLoop(ui *ui) {
|
||||
nav.volatilePreview = false
|
||||
}
|
||||
if len(path) != 0 {
|
||||
nav.preview(path, win)
|
||||
nav.preview(path, win, "preview")
|
||||
prev = path
|
||||
}
|
||||
}
|
||||
@@ -781,18 +796,63 @@ func matchPattern(pattern, name, path string) bool {
|
||||
return matched
|
||||
}
|
||||
|
||||
func (nav *nav) preview(path string, win *win) {
|
||||
func (nav *nav) preload() {
|
||||
if !gOpts.preview || !gOpts.preload {
|
||||
return
|
||||
}
|
||||
|
||||
dir := nav.currDir()
|
||||
doPreload := func(i int) {
|
||||
if i < 0 || i >= len(dir.files) {
|
||||
return
|
||||
}
|
||||
|
||||
file := dir.files[i]
|
||||
if !(file.Mode().IsRegular() || (file.IsDir() && gOpts.dirpreviews)) {
|
||||
return
|
||||
}
|
||||
|
||||
if _, ok := nav.regCache[file.path]; ok {
|
||||
return
|
||||
}
|
||||
|
||||
nav.regCache[file.path] = ®{loading: true, loadTime: time.Now(), path: file.path}
|
||||
nav.preloadChan <- file.path
|
||||
}
|
||||
|
||||
nav.startPreview()
|
||||
for i := 0; i <= nav.height/2; i++ {
|
||||
doPreload(dir.ind + i)
|
||||
}
|
||||
for i := 1; i <= nav.height/2; i++ {
|
||||
doPreload(dir.ind - i)
|
||||
}
|
||||
}
|
||||
|
||||
func (nav *nav) preview(path string, win *win, mode string) {
|
||||
reg := ®{loadTime: time.Now(), path: path}
|
||||
defer func() { nav.regChan <- reg }()
|
||||
defer func() {
|
||||
if (gOpts.preload && mode == "preview") || (!gOpts.preload && reg.volatile) {
|
||||
nav.volatilePreview = true
|
||||
}
|
||||
|
||||
if gOpts.preload == (mode == "preload") {
|
||||
nav.regChan <- reg
|
||||
}
|
||||
}()
|
||||
|
||||
var reader *bufio.Reader
|
||||
|
||||
if len(gOpts.previewer) != 0 {
|
||||
cmd := exec.Command(gOpts.previewer, path,
|
||||
cmd := exec.Command(
|
||||
gOpts.previewer,
|
||||
path,
|
||||
strconv.Itoa(win.w),
|
||||
strconv.Itoa(win.h),
|
||||
strconv.Itoa(win.x),
|
||||
strconv.Itoa(win.y))
|
||||
strconv.Itoa(win.y),
|
||||
mode,
|
||||
)
|
||||
|
||||
out, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
@@ -808,11 +868,9 @@ func (nav *nav) preview(path string, win *win) {
|
||||
|
||||
defer func() {
|
||||
if err := cmd.Wait(); err != nil {
|
||||
if e, ok := err.(*exec.ExitError); ok {
|
||||
if e.ExitCode() != 0 {
|
||||
reg.volatile = true
|
||||
nav.volatilePreview = true
|
||||
}
|
||||
var exitErr *exec.ExitError
|
||||
if errors.As(err, &exitErr) {
|
||||
reg.volatile = true
|
||||
} else {
|
||||
log.Printf("loading file: %s", err)
|
||||
}
|
||||
@@ -841,16 +899,24 @@ func (nav *nav) preview(path string, win *win) {
|
||||
|
||||
func (nav *nav) loadReg(path string, volatile bool) *reg {
|
||||
r, ok := nav.regCache[path]
|
||||
if !ok || (volatile && r.volatile) {
|
||||
r := ®{loading: true, loadTime: time.Now(), path: path, volatile: true}
|
||||
if !ok {
|
||||
r = ®{loading: true, loadTime: time.Now(), path: path}
|
||||
nav.regCache[path] = r
|
||||
nav.startPreview()
|
||||
nav.previewChan <- path
|
||||
if gOpts.preload {
|
||||
nav.preloadChan <- path
|
||||
} else {
|
||||
nav.previewChan <- path
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
nav.checkReg(r)
|
||||
if volatile && r.volatile {
|
||||
nav.startPreview()
|
||||
nav.previewChan <- path
|
||||
}
|
||||
|
||||
nav.checkReg(r)
|
||||
return r
|
||||
}
|
||||
|
||||
|
||||
@@ -82,6 +82,7 @@ var gOpts struct {
|
||||
number bool
|
||||
numberfmt string
|
||||
period int
|
||||
preload bool
|
||||
preserve []string
|
||||
preview bool
|
||||
previewer string
|
||||
@@ -243,6 +244,7 @@ func init() {
|
||||
gOpts.number = false
|
||||
gOpts.numberfmt = "\033[33m"
|
||||
gOpts.period = 0
|
||||
gOpts.preload = false
|
||||
gOpts.preserve = []string{"mode"}
|
||||
gOpts.preview = true
|
||||
gOpts.previewer = ""
|
||||
|
||||
Reference in new issue
Block a user