From cf996263bb80b7da8fffbea81eba5fa2c0d02378 Mon Sep 17 00:00:00 2001 From: Joe Lim <50560759+joelim-work@users.noreply.github.com> Date: Mon, 12 Feb 2024 10:27:57 +1100 Subject: [PATCH] Export mode for use in scripting keybindings (#1594) * Export mode * Add pipe mode * Add documentation --- app.go | 1 + doc.md | 7 +++++++ doc.txt | 8 ++++++++ lf.1 | 12 +++++++++++- ui.go | 37 +++++++++++++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 1 deletion(-) diff --git a/app.go b/app.go index 8f204bae..5b7e344c 100644 --- a/app.go +++ b/app.go @@ -500,6 +500,7 @@ func (app *app) runCmdSync(cmd *exec.Cmd, pause_after bool) { func (app *app) runShell(s string, args []string, prefix string) { app.nav.exportFiles() app.ui.exportSizes() + app.ui.exportMode() exportOpts() gState.mutex.Lock() diff --git a/doc.md b/doc.md index 90dfddcc..92dee461 100644 --- a/doc.md +++ b/doc.md @@ -224,6 +224,7 @@ The following environment variables are exported for shell commands: lf_width lf_height lf_count + lf_mode The following special shell commands are used to customize the behavior of lf when defined: @@ -1090,6 +1091,12 @@ Width/Height of the terminal. Value of the count associated with the current command. +## lf_mode + +Current mode that `lf` is operating in. +This is useful for customizing keybindings depending on what the current mode is. +Possible values are `delete`, `rename`, `filter`, `find`, `mark`, `search`, `command`, `shell`, `pipe` (when running a shell-pipe command), `normal`, and `unknown`. + # SPECIAL COMMANDS This section shows information about special shell commands. diff --git a/doc.txt b/doc.txt index 67ad7288..f932ec11 100644 --- a/doc.txt +++ b/doc.txt @@ -214,6 +214,7 @@ The following environment variables are exported for shell commands: lf_width lf_height lf_count + lf_mode The following special shell commands are used to customize the behavior of lf when defined: @@ -1201,6 +1202,13 @@ lf_count Value of the count associated with the current command. +lf_mode + +Current mode that lf is operating in. This is useful for customizing +keybindings depending on what the current mode is. Possible values are +delete, rename, filter, find, mark, search, command, shell, pipe (when +running a shell-pipe command), normal, and unknown. + SPECIAL COMMANDS This section shows information about special shell commands. diff --git a/lf.1 b/lf.1 index 70267752..73ee3df3 100644 --- a/lf.1 +++ b/lf.1 @@ -1,6 +1,6 @@ .\" Automatically generated by Pandoc 2.11.4 .\" -.TH "LF" "1" "2024-01-18" "" "DOCUMENTATION" +.TH "LF" "1" "2024-02-02" "" "DOCUMENTATION" .hy .SH NAME .PP @@ -236,6 +236,7 @@ lf_user_{option} lf_width lf_height lf_count +lf_mode \f[R] .fi .PP @@ -1216,6 +1217,15 @@ Width/Height of the terminal. .SS lf_count .PP Value of the count associated with the current command. +.SS lf_mode +.PP +Current mode that \f[C]lf\f[R] is operating in. +This is useful for customizing keybindings depending on what the current +mode is. +Possible values are \f[C]delete\f[R], \f[C]rename\f[R], +\f[C]filter\f[R], \f[C]find\f[R], \f[C]mark\f[R], \f[C]search\f[R], +\f[C]command\f[R], \f[C]shell\f[R], \f[C]pipe\f[R] (when running a +shell-pipe command), \f[C]normal\f[R], and \f[C]unknown\f[R]. .SH SPECIAL COMMANDS .PP This section shows information about special shell commands. diff --git a/ui.go b/ui.go index 17e53cbc..10c0f90e 100644 --- a/ui.go +++ b/ui.go @@ -1512,6 +1512,43 @@ func (ui *ui) resume() error { return err } +func (ui *ui) exportMode() { + getMode := func() string { + if strings.HasPrefix(ui.cmdPrefix, "delete") { + return "delete" + } + + if strings.HasPrefix(ui.cmdPrefix, "replace") || strings.HasPrefix(ui.cmdPrefix, "create") { + return "rename" + } + + switch ui.cmdPrefix { + case "filter: ": + return "filter" + case "find: ", "find-back: ": + return "find" + case "mark-save: ", "mark-load: ", "mark-remove: ": + return "mark" + case "rename: ": + return "rename" + case "/", "?": + return "search" + case ":": + return "command" + case "$", "%", "!", "&": + return "shell" + case ">": + return "pipe" + case "": + return "normal" + default: + return "unknown" + } + } + + os.Setenv("lf_mode", getMode()) +} + func (ui *ui) exportSizes() { w, h := ui.screen.Size() os.Setenv("lf_width", strconv.Itoa(w))