summary refs log tree commit diff
path: root/bin/fzfez
blob: 203927ec0b5334784bb73e512af00dea472f7ac8 (plain) (blame)
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
#!/bin/sh

set -e
[ -n "$DEBUG" ] && set -x

exec_hist=/tmp/fzf-interactive_exec-hist
if [ ! -f "$exec_hist" ]; then
	touch "$exec_hist"
fi

_cat='cat'
if bat --version >/dev/null; then
	_cat='bat --color=always --style=numbers,changes'
fi
_ls='ls'
if exa --version >/dev/null; then
	_ls='exa --classify --group --header --group-directories-first --color=always'
fi

case "$1" in
help|-h|--help)
	printf "usage: fzfez [exec|git-log|git-tag|git-branches|rg|fd|vimwiki|*]\n"
	exit 1 ;;

exec)
	uniq "$exec_hist" \
		| grep -v '^$' \
		| fzf \
			--header "${FZFEZ_HEADER:-EXEC:} select or enter a command, {} is \"$2\"" \
			--bind 'tab:replace-query' \
			--bind 'enter:print-query' \
		| tee -a $exec_hist \
		| sed "s#{}#$2#g"  \
		| sh -s
	exit $?
	;;

_preview)
	case "$(xdg-mime query filetype "$2")" in
	inode/directory)                $_ls -la "$2" ;;
	text/*)                         $_cat "$2" ;;
	application/javascript)         $_cat "$2" ;;
	application/x-yaml)             $_cat "$2" ;;
	application/x-shellscript)      $_cat "$2" ;;
	application/json)               jq "$2" || $_cat "$2" ;;
	application/x-compressed-tar)   tar -tvf "$2" ;;
	application/pdf)                pdftotext "$2" - ;;
	image/*)                        mediainfo "$2" ;;
	video/*|audio/*)                mediainfo "$2" ;;
	*)                              file "$2" ;;
	esac
	;;
_preview-detach) # invoke for media files
	case "$(xdg-mime query filetype "$2")" in
	inode/directory)   swx -f vifm "$2" ;;
	image/*)           swx -f mvi "$2" ;;
	video/*|audio/*)   swx -f mpv "$2" ;;
	application/pdf)   swx -f zathura "$2" ;;
	text/*)            swx -t ${EDITOR} "$2" ;;
	*)                 env FZFEZ_HEADER='PREVIEW-DETACH' $0 exec "$2" ;;
	esac
	;;

git-branches)
	git rev-parse HEAD > /dev/null 2>&1 || exit 1
	git branch -a --sort=refname --format='%(refname)' \
		| cut -d' ' -f1 \
		| sed 's#^refs/##' \
		| sed 's#^remotes/##' \
		| sed 's#^heads/##' \
		| env FZFEZ_HEADER='BRANCHES:' \
			$0 \
			--ansi --no-sort --reverse --tac \
			--preview 'git log --color=always --oneline --graph --date=short {} | head -${LINES}' \
			--preview-window right:70% \
			--bind "ctrl-o:execute(git checkout {})+abort"
	;;

git-tags)
	git rev-parse HEAD > /dev/null 2>&1 || exit 1
		git tag --sort -version:refname \
		| cut -d' ' -f1 \
		| sed 's#^refs/##' \
		| sed 's#^remotes/##' \
		| sed 's#^heads/##' \
		| env FZFEZ_HEADER='TAGS:' \
			$0 \
			--ansi --no-sort --reverse \
			--preview 'git show --color=always {} | head -${LINES}' \
			--preview-window right:70%
	;;

git-log)
	git log --color=always --oneline \
		| env FZFEZ_HEADER='LOG:' \
			$0 \
			--ansi --no-sort --reverse \
			--preview 'echo {} | cut -d\  -f1 | xargs git show --color=always --pretty=full --stat --patch' \
			--preview-window right:50% \
		| cut -d' ' -f1 \
		| xargs -r -i git log -1 --pretty=%H {}
	;;

rg)
	shift
	rg --files-with-matches "$@" \
		| env FZFEZ_HEADER='RG:' \
			$0 \
			--preview "rg \"$*\" --color=always --no-filename --glob='{}' --context=5 --line-number"
	;;

fd)
	shift
	fd "$@" \
		| env FZFEZ_HEADER='FD: c-o (xdg-open)' \
			$0 \
			--ansi --reverse \
			--bind "ctrl-o:execute(xdg-open {})+abort" \
			--preview-window ':hidden'
	;;

*)
	fzf \
		--header "${FZFEZ_HEADER:-FZF:} c-x (exec), ? (preview), c-space (preview-detach), c-s (sort)" \
		--preview "${0} _preview {}" \
		--preview-window ':hidden' \
		--bind '?:toggle-preview' \
		--bind 'ctrl-s:toggle-sort' \
		--bind "ctrl-space:execute(${0} _preview-detach {})" \
		--bind "ctrl-y:yank" \
		--bind "ctrl-e:execute(${EDITOR} {})+abort" \
		--bind "ctrl-x:execute(env FZFEZ_HEADER=EXEC: ${0} exec {})+abort" \
		"$@" ;;
esac