This repository has been archived by the owner on Mar 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfzf-fs-init
executable file
·340 lines (309 loc) · 14.4 KB
/
fzf-fs-init
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env bash
# fzf-fs-init
# part of fzf-fs
# Copyright (C) 2015 D630, The MIT License (MIT)
# <https://github.com/D630/fzf-fs>
# -- DEBUGGING.
#printf '%s (%s)\n' "$BASH_VERSION" "${BASH_VERSINFO[5]}" && exit 0
#set -o errexit
#set -o errtrace
#set -o noexec
#set -o nounset
#set -o pipefail
#set -o verbose
#set -o xtrace
#trap '(read -p "[$BASH_SOURCE:$LINENO] $BASH_COMMAND?")' DEBUG
#exec 2>> ~/fzf-fs.log
#typeset vars_base=$(set -o posix ; set)
#fgrep -v -e "$vars_base" < <(set -o posix ; set) | \
#egrep -v -e "^BASH_REMATCH=" \
# -e "^OPTIND=" \
# -e "^REPLY=" \
# -e "^BASH_LINENO=" \
# -e "^BASH_SOURCE=" \
# -e "^FUNCNAME=" | \
#less
# -- FUNCTIONS.
__fzffs_init ()
{
builtin typeset FZF_FS_CONFIG_DIR="${FZF_FS_CONFIG_DIR:-${XDG_CONFIG_HOME:-${HOME}/.config}/fzf-fs.d}";
__fzffs_util_echoE "${source}:Info: FZF_FS_CONFIG_DIR is ${FZF_FS_CONFIG_DIR}" 1>&2;
if [[ -e "$FZF_FS_CONFIG_DIR" ]]; then
__fzffs_util_echoE "${source}:Error:80: Directory existing: '${FZF_FS_CONFIG_DIR}'" 1>&2;
builtin return 80;
else
command mkdir -p -m 755 "${FZF_FS_CONFIG_DIR}"/{console/set,env};
__fzffs_util_echoE "${source}:Info: Initializing empty fzf-fs config directory in ${FZF_FS_CONFIG_DIR}" 1>&2;
fi;
builtin unset -v FZF_FS_LS_COMMAND FZF_FS_LS_COMMAND_COLOR FZF_FS_OS;
builtin typeset FZF_FS_LS_COMMAND= FZF_FS_LS_COMMAND_COLOR= FZF_FS_OS=;
case "$(command uname -s)" in
DragonFly|FreeBSD)
FZF_FS_OS=FreeBSD;
FZF_FS_LS_COMMAND="command ls -G -q";
FZF_FS_LS_COMMAND_COLOR="CLICOLOR_FORCE=1 command ls -G -q"
;;
OpenBSD)
FZF_FS_OS=OpenBSD;
if command -v colorls > /dev/null; then
FZF_FS_LS_COMMAND="command colorls -G -q";
FZF_FS_LS_COMMAND_COLOR="CLICOLOR_FORCE=1 command colorls -G -q";
else
if command -v gls > /dev/null; then
FZF_FS_LS_COMMAND="command gls --color=auto -q";
FZF_FS_LS_COMMAND_COLOR="command gls --color=always -q";
else
FZF_FS_LS_COMMAND="command ls";
FZF_FS_LS_COMMAND_COLOR="command ls";
fi;
fi
;;
Darwin)
FZF_FS_OS=Darwin;
FZF_FS_LS_COMMAND="command ls -G -q";
FZF_FS_LS_COMMAND_COLOR="CLICOLOR_FORCE=1 command ls -G -q"
;;
SunOS)
FZF_FS_OS=SunOS;
FZF_FS_LS_COMMAND="command ls --color=auto -q";
FZF_FS_LS_COMMAND_COLOR="command ls --color=always -q"
;;
*)
FZF_FS_OS=Linux;
FZF_FS_LS_COMMAND="command ls --color=auto -q";
FZF_FS_LS_COMMAND_COLOR="command ls --color=always -q"
;;
esac;
__fzffs_util_echoE "Creating ${FZF_FS_CONFIG_DIR}/env/console_shortcuts.default" 1>&2;
__fzffs_util_echoE "Creating ${FZF_FS_CONFIG_DIR}/env/console_shortcuts.user" 1>&2;
builtin unset -v f v s;
builtin typeset f= v= s=;
while IFS='|' builtin read -r f v s; do
__fzffs_util_echoE "Creating ${FZF_FS_CONFIG_DIR}${f}" 1>&2;
__fzffs_util_echoE "$v" > "${FZF_FS_CONFIG_DIR}${f}";
__fzffs_util_echoE "[${s:- }] ${f#*/*/}" >> "${FZF_FS_CONFIG_DIR}/env/console_shortcuts.default";
__fzffs_util_echoE "[${s:- }] ${f#*/*/}" >> "${FZF_FS_CONFIG_DIR}/env/console_shortcuts.user";
f=;
v=;
s=;
done <<-'SETTINGS'
/console/set/clicolor_force_false|console="FZF_FS_LS_CLICOLOR=0"
/console/set/clicolor_force_toggle|console="FZF_FS_LS_CLICOLOR=$((FZF_FS_LS_CLICOLOR ? 0 : 1))"
/console/set/clicolor_force_true|console="FZF_FS_LS_CLICOLOR=1"
/console/set/deference_commandline|console="FZF_FS_LS_SYMLINK=H"
/console/set/deference|console="FZF_FS_LS_SYMLINK=L"
/console/set/lc_collate_c|console="LC_COLLATE=C"
/console/set/lc_collate_lang|console="LC_COLLATE=$LANG"
/console/set/opener_console_default|console="FZF_FS_OPENER_CONSOLE=$FZF_FS_OPENER_CONSOLE_DEFAULT"|d
/console/set/opener_console_editor|console="FZF_FS_OPENER_CONSOLE=$EDITOR"|e
/console/set/opener_console_pager|console="FZF_FS_OPENER_CONSOLE=$PAGER"|p
/console/set/opener_default|console="FZF_FS_OPENER=$FZF_FS_OPENER_DEFAULT"|d
/console/set/opener_editor|console="FZF_FS_OPENER=$EDITOR"|e
/console/set/opener_interactive|console="FZF_FS_OPENER=$(command fzf --prompt="FZF_FS_OPENER " --print-query <<< "")"|oi
/console/set/opener_pager|console="FZF_FS_OPENER=$PAGER"|p
/console/set/os_interactive|console="FZF_FS_OS=$(command fzf --prompt="uname -s => " --print-query <<< "")"
/console/set/show_atime|console="FZF_FS_LS_OPTS=u"
/console/set/show_ctime|console="FZF_FS_LS_OPTS=c"
/console/set/show_hidden_false|console="FZF_FS_LS_HIDDEN=0"
/console/set/show_hidden_toggle|console="FZF_FS_LS_HIDDEN=$((FZF_FS_LS_HIDDEN ? 0 : 1))"
/console/set/show_hidden_true|console="FZF_FS_LS_HIDDEN=1"
/console/set/show_mtime|console="FZF_FS_LS_OPTS="|om
/console/set/sort_atime|console="FZF_FS_LS_OPTS=ut"|oa
/console/set/sort_basename|console="FZF_FS_LS_OPTS="|ob
/console/set/sort_ctime|console="FZF_FS_LS_OPTS=ct"|oc
/console/set/sort_interactive|console="FZF_FS_SORT=$(command fzf --prompt="sort " --print-query <<< "")"|si
/console/set/sort_mtime|console=FZF_FS_LS_OPTS="t"
/console/set/sort_reverse_false|console="FZF_FS_LS_REVERSE=0"
/console/set/sort_reverse_toggle|console="FZF_FS_LS_REVERSE=$((FZF_FS_LS_REVERSE ? 0 : 1))"|or
/console/set/sort_reverse_true|console="FZF_FS_LS_REVERSE=1"
/console/set/sort_size|console="FZF_FS_SORT=k6,6n"|os
/console/set/sort_type|console="FZF_FS_SORT=k2"|ot
SETTINGS
__fzffs_util_echoE "[q] quit" >> "${FZF_FS_CONFIG_DIR}/env/console_shortcuts.default";
__fzffs_util_echoE "[q] quit" >> "${FZF_FS_CONFIG_DIR}/env/console_shortcuts.user";
__fzffs_util_echoE "Creating ${FZF_FS_CONFIG_DIR}/env/env.default" 1>&2;
builtin unset -v env;
{
builtin typeset env="$(</dev/fd/0)"
} <<-'ENV'
builtin typeset EDITOR="${EDITOR:-nano}"
builtin typeset FZF_FS_CONFIG_DIR="$FZF_FS_CONFIG_DIR"
builtin typeset FZF_FS_DEFAULT_OPTS="${FZF_FS_DEFAULT_OPTS:--x -i}"
builtin typeset FZF_FS_LS_OPTS="${FZF_FS_LS_OPTS:+${FZF_FS_LS_OPTS}}"
builtin typeset FZF_FS_SORT="${FZF_FS_SORT:+${FZF_FS_SORT}}"
builtin typeset FZF_FS_LS_SYMLINK="$FZF_FS_LS_SYMLINK"
builtin typeset PAGER="${PAGER:-less -R}"
builtin typeset TERMINAL="${TERMINAL:-xterm}"
builtin typeset -i FZF_FS_LS_CLICOLOR="${FZF_FS_LS_CLICOLOR:-1}"
builtin typeset -i FZF_FS_LS_HIDDEN="${FZF_FS_LS_HIDDEN:-1}"
builtin typeset -i FZF_FS_LS_REVERSE="${FZF_FS_LS_REVERSE:-1}"
builtin typeset -x FZF_DEFAULT_COMMAND_OLD="${FZF_DEFAULT_COMMAND:+${FZF_DEFAULT_COMMAND}}"
builtin typeset -x FZF_FS_DEFAULT_OPTS_OLD="${FZF_DEFAULT_OPTS:+${FZF_DEFAULT_OPTS}}"
builtin typeset -x LC_COLLATE_OLD="$LC_COLLATE"
builtin typeset FZF_FS_OPENER_CONSOLE="$FZF_FS_OPENER_CONSOLE"
builtin typeset FZF_FS_OPENER_CONSOLE_DEFAULT="$FZF_FS_OPENER_CONSOLE"
builtin typeset FZF_FS_OPENER="${FZF_FS_OPENER:-${PAGER}}"
builtin typeset FZF_FS_OPENER_DEFAULT="$FZF_FS_OPENER"
builtin typeset -x FZF_DEFAULT_COMMAND="command echo uups"
builtin typeset -x FZF_DEFAULT_OPTS=
builtin typeset -x LC_COLLATE=C
ENV
{
__fzffs_util_echoE "$env";
__fzffs_util_echoE "builtin typeset FZF_FS_OS=\"\${FZF_FS_OS:-${FZF_FS_OS}}\"";
__fzffs_util_echoE "builtin typeset FZF_FS_LS_COMMAND=\"\${FZF_FS_LS_COMMAND:-${FZF_FS_LS_COMMAND}}\"";
__fzffs_util_echoE "builtin typeset FZF_FS_LS_COMMAND_COLOR=\"\${FZF_FS_LS_COMMAND_COLOR:-${FZF_FS_LS_COMMAND_COLOR}}\""
} > "${FZF_FS_CONFIG_DIR}/env/env.default";
__fzffs_util_echoE "Creating ${FZF_FS_CONFIG_DIR}/env/env.user" 1>&2;
__fzffs_util_echon "builtin . \"\${FZF_FS_CONFIG_DIR}/env/env.default\"" > "${FZF_FS_CONFIG_DIR}/env/env.user";
__fzffs_util_echoE "Creating ${FZF_FS_CONFIG_DIR}/env/macros.default" 1>&2;
builtin unset -v macros;
{
builtin typeset macros="$(</dev/fd/0)"
} <<-'MACROS'
console_args="${console_args//[%][b]/${FZF_FS_CONFIG_DIR}/env/browser_shortcuts.user}"
console_args="${console_args//[%][c]/${FZF_FS_CONFIG_DIR}/env/console_shortcuts.user}"
console_args="${console_args//[%][d]/$browser_pwd}"
console_args="${console_args//[%][e]/${FZF_FS_CONFIG_DIR}/env/env.user}"
console_args="${console_args//[%][f]/${FZF_FS_CONFIG_DIR}/env/flags.user}"
console_args="${console_args//[%][m]/${FZF_FS_CONFIG_DIR}/env/macros.user}"
console_args="${console_args//[%][s]/$browser_file}"
MACROS
__fzffs_util_echoE "$macros" >> "${FZF_FS_CONFIG_DIR}/env/macros.default";
__fzffs_util_echoE "Creating ${FZF_FS_CONFIG_DIR}/env/macros.user" 1>&2;
__fzffs_util_echon "builtin . \"\${FZF_FS_CONFIG_DIR}/env/macros.default\"" > "${FZF_FS_CONFIG_DIR}/env/macros.user";
__fzffs_util_echoE "Creating ${FZF_FS_CONFIG_DIR}/env/flags.default" 1>&2;
builtin unset -v flags_default;
{
builtin typeset flags_default="$(</dev/fd/0)"
} <<-'FLAGS'
flags_func ()
{
case "$REPLY" in
"f")
console_fork_background=console_fork_background
;;
"k")
console_keep=console_keep
;;
"t")
console_terminal=console_terminal
;;
*)
builtin :
;;
esac
}
FLAGS
__fzffs_util_echoE "$flags_default" > "${FZF_FS_CONFIG_DIR}/env/flags.default";
__fzffs_util_echoE "Creating ${FZF_FS_CONFIG_DIR}/env/flags.user" 1>&2;
__fzffs_util_echon "builtin . \"\${FZF_FS_CONFIG_DIR}/env/flags.default\"" > "${FZF_FS_CONFIG_DIR}/env/flags.user";
__fzffs_util_echoE "Creating ${FZF_FS_CONFIG_DIR}/env/browser_shortcuts.default" 1>&2;
builtin unset -v browser_shortcuts;
{
builtin typeset browser_shortcuts="$(</dev/fd/0)"
} <<-'BROWSER_SHORTCUTS'
_ [!] console/shell
_ [/] console/cd $browser_root
_ [:] console
_ [q] quit
_ [~] console/cd $HOME
_ [-] console/cd $OLDPWD
BROWSER_SHORTCUTS
__fzffs_util_echoE "$browser_shortcuts" > "${FZF_FS_CONFIG_DIR}/env/browser_shortcuts.default";
__fzffs_util_echoE "Creating ${FZF_FS_CONFIG_DIR}/env/browser_shortcuts.user" 1>&2;
__fzffs_util_echoE "$browser_shortcuts" > "${FZF_FS_CONFIG_DIR}/env/browser_shortcuts.user";
function __fzffs_console_edit ()
{
builtin unset -v console_args console_prompt;
builtin typeset console_args="$*" console_prompt="${console_file##*/}";
[[ -n "$console_args" || "$console_interactive" -eq 0 ]] || {
if [[ -n "$KSH_VERSION" ]]; then
console_args="$(command fzf --prompt=":${console_prompt} " --print-query <<< "")";
else
command tput sc;
command tput cup 99999 0;
if [[ -n "$ZSH_VERSION" ]]; then
builtin vared -p ":${console_prompt} " console_args;
else
builtin read -re -p ":${console_prompt} " console_args;
fi;
command tput rc;
command tput ed;
fi
};
builtin set -- ${console_args};
(($# > 0)) && {
__fzffs_util_parse_flags "$*" && builtin shift 1;
__fzffs_util_parse_macros "$*"
};
builtin eval ${EDITOR} "$console_args"
};
function __fzffs_util_rename_function ()
{
builtin test -n "$(builtin typeset -f "${1:-1}")" || builtin return 1;
builtin eval "${_/${1}/${2:-1}}" && builtin unset -f "$1"
};
{
builtin typeset code_shell="$(</dev/fd/0)"
} <<'CODE'
if [[ "$console_terminal" == "console_terminal" ]]; then
if [[ "$console_fork_background" == "console_fork_background" ]]; then
( builtin eval ${TERMINAL} -e "${console_args}\;${console_keep:+${SHELL:-sh}}" \& );
else
builtin eval ${TERMINAL} -e "${console_args}\;${console_keep:+${SHELL:-sh}}";
fi;
else
if [[ "$console_fork_background" == "console_fork_background" ]]; then
( builtin eval ${SHELL:-sh} "${console_args:+-c $console_args}" \& );
else
builtin eval ${SHELL:-sh} "${console_args:+-c $console_args}" ${console_keep:+\; command printf '%s\\n' \'Press ENTER to continue\' ; builtin read};
fi;
fi
CODE
function __fzffs_console_terminal ()
{
( command ${SHELL:-sh} -c ${TERMINAL} & )
};
f=;
v=;
s=;
while IFS='|' builtin read -r f v s; do
__fzffs_util_echoE "Creating ${FZF_FS_CONFIG_DIR}${f}" 1>&2;
__fzffs_util_echoE "#!/usr/bin/env bash" > "${FZF_FS_CONFIG_DIR}${f}";
__fzffs_util_rename_function "$v" console_func;
if [[ "$v" == *"open_with" ]]; then
builtin typeset -f console_func | command sed 's/builtin eval ${EDITOR} "$console_args"/builtin eval "$(__fzffs_util_echon builtin eval "$console_args")"/' >> "${FZF_FS_CONFIG_DIR}${f}";
else
if [[ $v == *page ]]; then
builtin typeset -f console_func | command sed 's/EDITOR/PAGER/' >> "${FZF_FS_CONFIG_DIR}${f}";
else
if [[ "$v" == *"cd" ]]; then
builtin typeset -f console_func | command sed 's/builtin eval ${EDITOR} "$console_args"/browser_pwd="$(builtin eval __fzffs_util_echon "$console_args")"/' >> "${FZF_FS_CONFIG_DIR}${f}";
else
if [[ "$v" == *"shell" ]]; then
builtin typeset -f console_func | command sed -e 's/ builtin eval ${EDITOR} "$console_args"//' -e 's/^}//' -e '/^$/d' >> "${FZF_FS_CONFIG_DIR}${f}";
__fzffs_util_echoE "$code_shell" >> "${FZF_FS_CONFIG_DIR}${f}";
__fzffs_util_echo "}" >> "${FZF_FS_CONFIG_DIR}${f}";
else
builtin typeset -f console_func >> "${FZF_FS_CONFIG_DIR}${f}";
fi;
fi;
fi;
fi;
__fzffs_util_echoE "[${s:- }] ${f#*/*/}" >> "${FZF_FS_CONFIG_DIR}/env/console_shortcuts.default";
__fzffs_util_echoE "[${s:- }] ${f#*/*/}" >> "${FZF_FS_CONFIG_DIR}/env/console_shortcuts.user";
done <<-COMMANDS
/console/edit|__fzffs_console_edit|e
/console/cd|__fzffs_console_cd|cd
/console/open_with|__fzffs_console_open_with|o
/console/page|__fzffs_console_page|p
/console/shell|__fzffs_console_shell|s
/console/terminal|__fzffs_console_terminal|t
COMMANDS
builtin unset -f __fzffs_console_edit __fzffs_console_shell __fzffs_console_terminal __fzffs_util_rename_function;
__fzffs_util_echoE "${source}:Info: Initialized empty fzf-fs config directory in ${FZF_FS_CONFIG_DIR}" 1>&2;
builtin return 0
}
# -- MAIN.
__fzffs_init