qutebrowser: Fix open downloads script for spaces

Fixed the 'open downloads' userscript to work correctly for files with
spaces in them.
This commit is contained in:
Marty Oehme 2022-01-17 09:45:44 +01:00
parent 08a46ed691
commit 701c5bbcfc
Signed by: Marty
GPG key ID: B7538B8F50A1C800

View file

@ -16,7 +16,7 @@
# see the recent downloads, just press "sd". # see the recent downloads, just press "sd".
# #
# Thorsten Wißmann, 2015 (thorsten` on Libera Chat) # Thorsten Wißmann, 2015 (thorsten` on Libera Chat)
# Refactored to work with bemenu by Marty Oehme, 2021 (@martyo@matrix.org on Matrix) # Marty Oehme, 2021 (@martyo@matrix.org on Matrix), refactored to work with bemenu
# Any feedback is welcome! # Any feedback is welcome!
set -e set -e
@ -25,10 +25,10 @@ set -e
DOWNLOAD_DIR=${DOWNLOAD_DIR:-${QUTE_DOWNLOAD_DIR:-$HOME/downloads}} DOWNLOAD_DIR=${DOWNLOAD_DIR:-${QUTE_DOWNLOAD_DIR:-$HOME/downloads}}
# the name of the rofi-like command # the name of the rofi-like command
if [ -n "$ROFI_CMD" ]; then if [ -n "$ROFI_CMD" ]; then
: :
elif command -v rofi >/dev/null 2>&1; then elif command -v rofi >/dev/null 2>&1; then
ROFI_CMD="rofi" ROFI_CMD="rofi"
ROFI_ARGS=${ROFI_ARGS:-( ROFI_ARGS=${ROFI_ARGS:-(
-monitor -2 # place above window -monitor -2 # place above window
-location 6 # aligned at the bottom -location 6 # aligned at the bottom
-width 100 # use full window width -width 100 # use full window width
@ -39,48 +39,48 @@ elif command -v rofi >/dev/null 2>&1; then
-p 'Open download:' -dmenu -p 'Open download:' -dmenu
)} )}
elif command -v bemenu >/dev/null 2>&1; then elif command -v bemenu >/dev/null 2>&1; then
ROFI_CMD="bemenu" ROFI_CMD="bemenu"
ROFI_ARGS="${ROFI_ARGS:--il 10}" ROFI_ARGS="${ROFI_ARGS:--il 10}"
fi fi
msg() { msg() {
local cmd="$1" local cmd="$1"
shift shift
local msg="$*" local msg="$*"
if [ -z "$QUTE_FIFO" ]; then if [ -z "$QUTE_FIFO" ]; then
echo "$cmd: $msg" >&2 echo "$cmd: $msg" >&2
else else
echo "message-$cmd '${msg//\'/\\\'}'" >>"$QUTE_FIFO" echo "message-$cmd '${msg//\'/\\\'}'" >>"$QUTE_FIFO"
fi fi
} }
die() { die() {
msg error "$*" msg error "$*"
if [ -n "$QUTE_FIFO" ]; then if [ -n "$QUTE_FIFO" ]; then
# when run as a userscript, the above error message already informs the # when run as a userscript, the above error message already informs the
# user about the failure, and no additional "userscript exited with status # user about the failure, and no additional "userscript exited with status
# 1" is needed. # 1" is needed.
exit 0 exit 0
else else
exit 1 exit 1
fi fi
} }
if ! [ -d "$DOWNLOAD_DIR" ]; then if ! [ -d "$DOWNLOAD_DIR" ]; then
die "Download directory »$DOWNLOAD_DIR« not found!" die "Download directory »$DOWNLOAD_DIR« not found!"
fi fi
if ! command -v "${ROFI_CMD}" >/dev/null; then if ! command -v "${ROFI_CMD}" >/dev/null; then
die "Rofi command »${ROFI_CMD}« not found in PATH!" die "Rofi command »${ROFI_CMD}« not found in PATH!"
fi fi
crop-first-column() { crop-first-column() {
cut -d' ' -f2 cut -d' ' -f2-
} }
ls-files() { ls-files() {
# add the slash at the end of the download dir enforces to follow the # add the slash at the end of the download dir enforces to follow the
# symlink, if the DOWNLOAD_DIR itself is a symlink # symlink, if the DOWNLOAD_DIR itself is a symlink
# sort by newest # sort by newest
find "${DOWNLOAD_DIR}/" -maxdepth 1 -type f -printf "%T+ %f\n" | sort -r find "${DOWNLOAD_DIR}/" -maxdepth 1 -type f -printf "%T+ %f\n" | sort -r
} }
mapfile -t entries < <(ls-files) mapfile -t entries < <(ls-files)
@ -88,14 +88,14 @@ mapfile -t entries < <(ls-files)
# we need to manually check that there are items, because rofi doesn't show up # we need to manually check that there are items, because rofi doesn't show up
# if there are no items and -no-custom is passed to rofi. # if there are no items and -no-custom is passed to rofi.
if [ "${#entries[@]}" -eq 0 ]; then if [ "${#entries[@]}" -eq 0 ]; then
die "Download directory »${DOWNLOAD_DIR}« empty" die "Download directory »${DOWNLOAD_DIR}« empty"
fi fi
line=$(printf '%s\n' "${entries[@]}" | line=$(printf '%s\n' "${entries[@]}" \
crop-first-column | | crop-first-column \
$ROFI_CMD "${ROFI_ARGS[@]}") || true | $ROFI_CMD "${ROFI_ARGS[@]}") || true
if [ -z "$line" ]; then if [ -z "$line" ]; then
exit 0 exit 0
fi fi
msg info "file is $line" msg info "file is $line"
@ -104,7 +104,7 @@ filetype=$(xdg-mime query filetype "$path")
application=$(xdg-mime query default "$filetype") application=$(xdg-mime query default "$filetype")
if [ -z "$application" ]; then if [ -z "$application" ]; then
die "Do not know how to open »$line« of type $filetype" die "Do not know how to open »$line« of type $filetype"
fi fi
msg info "Opening »$line« (of type $filetype) with ${application%.desktop}" msg info "Opening »$line« (of type $filetype) with ${application%.desktop}"