-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathj.sh
48 lines (47 loc) · 1.53 KB
/
j.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
#!/bin/bash
j() {
local datafile=$HOME/.j
if [ "$1" = "--add" ]; then
shift
# $HOME isn't worth matching
[ "$*" = "$HOME" ] && return
awk -v q="$*" -v t="$(date +%s)" -F"|" '
BEGIN { l[q] = 1; d[q] = t }
$2 >= 1 {
if( $1 == q ) {
l[$1] = $2 + 1
d[$1] = t
} else {
l[$1] = $2
d[$1] = $3
}
count += $2
}
END {
if( count > 1000 ) {
for( i in l ) print i "|" 0.9*l[i] "|" d[i] # aging
} else for( i in l ) print i "|" l[i] "|" d[i]
}
' $datafile 2>/dev/null > $datafile.tmp
mv -f $datafile.tmp $datafile
elif [ "$1" = "--complete" ]; then
# tab completion
awk -v q="$2" -F"|" '
BEGIN { split(substr(q,3),a," ") }
{
if( system("test -d \"" $1 "\"") ) next
for( i in a ) $1 !~ a[i] && $1 = ""; if( $1 ) print $1
}
' $datafile 2>/dev/null
else
# list/go (must set $JPY)
[ -f "$JPY" ] || return
cd="$($JPY -f $datafile $*)"
[ -z "$cd" -o "$cd" = "$PWD" ] && return
cd "$cd"
fi
}
# tab completion (only works in bash for now)
[ "$SHELL" = "/bin/bash" ] && complete -C 'j --complete "$COMP_LINE"' j
# populate directory list. avoid clobbering other PROMPT_COMMANDs.
PROMPT_COMMAND='j --add "$(pwd -P)";'"$PROMPT_COMMAND"