-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfuz
More file actions
executable file
·72 lines (62 loc) · 1.73 KB
/
fuz
File metadata and controls
executable file
·72 lines (62 loc) · 1.73 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
#!/usr/bin/env bash
# -*- coding: UTF-8 -*-
## Fuzzy search and open file from commandline
## options:
## -d, --directory <path> The path where to look for files [default: .]
## see https://gist.github.com/BaseCase/c45299e4f8474119881d708a4b728fbf#file-fuz-sh
# CLInt GENERATED_CODE: start
# info: https://github.com/clobrano/CLInt.git
# Default values
_directory=.
# Converting long-options into short ones
for arg in "$@"; do
shift
case "$arg" in
"--directory") set -- "$@" "-d";;
*) set -- "$@" "$arg"
esac
done
function print_illegal() {
echo "[!] Unexpected flag in command line $*"
}
# Parsing flags and arguments
while getopts 'hd:' OPT; do
case "$OPT" in
h) sed -ne 's/^## \(.*\)/\1/p' "$0"
exit 1 ;;
d) _directory=$OPTARG ;;
\?) print_illegal "$@" >&2;
echo "---"
sed -ne 's/^## \(.*\)/\1/p' "$0"
exit 1
;;
esac
done
# CLInt GENERATED_CODE: end
set -e
xclip=$(which xclip)
main() {
previous_file="$1"
file_to_edit=$(select_file "$previous_file")
if [ -n "$file_to_edit" ] && [ "$EDITOR" != none ] ; then
$EDITOR "$file_to_edit"
elif [ -n "$xclip" ]; then
echo "$file_to_edit" | tr -d '\n' | xclip -selection clipboard
echo "EDITOR is unset: $file_to_edit is available in the clipboard"
else
echo "$file_to_edit"
fi
}
select_file() {
given_file="$1"
fzf \
--prompt "Search your file: " \
--preview="cat {} --color=always" \
--preview-window=right:70%:wrap \
--query="$given_file" \
--bind 'ctrl-y:execute(readlink -f {} | echo {} | cut -d">" -f2 | tr -d " " | tr -d "\n" | xclip -selection clipboard)+abort'
}
previousdirectory=$(pwd)
cd "$_directory"
main "$@"
cd "$previousdirectory"