-
Notifications
You must be signed in to change notification settings - Fork 5
/
zfm.zsh
261 lines (235 loc) · 7.58 KB
/
zfm.zsh
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
function __zfm_select_bookmarks()
{
setopt localoptions pipefail no_aliases 2> /dev/null
local opts="--reverse --exact --no-sort --cycle --height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS"
__zfm_decorate | FZF_DEFAULT_OPTS="$@ ${opts}" fzf | awk '{ print $1 }'
}
function __zfm_select_with_query()
{
setopt localoptions pipefail no_aliases 2> /dev/null
local opts="--reverse --exact --no-sort --cycle --height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS"
__zfm_decorate | FZF_DEFAULT_OPTS="${opts}" fzf -q "$@" -1 -0 | awk '{ print $1 }'
}
function __zfm_filter_files()
{
while read line
do
if [ -f $line ]; then
echo $line
fi
done
}
function __zfm_filter_dirs()
{
while read line
do
if [ -d $line ]; then
echo $line
fi
done
}
function __zfm_decorate()
{
while read line
do
if [ ! -z "$line" ];then
if [ -d "$line" ]; then
echo "$line" "[d]"
elif [ -f "$line" ]; then
echo "$line" "[f]"
fi
fi
done | column -t
}
function __zfm_filter_non_existent()
{
while read line
do
if [[ -d $line ]] || [[ -e $line ]]; then
echo $line
fi
done
}
function __zfm_check_regex()
{
local command="$1"
local regex="$2"
shift 2
for arg in "$@"
do
if ! [[ "$arg" =~ "$regex" ]]; then
echo "Invalid Argument for command ${command}: '${arg}'"
return 1
fi
done
}
function __zfm_add_items_to_file()
{
for var in "${@:2}"
do
local item=${var:A}
if [ ! -e "$item" ] && [ ! -d "$item" ]; then
echo "$item" does not exist!
return 1
fi
echo "$item" >> "$1"
echo "$item" added!
done
local contents=$(cat "$1")
echo "$contents" | awk '!a[$0]++' > "$1"
}
function __zfm_cleanup()
{
local old_length=$(wc -l "$1" | cut -d\ -f 1)
local contents=$(cat "$1")
echo "$contents" | awk '!a[$0]++' | __zfm_filter_non_existent > $1
local new_length=$(wc -l "$1" | cut -d\ -f 1)
echo "removed" $(( $old_length - $new_length)) "entries"
}
usage="$(basename "$0") [-h] <command> [opts] -- fuzzy marks
commands:
list [--files] [--dirs] list bookmarks
add <path> [paths...] bookmark items
select [--files] [--dirs] [--multi] select bookmark(s) and print selection to sdtout
query <pattern> Query bookmark matching <pattern> and print match to stdout. Selection menu will open if match is ambiguous.
edit edit bookmarks file
fix remove bookmarked that no longer exist
clear clear all bookmarks
options:
-h,--help show help
--files restrict to files only
--dirs restrict to dirs only
--multi allow multiple selection of items
keybindings:
Ctrl+P Select a bookmarked directory and jump to it
Ctrl+O Select one or multiple bookmarks and insert them into the current command line
ENV configuration:
ZFM_NO_BINDINGS Disabled creation of bindings
ZFM_BOOKMARKS_FILE Bookmarks file. Defaults to '~/.zfm.txt'
"
function set_bookmarks_file()
{
[[ -n "${ZFM_BOOKMARKS_FILE+x}" ]] && local bookmarks_file="$ZFM_BOOKMARKS_FILE" || local bookmarks_file="${HOME}/.zfm.txt"
if [ ! -e "$bookmarks_file" ]; then
touch "$bookmarks_file" &> /dev/null
fi
if [ ! -f "$bookmarks_file" ]; then
printf "ZFM failed to create a bookmarks file.\n\n ZFM_BOOKMARKS_FILE is currently set to '${ZFM_BOOKMARKS_FILE}' if the ZFM_BOOKMARKS_FILE is variable has been set it has likely not been done correctly, the path does not exists, the variable is not exported, or the zfm cannot access the set directory. See zfm --help for more information.\n"
return 1
fi
echo $bookmarks_file
}
function zfm()
{
bookmarks_file=$(set_bookmarks_file)
case "$1" in
'list')
__zfm_check_regex "$1" '(--files|--dirs)' "${@:2}" || return 1
if [[ $* == *--files* ]]; then
cat "$bookmarks_file" | __zfm_filter_files | __zfm_decorate
elif [[ $* == *--dirs* ]]; then
cat "$bookmarks_file" | __zfm_filter_dirs | __zfm_decorate
else
cat "$bookmarks_file" | __zfm_decorate
fi
;;
'select')
__zfm_check_regex "$1" '(--multi|--files|--dirs)' "${@:2}" || return 1
[[ $* == *--multi* ]] && local multi="-m"
if [[ $* == *--files* ]]; then
cat "$bookmarks_file" | __zfm_filter_files | __zfm_select_bookmarks "${multi}"
elif [[ $* == *--dirs* ]]; then
cat "$bookmarks_file" | __zfm_filter_dirs | __zfm_select_bookmarks "${multi}"
else
cat "$bookmarks_file" | __zfm_select_bookmarks "${multi}"
fi
;;
'add')
echo "Added to: $bookmarks_file"
__zfm_add_items_to_file "$bookmarks_file" "${@:2}" || return 1
;;
'query')
if [[ "$2" == "--files" ]]; then
cat "$bookmarks_file" | __zfm_filter_files | __zfm_select_with_query "${@:3}"
elif [[ "$2" == "--dirs" ]]; then
cat "$bookmarks_file" | __zfm_filter_dirs | __zfm_select_with_query "${@:3}"
else
cat "$bookmarks_file" | __zfm_select_with_query "$2"
fi
;;
'fix')
! [[ -z "${@:2}" ]] && echo "Invalid option '${@:2}' for '$1'" && return 1
__zfm_cleanup "$bookmarks_file"
;;
'clear')
! [[ -z "${@:2}" ]] && echo "Invalid option '${@:2}' for '$1'" && return 1
echo "" > "$bookmarks_file"
echo "bookmarks deleted!"
;;
'edit')
! [[ -z "${@:2}" ]] && echo "Invalid option '${@:2}' for '$1'" && return 1
${EDITOR:-vim} "$bookmarks_file"
;;
'help')
echo "$usage" >&2
;;
*)
echo "$usage" >&2
echo "Unknown command $1"
;;
esac
}
#######################################################################
# CTRL-B - insert bookmark
function __zfm_append_to_prompt()
{
if [[ -z "$1" ]]; then
zle reset-prompt
return 0
fi
LBUFFER="${LBUFFER}$(echo "$1" | tr '\r\n' ' '| sed -e 's/\s$//')"
local ret=$?
zle reset-prompt
return $ret
}
function zfm-insert-bookmark()
{
__zfm_append_to_prompt "$(zfm select --multi)"
}
zle -N zfm-insert-bookmark
if [[ -z $ZFM_NO_BINDINGS ]]; then
bindkey '^O' zfm-insert-bookmark
fi
#######################################################################
# CTRL-P - cd into bookmarked directory
function zfm-cd-to-bookmark() {
local dir=$(zfm select --dirs)
if [[ -z "$dir" ]]; then
zle redisplay
return 0
fi
local ret=$?
zle reset-prompt
BUFFER="cd $dir"
zle accept-line
return $ret
}
zle -N zfm-cd-to-bookmark
if [[ -z $ZFM_NO_BINDINGS ]]; then
bindkey '^P' zfm-cd-to-bookmark
fi
#######################################################################
# f - jump to directory with query
function f()
{
if [ -z "$@" ]; then
local dir=$(zfm select --dirs)
else
local dir=$(zfm query --dirs "$@")
fi
if [[ -z "$dir" ]]; then
return 0
fi
cd "$dir"
}
"$@"