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:
Joe Lim authored and GitHub committed 2025-10-24 13:26:34 +11:00
1 parent 56fd6c27ba
commit d8a23be926
9 files changed
+226 -131

No files matched your search

+57 -51
View File
@@ -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.