summary refs log tree commit diff
path: root/bin/fzfez
diff options
context:
space:
mode:
Diffstat (limited to 'bin/fzfez')
-rwxr-xr-xbin/fzfez103
1 files changed, 62 insertions, 41 deletions
diff --git a/bin/fzfez b/bin/fzfez
index dea63a1..dff86ba 100755
--- a/bin/fzfez
+++ b/bin/fzfez
@@ -6,40 +6,57 @@ 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 'Select or enter a command, {} is previous selection' \
+			--header "${FZFEZ_HEADER:-EXEC:} select or enter a command, {} is \"${FZFEZ_EXEC_SELECTION}\"" \
 			--bind 'tab:replace-query' \
-			--bind 'enter:print-query' \
-		| tee -a $exec_hist \
+			--bind 'esc:print-query' \
+			--bind 'enter:replace-query+accept' \
+		| tee -a $exec_hist >/dev/null \
 		| sed "s#{}#$2#g"  \
-		| sh -s
+		| sh -x -s
 	exit $?
 	;;
 
 _preview)
 	case "$(xdg-mime query filetype "$2")" in
-	inode/directory) 			exa -la "$2" ;;
-	text/*)					head "$2" ;;
-	application/javascript)			head "$2" ;;
-	application/x-shellscript)		head "$2" ;;
-	application/json)			jq "$2" ;;
-	application/x-compressed-tar)		tar -tvf "$2" ;;
-	application/pdf)			pdftotext "$2" - ;;
-	image/*)				mediainfo "$2" ;;
-	video/*|audio/*)			mediainfo "$2" ;;
-	*)					file "$2" ;;
+	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
 	;;
-_preview2) # invoke mpv/imv for media files
+_preview-detach) # invoke for media files
 	case "$(xdg-mime query filetype "$2")" in
-	image/*)				swx -f imv "$2" ;;
-	video/*|audio/*)			swx -f mpv "$2" ;;
-	application/pdf)			swx -f zathura "$2" ;;
-	*)					$0 exec "$2" ;;
+	inode/directory)   exa -la "$2" ;;
+	image/*)           swx -f mvi "$2" ;;
+	video/*|audio/*)   swx -f mpv "$2" ;;
+	application/pdf)   swx -f zathura "$2" ;;
+	test/*)            swx -t ${EDITOR} "$2" ;;
+	*)                 env FZFEZ_HEADER='PREVIEW-DETACH' $0 exec "$2" ;;
 	esac
 	;;
 
@@ -50,13 +67,12 @@ git-branches)
 		| sed 's#^refs/##' \
 		| sed 's#^remotes/##' \
 		| sed 's#^heads/##' \
-		| $0 \
+		| env FZFEZ_HEADER='BRANCHES:' \
+			$0 \
 			--ansi --no-sort --reverse --tac \
-			--header 'Press ctrl-s to toggle sort' \
-			--bind 'ctrl-s:toggle-sort' \
 			--preview 'git log --color=always --oneline --graph --date=short {} | head -${LINES}' \
 			--preview-window right:70% \
-			--bind "ctrl-o:execute(git checkout {})"
+			--bind "ctrl-o:execute(git checkout {})+abort"
 	;;
 
 git-tags)
@@ -66,7 +82,8 @@ git-tags)
 		| sed 's#^refs/##' \
 		| sed 's#^remotes/##' \
 		| sed 's#^heads/##' \
-		| $0 \
+		| env FZFEZ_HEADER='TAGS:' \
+			$0 \
 			--ansi --no-sort --reverse \
 			--preview 'git show --color=always {} | head -${LINES}' \
 			--preview-window right:70%
@@ -74,7 +91,8 @@ git-tags)
 
 git-log)
 	git log --color=always --oneline \
-		| $0 \
+		| 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% \
@@ -84,11 +102,10 @@ git-log)
 
 rg)
 	shift
-	rg $@ --files-with-matches \
-		| $0 \
-			--header 'Press ctrl-s to toggle sort' \
-			--bind 'ctrl-s:toggle-sort' \
-			--preview "rg $* --color=always --no-filename --glob='{}' --context=5 --line-number"
+	rg --files-with-matches "$@" \
+		| env FZFEZ_HEADER='RG:' \
+			$0 \
+			--preview "rg \"$*\" --color=always --no-filename --glob='{}' --context=5 --line-number"
 	;;
 
 vimwiki)
@@ -96,26 +113,30 @@ vimwiki)
 	fd -t f -e md "${1:-.*}" "$HOME/wiki" \
 		| sed "s#^$HOME/wiki/##" \
 		| sed 's#.md$##' \
-		| $0 \
+		| env FZFEZ_HEADER='VIMWIKI:' \
+			$0 \
 			--print-query \
-			--preview="cat -n $HOME/wiki/{}.md" \
+			--preview="$_cat -n $HOME/wiki/{}.md" \
 		| xargs -r -i nvim +VimwikiIndex +VimwikiGoto\ {}
 	;;
 
 fd)
 	shift
-	fd $@ \
-		| $0 \
+	fd "$@" \
+		| env FZFEZ_HEADER='FD: c-o (xdg-open)' \
+			$0 \
 			--ansi --reverse \
-			--header 'Press ctrl-p to toggle preview, enter for xdg-open' \
-			--bind 'ctrl-p:toggle-preview' \
-			--bind "ctrl-space:execute($0 _preview2 {})" \
-			--preview "$0 _preview {}" \
-			--preview-window ':hidden' \
-		| xargs -r xdg-open
+			--bind "ctrl-o:execute(xdg-open {})+abort" \
+			--preview-window ':hidden'
 	;;
 
 *)
 	fzf \
-		--bind "ctrl-x:execute(${0} exec {})+accept" "$@" ;;
+		--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-x:execute(env FZFEZ_HEADER=EXEC: FZFEZ_EXEC_SELECTION={} ${0} exec {})+accept" "$@" ;;
 esac