-
-
Notifications
You must be signed in to change notification settings - Fork 757
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
quitcd: fix old bug and feat. for modular export
- now users can import as a module instead of just copying and paste - also refactor everything with internal commands - fix post-processing of nnn - make nnn env variable local
- Loading branch information
1 parent
f2a8648
commit 9ed2df6
Showing
1 changed file
with
25 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,27 @@ | ||
# The behaviour is set to cd on quit (nnn checks if NNN_TMPFILE is set) | ||
let cfgHome = ($env | default $"($env.HOME)/.config" XDG_CONFIG_HOME | get XDG_CONFIG_HOME) | ||
$env.NNN_TMPFILE = $"($cfgHome)/nnn/.lastd" | ||
# Run nnn with dynamic changing directory to the environment. | ||
export def --env n [ | ||
...flags : string # Extra flags to launch nnn with. | ||
] -> nothing { | ||
|
||
def --env n [...x] { | ||
# Launch nnn. Add desired flags after `^nnn`, ex: `^nnn -eda ($x | str join)` | ||
^nnn ($x | str join) | ||
let newpath = ( | ||
if ($env.NNN_TMPFILE | path exists) { | ||
# FIXME: fails if path contains single-quote | ||
let newpath = (open $env.NNN_TMPFILE | parse "cd '{nnnpath}'").0.nnnpath | ||
^rm -f $env.NNN_TMPFILE | ||
echo $newpath | ||
} else { | ||
pwd | ||
} | ||
) | ||
cd $newpath | ||
let config_home = if ($env.XDG_CONFIG_HOME | is-empty) { | ||
$'($env.HOME)/.config/' | ||
} else { | ||
$env.XDG_CONFIG_HOME | ||
} | ||
|
||
# The behaviour is set to cd on quit (nnn checks if NNN_TMPFILE is set). | ||
let nnn_tmpfile = $config_home + '/nnn/.lastd' | ||
|
||
# Launch nnn. Add desired flags after `^nnn`, ex: `^nnn -eda ($x | str join)` | ||
NNN_TMPFILE=$nnn_tmpfile ^nnn ...$flags | ||
|
||
if ($nnn_tmpfile | path exists) { | ||
# Remove <cd '> from the first part of the string and the last single quote <'>. | ||
# Fix post-processing of nnn with path, escaping single quotes with POSIX syntax. | ||
let path = (open $nnn_tmpfile | str substring 4..-1 | str replace --all `'\''` `'`) | ||
|
||
rm $nnn_tmpfile | ||
|
||
cd $path | ||
} | ||
} |