From ff0c7f1c96c7c719577ab4bba55be30d6cb19ee0 Mon Sep 17 00:00:00 2001 From: CatsDeservePets <145048791+CatsDeservePets@users.noreply.github.com> Date: Fri, 20 Jun 2025 09:36:57 +0200 Subject: [PATCH] Add `on-load` command (#2010) --- app.go | 3 +++ doc.md | 7 +++++++ doc.txt | 9 ++++++++- eval.go | 11 +++++++++++ lf.1 | 10 +++++++++- nav.go | 10 +++++++++- 6 files changed, 47 insertions(+), 3 deletions(-) diff --git a/app.go b/app.go index fb326650..9fe525dd 100644 --- a/app.go +++ b/app.go @@ -427,6 +427,7 @@ func (app *app) loop() { app.watchDir(d) + onLoad(app, d.fileNames()) app.ui.draw(app.nav) case r := <-app.nav.regChan: app.nav.regCache[r.path] = r @@ -464,6 +465,8 @@ func (app *app) loop() { if app.ui.msgIsStat { app.ui.loadFileInfo(app.nav) } + + onLoad(app, []string{f.path}) app.ui.draw(app.nav) case path := <-app.nav.delChan: deletePathRecursive(app.nav.selections, path) diff --git a/doc.md b/doc.md index 4fb46999..3bd1b1db 100644 --- a/doc.md +++ b/doc.md @@ -238,6 +238,7 @@ The following special shell commands are used to customize the behavior of lf wh delete pre-cd on-cd + on-load on-focus-gained on-focus-lost 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. +## 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 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. 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 diff --git a/doc.txt b/doc.txt index 9fa8af02..424b3ca8 100644 --- a/doc.txt +++ b/doc.txt @@ -229,6 +229,7 @@ of lf when defined: delete pre-cd on-cd + on-load on-focus-gained on-focus-lost on-init @@ -1261,6 +1262,11 @@ on-cd 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 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. 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 diff --git a/eval.go b/eval.go index 80dd7c1f..16b9db03 100644 --- a/eval.go +++ b/eval.go @@ -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) { if cmd, ok := gOpts.cmds["on-focus-gained"]; ok { cmd.eval(app, nil) diff --git a/lf.1 b/lf.1 index fa8e1341..80059712 100644 --- a/lf.1 +++ b/lf.1 @@ -1,6 +1,6 @@ .\" 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 .SH NAME .PP @@ -255,6 +255,7 @@ rename delete pre-cd on-cd +on-load on-focus-gained on-focus-lost on-init @@ -1266,6 +1267,11 @@ directory. .PP This shell command can be defined to be executed after changing a 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 .PP This shell command can be defined to be executed when the terminal gains @@ -2194,6 +2200,8 @@ causes flickers and pauses respectively. .PP 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. +Another related command is \f[C]on-load\f[R] which gets executed when +loading a directory. .SH COLORS .PP lf tries to automatically adapt its colors to the environment. diff --git a/nav.go b/nav.go index f2a30a2e..4615c67f 100644 --- a/nav.go +++ b/nav.go @@ -43,7 +43,6 @@ type file struct { func newFile(path string) *file { lstat, err := os.Lstat(path) - if err != nil { log.Printf("getting file information: %s", err) return &file{ @@ -388,6 +387,15 @@ func (dir *dir) name() string { 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) { if len(dir.files) == 0 { dir.ind, dir.pos = 0, 0