-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcd_xul.sh
executable file
·88 lines (72 loc) · 1.94 KB
/
cd_xul.sh
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
#!/bin/bash
function save_dir_history() {
if test ! -e ~/system_config/.cache/.dir_history
then
touch ~/system_config/.cache/.dir_history
fi
local dup_str=`escape_regex_char "$1"`
sed -i "/^${dup_str}$/d" ~/system_config/.cache/.dir_history
echo "$1" >> ~/system_config/.cache/.dir_history
}
function cd_select_output_line() {
echo -e "=> \c"
local select_number
read select_number
select_output_line "$select_number"
local select_output_line_ret="$?"
if [[ $select_output_line_ret -ge 0 && $select_output_line_ret -lt ${#output_line_array[@]} ]]; then
builtin cd ${output_line_array[$select_output_line_ret]}
save_dir_history `pwd`
elif [[ $select_output_line_ret -eq ${#output_line_array[@]} ]]; then
choose_to_display_or_not
if [[ $? -eq 0 ]]; then
display_output_line_array
else
return 1
fi
cd_select_output_line
else
echo 'cd_xul: please check your input'
fi
}
function main() {
output_line_array=()
if [ $# -eq 0 ]; then
builtin cd ~
return 0
elif [ $# -eq 1 ]; then
if [[ "$1" == '-' ]]; then
builtin cd -
return 0
elif test -e $1; then
builtin cd $1
save_dir_history `pwd`
return 0
fi
fi
if test ! -e ~/system_config/.cache/.dir_history; then
builtin cd $@
return $?
fi
local i=0
local line
for line in `tac ~/system_config/.cache/.dir_history`; do
match_line "$@" "$line"
if [ $? -eq 0 ]; then
output_line_array[$i]="$line"
let i++
fi
done
if test ${#output_line_array[@]} -eq 0; then
builtin cd $@
return $?
fi
choose_to_display_or_not
if [[ $? -eq 0 ]]; then
display_output_line_array
else
return 1
fi
cd_select_output_line
}
main "$@"