forked from psycofdj/xtdbash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.cdstack
83 lines (79 loc) · 1.99 KB
/
lib.cdstack
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
#! /bin/bash
function cdstack_enable ()
{
cdevent_push_pre __cdstack_cd
}
function __cdstack_cd ()
{
# shellcheck disable=SC2034
declare pushd_silent=t
case "$1" in
- ) builtin pushd +1 >/dev/null
return 1
;;
+ ) builtin pushd -0 >/dev/null
return 1
;;
-s ) __cdstack_show_stack
return 1
;;
-c* ) __cdstack_clear_stack ${1#-c}
__cdstack_show_stack
return 1
;;
-* ) declare i=${1#-}
if [ -z "${i##[0-9]*}" ]
then
builtin pushd +$i >/dev/null
fi
return 1
;;
esac
declare i=1 dirs="$(dirs -l)"
builtin pushd ${1:-~} >/dev/null 2>/dev/null || {
return 0
}
for d in $dirs
do if [ "$d" = "$PWD" ]
then builtin popd +$i >/dev/null; break; fi
let i=i+1
done
return 1
}
function __cdstack_clear_stack ()
{
case "$1" in
..* ) declare j=${1#..}
if [ -n "${j##[0-9]*}" ]; then return 1; fi
let "j=j+1"
while let "j>0"
do builtin popd +0 >/dev/null 2>/dev/null || return; let "j=j-1"; done
;;
*.. ) declare i=${1%..}
if [ -n "${i##[0-9]*}" ]; then return 1; fi
if [ $i -eq 0 ]; then let "i=1"; fi
while builtin popd +$i >/dev/null 2>/dev/null; do : nothing; done
;;
*..* ) declare i=${1%..*} j=${1#*..}
if [ -n "${i##[0-9]*}" ] || [ -n "${j##[0-9]*}" ]
then return; fi
let "j=j-i+1"
while let "j>0"
do builtin popd +$i >/dev/null 2>/dev/null || return; let "j=j-1"; done
;;
'' ) while builtin popd +1 >/dev/null 2>/dev/null; do : nothing; done
;;
* ) declare i=$1
if [ -n "${i##[0-9]*}" ]; then return 1; fi
builtin popd +$i >/dev/null 2>/dev/null || return
;;
esac
return 0
}
function __cdstack_show_stack ()
{
declare -i i=0
for d in $(dirs -l)
do echo -e "[$i]\t$d"; let "i=i+1"; done
return 0
}