-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnav.nu
65 lines (59 loc) · 1.46 KB
/
nav.nu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Directory Navigation
def get-subdirs [$p: string] {
return (ls $p | where type == dir | get name | sort --ignore-case --natural | path basename)
}
def select-subpath [$basepath?: string] {
let $base = ($basepath | default $env.PWD)
mut $p = $base
mut $open = true
while $open {
let subdir = (get-subdirs $p) | prepend ['.','..']
| input list --fuzzy $'Navigating Directory (ansi magenta)($p)(ansi reset) - was ($env.PWD) - choose . to cd into, .. for parent'
if $subdir == null {
$p = null
break
} else if $subdir == '.' {
$open = false
} else if $subdir == '..' {
$p = ($p | path dirname)
} else {
$p = ($p | path join $subdir)
}
}
return $p
}
def --env cc [$basepath?: string] {
let $p = select-subpath $basepath
if $p != null {
echo $'Changing to ($p)…'
cd $p
return $p
}
}
def select-parent [$basepath?: string] {
let $base = ($basepath | default $env.PWD)
mut $paths = []
mut $i = $base
while true {
$paths = ($paths | append $i)
let $iBase = ($i | path dirname)
if $i == $iBase {
break
}
$i = $iBase
}
($paths | input list --fuzzy $'Navigate to parent directory…')
}
def --env xx [$basepath?: string] {
let $p = select-parent $basepath
if $p != null {
echo $'Changing to ($p)…'
cd $p
}
}
def nav [$action?: string] {
match $action {
up => xx
_ => cc
}
}