File: rgfind.sh

package info (click to toggle)
clifm 1.26.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,816 kB
  • sloc: ansic: 64,595; sh: 3,133; python: 1,851; makefile: 567
file content (60 lines) | stat: -rwxr-xr-x 1,637 bytes parent folder | download
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
#!/bin/sh

# Plugin to search for files by content for Clifm
# Dependencies: rg (Ripgrep), fzf, and sed

# Written by L. Abramovich
# License: GPL2+

MAX_DEPTH=1

if [ -n "$1" ] && { [ "$1" = "--help" ] || [ "$1" = "-h" ]; }; then
	name="${CLIFM_PLUGIN_NAME:-$(basename "$0")}"
	printf "Search files by content via Ripgrep and FZF\n"
	printf "\n\x1b[1mUSAGE\x1b[0m\n  %s STRING|REGEXP\n" "$name"
	exit 0
fi

if ! type rg > /dev/null 2>&1; then
	printf "clifm: rg: Command not found\nInstall ripgrep to use this plugin\n" >&2
	exit 127
fi

if ! type fzf > /dev/null 2>&1; then
	printf "clifm: fzf: Command not found\nInstall fzf to use this plugin\n" >&2
	exit 127
fi

# Source our plugins helper
if [ -z "$CLIFM_PLUGINS_HELPER" ] || ! [ -f "$CLIFM_PLUGINS_HELPER" ]; then
	printf "clifm: Unable to find plugins-helper file\n" >&2
	exit 1
fi
# shellcheck source=/dev/null
. "$CLIFM_PLUGINS_HELPER"

# shellcheck disable=SC2154
fzf_colors="$(get_fzf_colors)"
rg_colors="ansi"
[ "$fzf_colors" = "bw" ] && rg_colors="never"

args="$(echo "$@" | sed 's/\\//g')"

while true; do
	# shellcheck disable=SC2154
	file="$(rg --color="$rg_colors" --hidden --heading --line-number \
		--trim --ignore-case --max-depth="$MAX_DEPTH" \
		-- "$args" 2>/dev/null | \
		fzf --ansi --reverse --prompt="$fzf_prompt" \
		--height="$fzf_height" --color="$fzf_colors" \
		--no-clear --bind "right:accept" --no-unicode \
		--header="Select a file name and press Enter or Right to open it")"
	[ -z "$file" ] && break
	clifm --open "$PWD/$(printf "%s" "$file" | cut -d: -f1)"
done

# Erase the FZF window
_lines="${LINES:-100}"
printf "\033[%dM" "$_lines"

exit 0