From 6aa3d2aa18ebc3defbdb45a811cc27fa8ec29de8 Mon Sep 17 00:00:00 2001 From: Joe Lim <50560759+joelim-work@users.noreply.github.com> Date: Thu, 31 Aug 2023 10:10:31 +1000 Subject: [PATCH] Add `-print-last-dir` and `-print-selection` flags (#1402) * Add `-print-last-dir` and `-print-selection` flags * Add shell completions * Update `lfcd.sh` example to use new flag --- client.go | 10 ++++++++++ etc/lf.bash | 2 ++ etc/lf.csh | 2 +- etc/lf.fish | 2 ++ etc/lf.nu | 4 +++- etc/lf.ps1 | 2 ++ etc/lf.zsh | 2 ++ etc/lfcd.sh | 12 +----------- eval.go | 2 +- main.go | 38 ++++++++++++++++++++++++++------------ 10 files changed, 50 insertions(+), 26 deletions(-) diff --git a/client.go b/client.go index 89ef8716..2beff11d 100644 --- a/client.go +++ b/client.go @@ -69,6 +69,16 @@ func run() { if gSelectionPath != "" && len(app.selectionOut) > 0 { writeSelection(gSelectionPath, app.selectionOut) } + + if gPrintLastDir { + fmt.Println(app.nav.currDir().path) + } + + if gPrintSelection && len(app.selectionOut) > 0 { + for _, file := range app.selectionOut { + fmt.Println(file) + } + } } func writeLastDir(filename string, lastDir string) { diff --git a/etc/lf.bash b/etc/lf.bash index 23d3c3a7..cde3a3c8 100644 --- a/etc/lf.bash +++ b/etc/lf.bash @@ -15,6 +15,8 @@ _lf () { -last-dir-path -log -memprofile + -print-last-dir + -print-selection -remote -selection-path -server diff --git a/etc/lf.csh b/etc/lf.csh index 21c08827..d137e6dc 100644 --- a/etc/lf.csh +++ b/etc/lf.csh @@ -9,7 +9,7 @@ # endif # -set LF_ARGS = "-command -config -cpuprofile -doc -last-dir-path -log -memprofile -remote -selection-path -server -single -version -help " +set LF_ARGS = "-command -config -cpuprofile -doc -last-dir-path -log -memprofile -print-last-dir -print-selection -remote -selection-path -server -single -version -help " complete lf "C/-*/(${LF_ARGS})/" complete lfcd "C/-*/(${LF_ARGS})/" diff --git a/etc/lf.fish b/etc/lf.fish index 4087c8f3..6a26770b 100644 --- a/etc/lf.fish +++ b/etc/lf.fish @@ -13,6 +13,8 @@ complete -c lf -o doc -d 'show documentation' complete -c lf -o last-dir-path -r -d 'path to the file to write the last dir on exit (to use for cd)' complete -c lf -o log -r -d 'path to the log file to write messages' complete -c lf -o memprofile -r -d 'path to the file to write the memory profile' +complete -c lf -o print-last-dir -d 'print the last dir to stdout on exit (to use for cd)' +complete -c lf -o print-selection -d 'print the selected files to stdout on open (to use as open file dialog)' complete -c lf -o remote -x -d 'send remote command to server' complete -c lf -o selection-path -r -d 'path to the file to write selected files on open (to use as open file dialog)' complete -c lf -o server -d 'start server (automatic)' diff --git a/etc/lf.nu b/etc/lf.nu index e4b1d0ff..017a3a43 100644 --- a/etc/lf.nu +++ b/etc/lf.nu @@ -1,5 +1,5 @@ # Autocompletion for nushell. -# +# # Documentation: https://www.nushell.sh/book/externs.html # To enable autocompletion you may put this file into a directory: @@ -20,6 +20,8 @@ export extern "lf" [ --last-dir-path: string # path to the file to write the last dir on exit (to use for cd) --log: string # path to the log file to write messages --memprofile: string # path to the file to write the memory profile + --print-last-dir # print the last dir to stdout on exit (to use for cd) + --print-selection # print the selected files to stdout on open (to use as open file dialog) --remote: string # send remote command to server --selection-path: string # path to the file to write selected files on open (to use as open file dialog) --server # start server (automatic) diff --git a/etc/lf.ps1 b/etc/lf.ps1 index ed33cb0a..7bb49c91 100644 --- a/etc/lf.ps1 +++ b/etc/lf.ps1 @@ -16,6 +16,8 @@ Register-ArgumentCompleter -Native -CommandName 'lf' -ScriptBlock { [CompletionResult]::new('-last-dir-path ', '-last-dir-path', [CompletionResultType]::ParameterName, 'path to the file to write the last dir on exit (to use for cd)') [CompletionResult]::new('-log ', '-log', [CompletionResultType]::ParameterName, 'path to the log file to write messages') [CompletionResult]::new('-memprofile ', '-memprofile', [CompletionResultType]::ParameterName, 'path to the file to write the memory profile') + [CompletionResult]::new('-print-last-dir', '-print-last-dir', [CompletionResultType]::ParameterName, 'print the last dir to stdout on exit (to use for cd)') + [CompletionResult]::new('-print-selection', '-print-selection', [CompletionResultType]::ParameterName, 'print the selected files to stdout on open (to use as open file dialog)') [CompletionResult]::new('-remote ', '-remote', [CompletionResultType]::ParameterName, 'send remote command to server') [CompletionResult]::new('-selection-path ', '-selection-path', [CompletionResultType]::ParameterName, 'path to the file to write selected files on open (to use as open file dialog)') [CompletionResult]::new('-server', '-server', [CompletionResultType]::ParameterName, 'start server (automatic)') diff --git a/etc/lf.zsh b/etc/lf.zsh index 54cec9f9..cc2219c8 100644 --- a/etc/lf.zsh +++ b/etc/lf.zsh @@ -20,6 +20,8 @@ arguments=( '-last-dir-path[path to the file to write the last dir on exit (to use for cd)]' '-log[path to the log file to write messages]' '-memprofile[path to the file to write the memory profile]' + '-print-last-dir[print the last dir to stdout on exit (to use for cd)]' + '-print-selection[print the selected files to stdout on open (to use as open file dialog)]' '-remote[send remote command to server]' '-selection-path[path to the file to write selected files on open (to use as open file dialog)]' '-server[start server (automatic)]' diff --git a/etc/lfcd.sh b/etc/lfcd.sh index 323e4060..e35199af 100644 --- a/etc/lfcd.sh +++ b/etc/lfcd.sh @@ -15,16 +15,6 @@ # lfcd () { - tmp="$(mktemp)" # `command` is needed in case `lfcd` is aliased to `lf` - command lf -last-dir-path="$tmp" "$@" - if [ -f "$tmp" ]; then - dir="$(cat "$tmp")" - rm -f "$tmp" - if [ -d "$dir" ]; then - if [ "$dir" != "$(pwd)" ]; then - cd "$dir" - fi - fi - fi + cd "$(command lf -print-last-dir "$@")" } diff --git a/eval.go b/eval.go index e2f27af3..362efb4f 100644 --- a/eval.go +++ b/eval.go @@ -1446,7 +1446,7 @@ func (e *callExpr) eval(app *app, args []string) { return } - if gSelectionPath != "" { + if gSelectionPath != "" || gPrintSelection { app.selectionOut, _ = app.nav.currFileOrSelections() app.quitChan <- struct{}{} return diff --git a/main.go b/main.go index 22667c99..ad88a822 100644 --- a/main.go +++ b/main.go @@ -22,18 +22,20 @@ var ( type arrayFlag []string var ( - gSingleMode bool - gClientID int - gHostname string - gLastDirPath string - gSelectionPath string - gSocketProt string - gSocketPath string - gLogPath string - gSelect string - gConfigPath string - gCommands arrayFlag - gVersion string + gSingleMode bool + gPrintLastDir bool + gPrintSelection bool + gClientID int + gHostname string + gLastDirPath string + gSelectionPath string + gSocketProt string + gSocketPath string + gLogPath string + gSelect string + gConfigPath string + gCommands arrayFlag + gVersion string ) func (a *arrayFlag) Set(v string) error { @@ -228,6 +230,16 @@ func main() { false, "start a client without server") + printLastDir := flag.Bool( + "print-last-dir", + false, + "print the last dir to stdout on exit (to use for cd)") + + printSelection := flag.Bool( + "print-selection", + false, + "print the selected files to stdout on open (to use as open file dialog)") + remoteCmd := flag.String( "remote", "", @@ -305,6 +317,8 @@ func main() { serve() default: gSingleMode = *singleMode + gPrintLastDir = *printLastDir + gPrintSelection = *printSelection if !gSingleMode { checkServer()