-
Notifications
You must be signed in to change notification settings - Fork 1
/
fuz
executable file
·76 lines (66 loc) · 1.76 KB
/
fuz
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
#!/usr/bin/env bash
# -*- coding: UTF-8 -*-
## Fuzzy search and open file from commandline
## options:
## -e, --edit <command> The editor to use [default: none]
## -d, --directory <path> The path where to look for files [default: .]
## see https://gist.github.com/BaseCase/c45299e4f8474119881d708a4b728fbf#file-fuz-sh
# GENERATED_CODE: start
# Default values
_edit=none
_directory=.
# Converting long-options into short ones
for arg in "$@"; do
shift
case "$arg" in
"--edit") set -- "$@" "-e";;
"--directory") set -- "$@" "-d";;
*) set -- "$@" "$arg"
esac
done
function print_illegal() {
echo Unexpected flag in command line \"$@\"
}
# Parsing flags and arguments
while getopts 'he:d:' OPT; do
case $OPT in
h) sed -ne 's/^## \(.*\)/\1/p' $0
exit 1 ;;
e) _edit=$OPTARG ;;
d) _directory=$OPTARG ;;
\?) print_illegal $@ >&2;
echo "---"
sed -ne 's/^## \(.*\)/\1/p' $0
exit 1
;;
esac
done
# GENERATED_CODE: end
set -e
xclip=$(which xclip)
main() {
previous_file="$1"
file_to_edit=`select_file $previous_file`
if [ -n "$file_to_edit" ] && [ "$_edit" != none ] ; then
"$_edit" "$file_to_edit"
else if [ ! -z $xclip ]; then
echo $file_to_edit | tr -d '\n' | xclip -selection clipboard
echo "$file_to_edit is available in the clipboard"
else
echo $file_to_edit
fi
fi
}
select_file() {
given_file="$1"
has_bat=$(which bat | wc -l)
if [ $has_bat -eq 0 ]; then
fzf --preview="cat {}" --preview-window=right:70%:wrap --query="$given_file"
else
fzf --preview="bat {} --color=always" --preview-window=right:70%:wrap --query="$given_file"
fi
}
previousdirectory=`pwd`
cd "$_directory"
main "$@"
cd "$previousdirectory"