Add on-load command (#2010)

This commit is contained in:
CatsDeservePets authored and GitHub committed 2025-06-20 17:36:57 +10:00
1 parent b76d2546e1
commit ff0c7f1c96
6 files changed
+47 -3

No files matched your search

+3
View File
@@ -427,6 +427,7 @@ func (app *app) loop() {
app.watchDir(d) app.watchDir(d)
onLoad(app, d.fileNames())
app.ui.draw(app.nav) app.ui.draw(app.nav)
case r := <-app.nav.regChan: case r := <-app.nav.regChan:
app.nav.regCache[r.path] = r app.nav.regCache[r.path] = r
@@ -464,6 +465,8 @@ func (app *app) loop() {
if app.ui.msgIsStat { if app.ui.msgIsStat {
app.ui.loadFileInfo(app.nav) app.ui.loadFileInfo(app.nav)
} }
onLoad(app, []string{f.path})
app.ui.draw(app.nav) app.ui.draw(app.nav)
case path := <-app.nav.delChan: case path := <-app.nav.delChan:
deletePathRecursive(app.nav.selections, path) deletePathRecursive(app.nav.selections, path)
+7
View File
@@ -238,6 +238,7 @@ The following special shell commands are used to customize the behavior of lf wh
delete delete
pre-cd pre-cd
on-cd on-cd
on-load
on-focus-gained on-focus-gained
on-focus-lost on-focus-lost
on-init on-init
@@ -1143,6 +1144,11 @@ This shell command can be defined to be executed before changing a directory.
This shell command can be defined to be executed after changing a directory. This shell command can be defined to be executed after changing a directory.
## on-load
This shell command can be defined to be executed after loading a directory.
It provides the files inside the directory as arguments.
## on-focus-gained ## on-focus-gained
This shell command can be defined to be executed when the terminal gains focus. This shell command can be defined to be executed when the terminal gains focus.
@@ -1704,6 +1710,7 @@ You can add an extra call to make it run on startup as well:
Note that all shell commands are possible but `%` and `&` are usually more appropriate as `$` and `!` causes flickers and pauses respectively. Note that all shell commands are possible but `%` and `&` are usually more appropriate as `$` and `!` causes flickers and pauses respectively.
There is also a `pre-cd` command, that works like `on-cd`, but is run before the directory is actually changed. There is also a `pre-cd` command, that works like `on-cd`, but is run before the directory is actually changed.
Another related command is `on-load` which gets executed when loading a directory.
# COLORS # COLORS
+8 -1
View File
@@ -229,6 +229,7 @@ of lf when defined:
delete delete
pre-cd pre-cd
on-cd on-cd
on-load
on-focus-gained on-focus-gained
on-focus-lost on-focus-lost
on-init on-init
@@ -1261,6 +1262,11 @@ on-cd
This shell command can be defined to be executed after changing a This shell command can be defined to be executed after changing a
directory. directory.
on-load
This shell command can be defined to be executed after loading a
directory. It provides the files inside the directory as arguments.
on-focus-gained on-focus-gained
This shell command can be defined to be executed when the terminal gains This shell command can be defined to be executed when the terminal gains
@@ -1920,7 +1926,8 @@ Note that all shell commands are possible but % and & are usually more
appropriate as $ and ! causes flickers and pauses respectively. appropriate as $ and ! causes flickers and pauses respectively.
There is also a pre-cd command, that works like on-cd, but is run before There is also a pre-cd command, that works like on-cd, but is run before
the directory is actually changed. the directory is actually changed. Another related command is on-load
which gets executed when loading a directory.
COLORS COLORS
+11
View File
@@ -593,6 +593,17 @@ func onChdir(app *app) {
} }
} }
func onLoad(app *app, files []string) {
// prevent infinite loops
if !gOpts.dircache {
return
}
if cmd, ok := gOpts.cmds["on-load"]; ok {
cmd.eval(app, files)
}
}
func onFocusGained(app *app) { func onFocusGained(app *app) {
if cmd, ok := gOpts.cmds["on-focus-gained"]; ok { if cmd, ok := gOpts.cmds["on-focus-gained"]; ok {
cmd.eval(app, nil) cmd.eval(app, nil)
+9 -1
View File
@@ -1,6 +1,6 @@
.\" Automatically generated by Pandoc 2.11.4 .\" Automatically generated by Pandoc 2.11.4
.\" .\"
.TH "LF" "1" "2025-05-12" "" "DOCUMENTATION" .TH "LF" "1" "2025-06-19" "r35-23-gef2fb79" "DOCUMENTATION"
.hy .hy
.SH NAME .SH NAME
.PP .PP
@@ -255,6 +255,7 @@ rename
delete delete
pre-cd pre-cd
on-cd on-cd
on-load
on-focus-gained on-focus-gained
on-focus-lost on-focus-lost
on-init on-init
@@ -1266,6 +1267,11 @@ directory.
.PP .PP
This shell command can be defined to be executed after changing a This shell command can be defined to be executed after changing a
directory. directory.
.SS on-load
.PP
This shell command can be defined to be executed after loading a
directory.
It provides the files inside the directory as arguments.
.SS on-focus-gained .SS on-focus-gained
.PP .PP
This shell command can be defined to be executed when the terminal gains This shell command can be defined to be executed when the terminal gains
@@ -2194,6 +2200,8 @@ causes flickers and pauses respectively.
.PP .PP
There is also a \f[C]pre-cd\f[R] command, that works like There is also a \f[C]pre-cd\f[R] command, that works like
\f[C]on-cd\f[R], but is run before the directory is actually changed. \f[C]on-cd\f[R], but is run before the directory is actually changed.
Another related command is \f[C]on-load\f[R] which gets executed when
loading a directory.
.SH COLORS .SH COLORS
.PP .PP
lf tries to automatically adapt its colors to the environment. lf tries to automatically adapt its colors to the environment.
+9 -1
View File
@@ -43,7 +43,6 @@ type file struct {
func newFile(path string) *file { func newFile(path string) *file {
lstat, err := os.Lstat(path) lstat, err := os.Lstat(path)
if err != nil { if err != nil {
log.Printf("getting file information: %s", err) log.Printf("getting file information: %s", err)
return &file{ return &file{
@@ -388,6 +387,15 @@ func (dir *dir) name() string {
return dir.files[dir.ind].Name() return dir.files[dir.ind].Name()
} }
func (d *dir) fileNames() []string {
names := []string{}
for _, file := range d.files {
names = append(names, file.path)
}
return names
}
func (dir *dir) sel(name string, height int) { func (dir *dir) sel(name string, height int) {
if len(dir.files) == 0 { if len(dir.files) == 0 {
dir.ind, dir.pos = 0, 0 dir.ind, dir.pos = 0, 0