mirror of
https://github.com/jarun/nnn.git
synced 2025-01-15 13:26:37 +00:00
quitcd: fix bugs and feat. for modular export and selective quit
This commit is contained in:
parent
5b05c8b9b1
commit
2a442ec30c
|
@ -1,19 +1,37 @@
|
||||||
# The behaviour is set to cd on quit (nnn checks if NNN_TMPFILE is set)
|
# Run nnn with dynamic changing directory to the environment.
|
||||||
let cfgHome = ($env | default $"($env.HOME)/.config" XDG_CONFIG_HOME | get XDG_CONFIG_HOME)
|
#
|
||||||
$env.NNN_TMPFILE = $"($cfgHome)/nnn/.lastd"
|
# $env.XDG_CONFIG_HOME sets the home folder for `nnn` folder and its $env.NNN_TMPFILE variable.
|
||||||
|
# See manual NNN(1) for more information.
|
||||||
|
#
|
||||||
|
# Import module using `use quitcd.nu n` to have `n` command in your context.
|
||||||
|
export def --env n [
|
||||||
|
...args : string # Extra flags to launch nnn with.
|
||||||
|
--selective = false # Change directory only when exiting via ^G.
|
||||||
|
] -> nothing {
|
||||||
|
|
||||||
def --env n [...x] {
|
# The behaviour is set to cd on quit (nnn checks if $env.NNN_TMPFILE is set).
|
||||||
# Launch nnn. Add desired flags after `^nnn`, ex: `^nnn -eda ($x | str join)`
|
# Hard-coded to its respective behaviour in `nnn` source-code.
|
||||||
^nnn ($x | str join)
|
let nnn_tmpfile = $env
|
||||||
let newpath = (
|
| default '~/.config/' 'XDG_CONFIG_HOME'
|
||||||
if ($env.NNN_TMPFILE | path exists) {
|
| get 'XDG_CONFIG_HOME'
|
||||||
# FIXME: fails if path contains single-quote
|
| path join 'nnn/.lastd'
|
||||||
let newpath = (open $env.NNN_TMPFILE | parse "cd '{nnnpath}'").0.nnnpath
|
| path expand
|
||||||
^rm -f $env.NNN_TMPFILE
|
|
||||||
echo $newpath
|
# Launch nnn. Add desired flags after `^nnn`, ex: `^nnn -eda ...$args`,
|
||||||
|
# or make an alias `alias n = n -eda`.
|
||||||
|
if $selective {
|
||||||
|
^nnn ...$args
|
||||||
} else {
|
} else {
|
||||||
pwd
|
NNN_TMPFILE=$nnn_tmpfile ^nnn ...$args
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($nnn_tmpfile | path exists) {
|
||||||
|
# Remove <cd '> from the first part of the string and the last single quote <'>.
|
||||||
|
# Fix post-processing of nnn's given path that escapes its single quotes with POSIX syntax.
|
||||||
|
let path = open $nnn_tmpfile | str substring 4..-1 | str replace --all `'\''` `'`
|
||||||
|
|
||||||
|
^rm $nnn_tmpfile
|
||||||
|
|
||||||
|
cd $path
|
||||||
}
|
}
|
||||||
)
|
|
||||||
cd $newpath
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue