File: fzfsel.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 (122 lines) | stat: -rwxr-xr-x 2,960 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
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
#!/bin/sh

# Files selection plugin for Clifm
# Dependencies: fzf, find

# Author: L. Abramovich
# License: GPL2+

if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
	name="${CLIFM_PLUGIN_NAME:-$(basename "$0")}"
	printf "List files in the current directory allowing \
the user to select one or more of them.\n"
	printf "\n\x1b[1mUSAGE\x1b[0m\n  %s [-f, --flat] [\PATTERN] [-h, --help]

With the -f or --flat option files are listed recursively \
starting from the current directory (aka flat or branch view).

\PATTERN is a glob expression used to filter files. Ex: '-f \*.pdf' \
will recursively list all .pdf files in the current directory.

At exit, selected files are sent to Clifm's Selection Box.

Dependencies: fzf(1), find(1)\n" "$name"
	exit 0
fi

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

TMP_DIR="${TMPDIR:-/tmp}/clifm/$CLIFM_PROFILE"
TMPFILE="$TMP_DIR/${CLIFM_PROFILE}.fzfsel"

# 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"

! [ -d "$TMP_DIR" ] && mkdir -p "$TMP_DIR"

HELP="Usage:

Alt-h: Toggle this help screen

TAB, Alt-down: Toggle select down

Alt-up: Toggle select up

Ctrl-s: Select all files

Ctrl-d: Deselect all files

Ctrl-t: Invert selection

Enter: Confirm selection, exit, and send selected files to Clifm

Esc: Cancel and exit"

exit_status=0
pattern=""
flat_view=0

if [ "$1" = "-f" ] || [ "$1" = "--flat" ]; then
	flat_view=1
	shift
fi

if [ -n "$1" ]; then
	if [ "$(printf "%c" "$1")" = "\\" ]; then
		pattern="$(echo "$1" | cut -c2-)"
	else
		pattern="$1"
	fi
fi

if [ "$flat_view" -eq 1 ]; then
	if [ -n "$pattern" ]; then
		ls_cmd="find . -name \"$pattern\" 2>/dev/null | cut -c3-"
	else
		ls_cmd="find . 2>/dev/null | cut -c3-"
	fi
else
	if ls --version >/dev/null 2>&1; then
		ls_cmd="ls -Ap --group-directories-first --color=always $pattern"
	else
		ls_cmd="ls -Ap $pattern"
	fi
fi

# shellcheck disable=SC2012
# shellcheck disable=SC2154
eval "$ls_cmd" | fzf --multi --marker='*' --info=inline --keep-right \
	--height "$fzf_height" --header "Select files. Press Alt-h for help" \
	--color "$(get_fzf_colors)" \
	--bind "alt-down:toggle+down,insert:toggle+down" \
	--bind "alt-up:toggle+up" \
	--bind "alt-h:toggle-preview" \
	--bind "ctrl-s:select-all,ctrl-d:deselect-all,ctrl-t:toggle-all" \
	--preview-window=:wrap \
	--bind "alt-enter:toggle-all" --reverse "$(fzf_borders)" \
	--preview "printf %s \"$HELP\"" \
	--no-sort --ansi --prompt "$fzf_prompt" > "$TMPFILE"
exit_status=$?

[ "$exit_status" -eq 130 ] && exit_status=0

# shellcheck disable=SC1007
while ISF= read -r line; do
	printf "%s\n" "$PWD/$line" >> "$CLIFM_SELFILE"
done < "$TMPFILE"

rm -f -- "$TMPFILE" > /dev/null 2>&1

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

exit "$exit_status"