-
Notifications
You must be signed in to change notification settings - Fork 10
/
up.fish
96 lines (76 loc) · 1.56 KB
/
up.fish
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env fish
#
# up.fish: Quickly traverse the current working path.
# [ Author ] endowdly
# [ Email ] endowdly@gmail.com
# [ Date Created ] 23 May, 2017
# [ Last Modified ] 26 May, 2017
#
# fish fork from up.sh by Shannon Moeller <me@shannonmoeller.com>
#
# source to use: source /path/to/up/up.fish
function _updir
if test $argv[1] = '/' -o -z $argv[1] -o -z $argv[2]
return
end
set --local p (dirname "$argv[1]")
set --local a (basename "$p")
set --local b (basename "$argv[2]")
if test -z $a -o -z $b
return
end
if string match -iq "$b*" $a
echo $p
return
end
_updir $p $argv[2]
# Ooo, look at this clever recursion!
end
function _upnum
if test -z $argv[1] -o -z $argv[2]
return
end
set --local p $argv[1]
set --local i $argv[2]
while test $i -ne 0
set p (dirname $p)
set i (math $i - 1)
end
echo $p
end
function up
# up one
if not count $argv > /dev/null
cd ..
return
end
# go back
if test $argv[1] = '-' -o -d $argv[1]
cd $argv[1]
return
end
# check for num
set --local regex '^[0-9]+$'
if string match -rq $regex $argv[1]
set d (_upnum $PWD $argv[1])
else
set d (_updir $PWD $argv[1])
end
if test -d $d
cd $d
return
end
echo -e "usage: up [dir|num|-]\npwd: $PWD"
end
function _up
set --local p (string trim --chars=/ (dirname $PWD) | string split /)
for d in $p
echo $d
end
end
complete -c up -fa "(_up)"
funcsave up
funcsave _up
funcsave _updir
funcsave _upnum
# __END___