1#!/bin/sh
2
3# CliFM plugin to find/open/cd files in CWD using FZF/Rofi
4# Written by L. Abramovich
5
6SUCCESS=0
7ERROR=1
8
9if [ -n "$1" ] && { [ "$1" = "--help" ] || [ "$1" = "help" ]; }; then
10	name="$(basename "$0")"
11	printf "Find/open/cd files in the current directory using FZF/Rofi. Once found, press Enter to cd/open the desired file.\n"
12	printf "Usage: %s\n" "$name"
13	exit $SUCCESS
14fi
15
16if [ "$(which fzf)" ]; then
17	finder="fzf"
18elif [ "$(which rofi)" ]; then
19	finder="rofi"
20else
21	printf "CliFM: No finder found. Install either FZF or Rofi\n" >&2
22	exit $ERROR
23fi
24
25OS="$(uname -s)"
26
27if [ -z "$OS" ]; then
28	printf "CliFM: Unable to detect operating system\n" >&2
29	exit $ERROR
30fi
31
32case "$OS" in
33	Linux) ls_cmd="ls -A --group-directories-first --color=always" ;;
34	*) ls_cmd="ls -A" ;;
35esac
36
37if [ "$finder" = "fzf" ]; then
38	# shellcheck disable=SC2012
39	FILE="$($ls_cmd | fzf --ansi --prompt "CliFM> ")"
40else
41	# shellcheck disable=SC2012
42	FILE="$(ls -A | rofi -dmenu -p CliFM)"
43fi
44
45if [ -n "$FILE" ]; then
46	printf "%s\n" "$FILE" > "$CLIFM_BUS"
47fi
48
49exit $SUCCESS
50