2015-01-28 06:49:52 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2016-01-17 20:29:04 +00:00
|
|
|
# Various options for the file browser script:
|
|
|
|
ROFI_FB_GENERIC_FO="xdg-open" # command used for opening the selection
|
|
|
|
ROFI_FB_PREV_LOC_FILE=~/.local/share/rofi/rofi_fb_prevloc
|
2021-08-14 11:03:16 +00:00
|
|
|
ROFI_FB_HISTORY_FILE=~/.local/share/rofi/rofi_fb_history
|
2016-01-17 20:29:04 +00:00
|
|
|
ROFI_FB_HISTORY_MAXCOUNT=5 # maximum number of history entries
|
|
|
|
# Comment the next variable to always start in the last visited directory,
|
|
|
|
# otherwise rofi_fb will start in the specified directory:
|
|
|
|
ROFI_FB_START_DIR=$HOME # starting directory
|
|
|
|
# Uncomment the following line to disable history:
|
|
|
|
# ROFI_FB_NO_HISTORY=1
|
2015-01-28 06:49:52 +00:00
|
|
|
|
2016-01-17 20:29:04 +00:00
|
|
|
# Beginning of the script:
|
|
|
|
# Create the directory for the files of the script
|
2021-08-14 11:03:16 +00:00
|
|
|
if [ ! -d "$(dirname "${ROFI_FB_PREV_LOC_FILE}")" ]
|
2016-01-17 20:29:04 +00:00
|
|
|
then
|
|
|
|
mkdir -p "$(dirname "${ROFI_FB_PREV_LOC_FILE}")"
|
|
|
|
fi
|
2021-08-14 11:03:16 +00:00
|
|
|
if [ ! -d "$(dirname "${ROFI_FB_HISTORY_FILE}")" ]
|
2016-01-17 20:29:04 +00:00
|
|
|
then
|
|
|
|
mkdir -p "$(dirname "${ROFI_FB_HISTORY_FILE}")"
|
|
|
|
fi
|
2015-01-28 06:49:52 +00:00
|
|
|
|
2016-01-17 20:29:04 +00:00
|
|
|
# Initialize $ROFI_FB_CUR_DIR
|
|
|
|
if [ -d "${ROFI_FB_START_DIR}" ]
|
2015-01-28 06:49:52 +00:00
|
|
|
then
|
2016-01-17 20:29:04 +00:00
|
|
|
ROFI_FB_CUR_DIR="${ROFI_FB_START_DIR}"
|
|
|
|
else
|
|
|
|
ROFI_FB_CUR_DIR="$PWD"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Read last location, otherwise we default to $ROFI_FB_START_DIR or $PWD.
|
|
|
|
if [ -f "${ROFI_FB_PREV_LOC_FILE}" ]
|
|
|
|
then
|
2021-08-14 11:03:16 +00:00
|
|
|
ROFI_FB_CUR_DIR=$(cat "${ROFI_FB_PREV_LOC_FILE}")
|
|
|
|
fi
|
2015-01-28 06:49:52 +00:00
|
|
|
|
|
|
|
# Handle argument.
|
|
|
|
if [ -n "$@" ]
|
|
|
|
then
|
2016-01-17 20:29:04 +00:00
|
|
|
if [[ "$@" == /* ]]
|
|
|
|
then
|
|
|
|
ROFI_FB_CUR_DIR="$@"
|
|
|
|
else
|
|
|
|
ROFI_FB_CUR_DIR="${ROFI_FB_CUR_DIR}/$@"
|
|
|
|
fi
|
2015-01-28 06:49:52 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# If argument is no directory.
|
2016-01-17 20:29:04 +00:00
|
|
|
if [ ! -d "${ROFI_FB_CUR_DIR}" ]
|
2015-01-28 06:49:52 +00:00
|
|
|
then
|
2016-01-17 20:29:04 +00:00
|
|
|
if [ -x "${ROFI_FB_CUR_DIR}" ]
|
2015-01-28 06:49:52 +00:00
|
|
|
then
|
2021-08-14 11:03:16 +00:00
|
|
|
coproc ( "${ROFI_FB_CUR_DIR}" >/dev/null 2>&1 )
|
2015-01-28 06:49:52 +00:00
|
|
|
exec 1>&-
|
|
|
|
exit;
|
2016-01-17 20:29:04 +00:00
|
|
|
elif [ -f "${ROFI_FB_CUR_DIR}" ]
|
2015-01-28 06:49:52 +00:00
|
|
|
then
|
2016-01-17 20:29:04 +00:00
|
|
|
if [[ "${ROFI_FB_NO_HISTORY}" -ne 1 ]]
|
|
|
|
then
|
|
|
|
# Append selected entry to history and remove exceeding entries
|
|
|
|
sed -i "s|${ROFI_FB_CUR_DIR}|##deleted##|g" "${ROFI_FB_HISTORY_FILE}"
|
|
|
|
sed -i '/##deleted##/d' "${ROFI_FB_HISTORY_FILE}"
|
|
|
|
echo "${ROFI_FB_CUR_DIR}" >> "${ROFI_FB_HISTORY_FILE}"
|
2021-08-14 11:03:16 +00:00
|
|
|
if [ "$( wc -l < "${ROFI_FB_HISTORY_FILE}" )" -gt "${ROFI_FB_HISTORY_MAXCOUNT}" ]
|
2016-01-17 20:29:04 +00:00
|
|
|
then
|
|
|
|
sed -i 1d "${ROFI_FB_HISTORY_FILE}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
# Open the selected entry with $ROFI_FB_GENERIC_FO
|
2021-08-14 11:03:16 +00:00
|
|
|
coproc ( "${ROFI_FB_GENERIC_FO}" "${ROFI_FB_CUR_DIR}" >/dev/null 2>&1 )
|
2016-01-17 20:29:04 +00:00
|
|
|
if [ -d "${ROFI_FB_START_DIR}" ]
|
|
|
|
then
|
|
|
|
echo "${ROFI_FB_START_DIR}" > "${ROFI_FB_PREV_LOC_FILE}"
|
|
|
|
fi
|
2015-01-28 06:49:52 +00:00
|
|
|
exit;
|
|
|
|
fi
|
|
|
|
exit;
|
|
|
|
fi
|
|
|
|
|
2016-01-17 20:29:04 +00:00
|
|
|
# Process current dir.
|
|
|
|
if [ -n "${ROFI_FB_CUR_DIR}" ]
|
2015-01-28 06:49:52 +00:00
|
|
|
then
|
2016-01-17 20:29:04 +00:00
|
|
|
ROFI_FB_CUR_DIR=$(readlink -e "${ROFI_FB_CUR_DIR}")
|
|
|
|
echo "${ROFI_FB_CUR_DIR}" > "${ROFI_FB_PREV_LOC_FILE}"
|
|
|
|
pushd "${ROFI_FB_CUR_DIR}" >/dev/null
|
2015-01-28 06:49:52 +00:00
|
|
|
fi
|
|
|
|
|
2016-01-17 20:29:04 +00:00
|
|
|
# Output to rofi
|
|
|
|
if [[ "${ROFI_FB_NO_HISTORY}" -ne 1 ]]
|
|
|
|
then
|
|
|
|
tac "${ROFI_FB_HISTORY_FILE}" | grep "${ROFI_FB_CUR_DIR}"
|
|
|
|
fi
|
2015-01-28 06:49:52 +00:00
|
|
|
echo ".."
|
|
|
|
ls
|
2016-01-17 20:29:04 +00:00
|
|
|
# vim:sw=4:ts=4:et:
|