Skip to content

Commit

Permalink
quitcd: fix old bug and feat. for modular export
Browse files Browse the repository at this point in the history
- 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
BeyondMagic committed Feb 8, 2024
1 parent f2a8648 commit 9ed2df6
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions misc/quitcd/quitcd.nu
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
}
}

0 comments on commit 9ed2df6

Please sign in to comment.