-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathc
80 lines (76 loc) · 2.45 KB
/
c
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
# Fuzzy (fzf) cd based on:
# - visited locations (bookmarks) OR
# - indexed files (locate/mdfind)
#
# Usage: c [fuzzy pattern]
# c -s (show statistics)
local db=$XDG_DATA_HOME/marks/marks.sqlite
# Show statistics
if [[ $1 == -s ]]
then
sqlite3 $db 'SELECT * FROM marks ORDER BY weight DESC;' | column -t -s'|' | less
return 0
fi
# Search bookmarks
if (($# > 0))
then
if (( $+commands[fzf] ))
then
local dir="$(sqlite3 $db "SELECT dir FROM marks ORDER BY weight DESC;" | fzf +s -0 -1 -q"$*" || echo $pipestatus[2])"
else
# Note: for more than 2 arguments, not all permutations are tried.
# So for c 1 2 3, %1%2%3% and %3%2%1% are only tried.
local dir="$(sqlite3 $db "SELECT dir FROM marks WHERE dir LIKE '%${(j.%.)@}%' or dir LIKE '%${(j.%.)${(aO)@}}%' ORDER BY weight DESC LIMIT 1;")"
[[ -z $dir ]] && dir=1
fi
else
if (( $+commands[fzf] ))
then
local dir="$(sqlite3 $db "SELECT dir FROM marks ORDER BY weight DESC;" | fzf +s -0 -1 || echo $pipestatus[2])"
else
local dir="$(sqlite3 $db "SELECT dir FROM marks ORDER BY weight DESC LIMIT 1;")"
[[ -z $dir ]] && dir=1
fi
fi
if [[ -d $dir ]]
then
cd -- $dir
# Try locate,
# only if there were no matches, not if 'Ctrl+c' (dir = 130) was used for instance
elif ((dir == 1)) && (( $+commands[fzf] ))
then
if (($# > 0))
then
print -P '%F{yellow}trying locate%f...'
local file="$(locate -0 / | grep -zv '/\.\(git\|svn\|hg\)\(/\|$\)\|~$' | fzf --read0 -0 -1 -q"$*" || echo $pipestatus[3])"
# try mdfind
if [[ ! -e $file ]]
then
if ((file == 1)) && [[ $(uname) == Darwin && "$*" != .* && "$*" != *\ .* ]]
then
print -P '%F{yellow}trying mdfind%f...'
file="$(mdfind -0 / | grep -zv '/\.\(git\|svn\|hg\)\(/\|$\)\|~$' | fzf --read0 -0 -1 -q"$*")"
fi
fi
else
print -P '%F{yellow}trying locate%f...'
local file="$(locate -0 / | grep -zv '/\.\(git\|svn\|hg\)\(/\|$\)\|~$' | fzf --read0 -0 -1 || echo $pipestatus[3])"
if [[ ! -e $file ]]
then
if ((file == 1)) && [[ $(uname) == Darwin && "$*" != .* && "$*" != *\ .* ]]
then
print -P '%F{yellow}trying mdfind%f...'
file="$(mdfind -0 / | grep -zv '/\.\(git\|svn\|hg\)\(/\|$\)\|~$' | fzf --read0 -0 -1)"
fi
fi
fi
if [[ -n $file ]]
then
if [[ -d $file ]]
then
cd -- $file
else
cd -- ${file:h}
fi
fi
fi