-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterceptor.bash
More file actions
90 lines (73 loc) · 2.54 KB
/
interceptor.bash
File metadata and controls
90 lines (73 loc) · 2.54 KB
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
###############################################################################
############# The following snippet is for bash #############
declare -A directory_stacks
declare -A access_time
# Push the home as the initial directory
directory_stacks=( [~]=0 )
access_time=( [~]=$(date +%s) )
function push_if_success() {
# The $1 represents the execute status of `cd` or `pushd`
# We should use absolute path for later use. @example: there is a diretory
# called `example` in `/home/nick`, given we are in `/home/nick` and use `cd`
# to change into `example`, then the argument passed in is `exmaple`, we cannot
# use it, we need get the absulote one, that is where comes in the 'cwd'.
local cwd=$PWD
if [ $1 = 0 ]; then
if [ "${directory_stacks[$cwd]}" = "" ]; then
directory_stacks+=( [$cwd]=0 )
else
let directory_stacks[$cwd]++
fi
access_time[$cwd]=$(date +%s)
else
# Fix error message. @exmaple: cd 0
# 'cd_interceptor:cd:1: no such file or directory: 0' to 'cd:1: no such file or directory: 0'
sed 's/^.*_interceptor:\([a-z]\+\):[0-9]\+:\(.*\)$/\1:\2/' <<<$(cat ~/.dirx/stderr) >/dev/stderr
fi
}
function generate_directories() {
# Data form: dirname#frequency@accesstime
for key in "${!directory_stacks[@]}"; do
echo "$key#${directory_stacks[$key]}@${access_time[$key]}";
done
}
function cd_interceptor() {
cd $1 2>~/.dirx/stderr
# If success then added it to directory_stacks, for $1 may
# be a invalid value.
push_if_success "$?"
}
alias cd="cd_interceptor"
function dirx {
if [ $# = 0 ]; then
# Hide cursor
printf "\033[?25l"
{INSTALL_PATH}/index.js $(generate_directories) 2>~/.dirx/stderr
dir=$(cat ~/.dirx/stderr)
# For some reason, we cann't add listener for `INT` signal, so we check the value of `dir`;
# If it equals to "", then "CTRL + C" was pressed, we should still in current directory.
if [ ! "$dir" = "" ]; then
cd $dir
fi
# Show cursor
printf "\033[?25h"
else
if [ "$1" = "-c" ]; then
directory_stacks=()
access_time=()
directory_stacks=( [$PWD]=0 )
access_time=( [$PWD]=$(date +%s) )
fi
fi
}
alias dirx="dirx"
alias d="dirx"
function pushd_interceptor() {
pushd $1 2>~/.dirx/stderr
# If success then added it to directory_stacks, for $1 may
# be a invalid value.
push_if_success "$?"
}
alias pushd="pushd_interceptor"
trap 'printf "\033[?25h"' INT
###############################################################################