Skip to content

Commit

Permalink
Add -print-last-dir and -print-selection flags (gokcehan#1402)
Browse files Browse the repository at this point in the history
* Add `-print-last-dir` and `-print-selection` flags

* Add shell completions

* Update `lfcd.sh` example to use new flag
  • Loading branch information
joelim-work authored Aug 31, 2023
1 parent 63d21b5 commit 6aa3d2a
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 26 deletions.
10 changes: 10 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions etc/lf.bash
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ _lf () {
-last-dir-path
-log
-memprofile
-print-last-dir
-print-selection
-remote
-selection-path
-server
Expand Down
2 changes: 1 addition & 1 deletion etc/lf.csh
Original file line number Diff line number Diff line change
Expand Up @@ -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})/"
2 changes: 2 additions & 0 deletions etc/lf.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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)'
Expand Down
4 changes: 3 additions & 1 deletion etc/lf.nu
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions etc/lf.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)')
Expand Down
2 changes: 2 additions & 0 deletions etc/lf.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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)]'
Expand Down
12 changes: 1 addition & 11 deletions etc/lfcd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$@")"
}
2 changes: 1 addition & 1 deletion eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 26 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
"",
Expand Down Expand Up @@ -305,6 +317,8 @@ func main() {
serve()
default:
gSingleMode = *singleMode
gPrintLastDir = *printLastDir
gPrintSelection = *printSelection

if !gSingleMode {
checkServer()
Expand Down

0 comments on commit 6aa3d2a

Please sign in to comment.