2021-05-13 10:33:04 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
2021-05-15 17:32:01 +00:00
|
|
|
# Description: Sets the xdg-open's default application for the current entry's file
|
|
|
|
# type. ${XDG_DATA_DIRS} and ${XDG_DATA_HOME} are set to the recommended
|
|
|
|
# defaults if unset, as specified in XDG Base Directory Specification
|
|
|
|
# - http://specifications.freedesktop.org/basedir-spec/.
|
2021-05-13 10:33:04 +00:00
|
|
|
#
|
2021-07-23 11:36:19 +00:00
|
|
|
# Dependencies: xdg-utils, fzf or dmenu (GUI)
|
2021-05-13 10:33:04 +00:00
|
|
|
#
|
2021-05-15 17:32:01 +00:00
|
|
|
# Shell: POSIX compliant
|
2021-05-13 10:33:04 +00:00
|
|
|
# Author: lwnctd
|
|
|
|
|
2021-07-23 11:36:19 +00:00
|
|
|
# set to 1 to enable GUI apps
|
|
|
|
GUI="${GUI:-0}"
|
|
|
|
|
2021-07-28 06:30:30 +00:00
|
|
|
if [ "$GUI" -ne 0 ] && command -v dmenu > /dev/null 2>& 1; then
|
|
|
|
menu="dmenu -i -l 7"
|
2021-07-23 11:36:19 +00:00
|
|
|
elif command -v fzf > /dev/null 2>& 1; then
|
2021-07-28 06:30:30 +00:00
|
|
|
menu="fzf -e --tiebreak=begin"
|
2021-07-23 11:36:19 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$1" ] || [ -z "$menu" ] > /dev/null 2>& 1; then
|
2021-05-13 10:33:04 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
ftype=$(xdg-mime query filetype "$2/$1")
|
|
|
|
|
|
|
|
if [ -z "$ftype" ]; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
dirs=${XDG_DATA_DIRS:-/usr/local/share:/usr/share}
|
|
|
|
dirs=${dirs}:${XDG_DATA_HOME:-$HOME/.local/share}:
|
|
|
|
|
|
|
|
while [ -n "$dirs" ]; do
|
|
|
|
d=${dirs%%:*}
|
|
|
|
if [ -n "$d" ] && [ -d "$d"/applications ]; then
|
|
|
|
set -- "$@" "$d"/applications
|
|
|
|
fi
|
|
|
|
dirs=${dirs#*:}
|
|
|
|
done
|
|
|
|
|
|
|
|
app=$(find "$@" -iname '*.desktop' -exec grep '^Name=' {} + \
|
|
|
|
| sort -u -t ':' -k 1,1 \
|
|
|
|
| sed -E 's;.+/(.+desktop):Name=(.+);\2:\1;' \
|
|
|
|
| sort -t ':' -k 1,1 \
|
|
|
|
| column -t -s ':' -o "$(printf '\t')" \
|
2021-07-23 11:36:19 +00:00
|
|
|
| $menu \
|
2021-05-13 10:33:04 +00:00
|
|
|
| cut -f 2)
|
|
|
|
|
|
|
|
if [ -n "$app" ]; then
|
|
|
|
xdg-mime default "$app" "$ftype"
|
|
|
|
fi
|