From 77f113639fbfccdf15b2fe7576c1e173fb50a0f8 Mon Sep 17 00:00:00 2001 From: Christian Zangl Date: Wed, 20 Sep 2023 23:48:28 +0200 Subject: [PATCH] Enable focus events through configuration by using focus (option) and on-ui-enter/on-ui-exit events. --- app.go | 18 +++++++++++++++--- doc.go | 10 ++++++++++ docstring.go | 14 ++++++++++++++ eval.go | 19 +++++++++++++++++++ lf.1 | 14 ++++++++++++++ ui.go | 27 +++++++++++++++++++++------ 6 files changed, 93 insertions(+), 9 deletions(-) diff --git a/app.go b/app.go index 41316339..d81c76d9 100644 --- a/app.go +++ b/app.go @@ -300,6 +300,8 @@ func (app *app) loop() { }() } + onUiEnter(app) + for { select { case <-app.quitChan: @@ -322,6 +324,7 @@ func (app *app) loop() { cmd.eval(app, nil) } + onUiExit(app) app.quit() app.nav.previewChan <- "" @@ -461,12 +464,14 @@ func (app *app) loop() { app.ui.draw(app.nav) } } + } func (app *app) runCmdSync(cmd *exec.Cmd, pause_after bool) { app.nav.previewChan <- "" app.nav.dirPreviewChan <- nil + onUiExit(app) if err := app.ui.suspend(); err != nil { log.Printf("suspend: %s", err) } @@ -475,6 +480,7 @@ func (app *app) runCmdSync(cmd *exec.Cmd, pause_after bool) { app.quit() os.Exit(3) } + onUiEnter(app) }() if err := cmd.Run(); err != nil { @@ -495,6 +501,7 @@ func (app *app) runCmdSync(cmd *exec.Cmd, pause_after bool) { // % No No Yes Yes Yes Statline for input/output // ! Yes No Yes Yes Yes Pause and then resume // & No Yes No No No Do nothing +// ^ No No Yes Yes Yes (internal for events) func (app *app) runShell(s string, args []string, prefix string) { app.nav.exportFiles() app.ui.exportSizes() @@ -513,12 +520,17 @@ func (app *app) runShell(s string, args []string, prefix string) { var out io.Reader var err error switch prefix { - case "$", "!": + case "$", "!", "^": cmd.Stdin = os.Stdin cmd.Stdout = os.Stderr cmd.Stderr = os.Stderr - - app.runCmdSync(cmd, prefix == "!") + if prefix != "^" { + app.runCmdSync(cmd, prefix == "!") + } else { + if err := cmd.Run(); err != nil { + app.ui.echoerrf("running ^shell: %s", err) + } + } return } diff --git a/doc.go b/doc.go index da7fe6ed..31fc5275 100644 --- a/doc.go +++ b/doc.go @@ -208,6 +208,8 @@ The following special shell commands are used to customize the behavior of lf wh pre-cd on-cd on-select + on-ui-enter + on-ui-exit on-quit The following commands/keybindings are provided by default: @@ -1110,6 +1112,14 @@ This shell command can be defined to be executed after changing a directory. This shell command can be defined to be executed after the selection changes. + on-ui-enter + +This shell command can be defined to be executed when the UI of lf becomes active. This will be triggered after lf starts and when foreground shell commands finish. + + on-ui-exit + +This shell command can be defined to be executed when the UI of lf becomes inactive. This will be triggered before lf exits and before starting foreground shell commands. + on-quit This shell command can be defined to be executed before quit. diff --git a/docstring.go b/docstring.go index a16a31c1..e9fd0aba 100644 --- a/docstring.go +++ b/docstring.go @@ -212,6 +212,8 @@ when defined: pre-cd on-cd on-select + on-ui-enter + on-ui-exit on-quit The following commands/keybindings are provided by default: @@ -1202,6 +1204,18 @@ This shell command can be defined to be executed after changing a directory. This shell command can be defined to be executed after the selection changes. + on-ui-enter + +This shell command can be defined to be executed when the UI of lf becomes +active. This will be triggered after lf starts and when foreground shell +commands finish. + + on-ui-exit + +This shell command can be defined to be executed when the UI of lf becomes +inactive. This will be triggered before lf exits and before starting foreground +shell commands. + on-quit This shell command can be defined to be executed before quit. diff --git a/eval.go b/eval.go index ff04308e..89af1f05 100644 --- a/eval.go +++ b/eval.go @@ -1157,6 +1157,22 @@ func onSelect(app *app) { } } +func onUiEnter(app *app) { + if cmd, ok := gOpts.cmds["on-ui-enter"]; ok { + ecmd := cmd.(*execExpr) + ecmd.prefix = "^" + ecmd.eval(app, nil) + } +} + +func onUiExit(app *app) { + if cmd, ok := gOpts.cmds["on-ui-exit"]; ok { + ecmd := cmd.(*execExpr) + ecmd.prefix = "^" + ecmd.eval(app, nil) + } +} + func splitKeys(s string) (keys []string) { for i := 0; i < len(s); { r, w := utf8.DecodeRuneInString(s[i:]) @@ -2764,6 +2780,9 @@ func (e *execExpr) eval(app *app, args []string) { case "&": log.Printf("shell-async: %s -- %s", e, args) app.runShell(e.value, args, e.prefix) + case "^": + log.Printf("shell-sync-evt: %s -- %s", e, args) + app.runShell(e.value, args, e.prefix) default: log.Printf("evaluating unknown execution prefix: %q", e.prefix) } diff --git a/lf.1 b/lf.1 index f7682fa0..aa89c199 100644 --- a/lf.1 +++ b/lf.1 @@ -231,6 +231,8 @@ The following special shell commands are used to customize the behavior of lf wh pre-cd on-cd on-select + on-ui-enter + on-ui-exit on-quit .EE .PP @@ -1332,6 +1334,18 @@ This shell command can be defined to be executed after changing a directory. .PP This shell command can be defined to be executed after the selection changes. .PP +.EX + on-ui-enter +.EE +.PP +This shell command can be defined to be executed when the UI of lf becomes active. This will be triggered after lf starts and when foreground shell commands finish. +.PP +.EX + on-ui-exit +.EE +.PP +This shell command can be defined to be executed when the UI of lf becomes inactive. This will be triggered before lf exits and before starting foreground shell commands. +.PP .EX on-quit .EE diff --git a/ui.go b/ui.go index 9a181201..4fb45970 100644 --- a/ui.go +++ b/ui.go @@ -1449,17 +1449,32 @@ func (ui *ui) readNormalEvent(ev tcell.Event, nav *nav) expr { return nil } -func readCmdEvent(ev tcell.Event) expr { +func (ui *ui) readCmdEvent(ev tcell.Event) expr { switch tev := ev.(type) { case *tcell.EventKey: if tev.Key() == tcell.KeyRune { if tev.Modifiers() == tcell.ModMask(tcell.ModAlt) { - val := string([]rune{'<', 'a', '-', tev.Rune(), '>'}) - if expr, ok := gOpts.cmdkeys[val]; ok { - return expr + // support multi-key bindings but only for ESC[ at the moment + if tev.Rune() == '[' { + ui.keyAcc = append(ui.keyAcc, '<', 'a', '-', tev.Rune(), '>') + } else { + val := string([]rune{'<', 'a', '-', tev.Rune(), '>'}) + if expr, ok := gOpts.cmdkeys[val]; ok { + return expr + } } } else { - return &callExpr{"cmd-insert", []string{string(tev.Rune())}, 1} + // multi-key bindings + if ui.keyAcc != nil { + ui.keyAcc = append(ui.keyAcc, tev.Rune()) + val := string(ui.keyAcc) + ui.keyAcc = nil + if expr, ok := gOpts.cmdkeys[val]; ok { + return expr + } + } else { + return &callExpr{"cmd-insert", []string{string(tev.Rune())}, 1} + } } } else { val := gKeyVal[tev.Key()] @@ -1478,7 +1493,7 @@ func (ui *ui) readEvent(ev tcell.Event, nav *nav) expr { } if _, ok := ev.(*tcell.EventKey); ok && ui.cmdPrefix != "" { - return readCmdEvent(ev) + return ui.readCmdEvent(ev) } return ui.readNormalEvent(ev, nav)