16 Commits
1.5.0 ... 1.6.1

Author SHA1 Message Date
fbt
ada3aafd2c Tiny fix: make file follow symlinks
Signed-off-by: fbt <fbt@fleshless.org>
2018-08-01 18:27:45 +03:00
fbt
beae2098fb duh
Signed-off-by: fbt <fbt@fleshless.org>
2018-07-29 10:10:08 +03:00
fbt
4d9c558264 bugfix
Signed-off-by: fbt <fbt@fleshless.org>
2018-07-27 23:49:51 +03:00
fbt
ccad66749c what are the chances you actually need literal '%template' in cmd?
Signed-off-by: fbt <fbt@fleshless.org>
2018-07-27 20:43:04 +03:00
fbt
5675a08012 exit here
Signed-off-by: fbt <fbt@fleshless.org>
2018-07-27 20:21:26 +03:00
fbt
213c8e4ce2 Configuration is serious business
Signed-off-by: fbt <fbt@fleshless.org>
2018-07-27 20:03:36 +03:00
fbt
614b2bea94 a key to disable desktop notifications
Signed-off-by: fbt <fbt@fleshless.org>
2018-07-27 18:34:22 +03:00
fbt
240d35cba3 use notify-send if available
Signed-off-by: fbt <fbt@fleshless.org>
2018-07-27 18:32:42 +03:00
fbt
2b1353ac9c wut?
Signed-off-by: fbt <fbt@fleshless.org>
2018-07-27 17:50:32 +03:00
fbt
0b3468ed49 macro support
Signed-off-by: fbt <fbt@fleshless.org>
2018-07-27 17:48:58 +03:00
fbt
a77e18d908 nify the handler functions
Signed-off-by: fbt <fbt@fleshless.org>
2018-07-27 17:35:51 +03:00
fbt
f68c9e817b This is why I stopped doing a release every few commits
Signed-off-by: fbt <fbt@fleshless.org>
2018-07-27 17:02:22 +03:00
fbt
739c430e42 even smarter DSL
Signed-off-by: fbt <fbt@fleshless.org>
2018-07-27 16:49:08 +03:00
fbt
c054744d48 Allow multiple regexes to be specified
Signed-off-by: fbt <fbt@fleshless.org>
2018-07-27 16:28:13 +03:00
fbt
8493a07fdc Read pairs, no need for a separator this way
Signed-off-by: fbt <fbt@fleshless.org>
2018-07-27 08:05:14 +03:00
fbt
7eef67695a cosmetics
Signed-off-by: fbt <fbt@fleshless.org>
2018-07-27 07:47:43 +03:00
3 changed files with 191 additions and 62 deletions

View File

@@ -4,7 +4,7 @@ sx-open is an attempt to implement a saner alternative to xdg-open.
## Installation ## Installation
Clone the repo, drop sx-open.cfg into your $HOME/.config. Clone the repo, drop sx-open.cfg into your $HOME/.config and set it up to do what you want. There are no built-in defaults, it's all in the config.
As this thing is meant to replace xdg-open, you will probably want to link sx-open into xdg-open somewhere in your $PATH as to override the default one. As this thing is meant to replace xdg-open, you will probably want to link sx-open into xdg-open somewhere in your $PATH as to override the default one.
## Usage ## Usage

215
sx-open
View File

@@ -1,84 +1,189 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# This is an attempt to replace xdg-open with something sane. # This is an attempt to replace xdg-open with something sane.
usage() { printf '%s [-dhv] <uri/file>\n' "${0##*/}"; }
usage() { usage() {
cat <<- EOF cat <<- EOF
sx-open [-dhv] <uri/file> sx-open [-dhv] <uri/file>
Flags: Flags:
-d Dry run -d Dry run
-v Verbose -v Verbose mode
-n Disable desktop notifications
-h Help -h Help
EOF EOF
} }
act() { act() {
(( verbose )) && printf 'CMD: %s\n' "$*" >&2 cfg verbose && printf 'CMD: %s\n' "$*" >&2
(( dry_run )) || { "$@"; return $?; } cfg dryrun || { "$@"; return $?; }
return 0 return 0
} }
# handle_uri <res> <uri> # cfg foo bool = [true|1]
# 1: cmd failed # cfg foo [string] = 'bar'
# 3: no handler # cfg foo
handle_uri() { cfg() {
declare -n result=$1 declare s_name=$1; shift
declare h cmd regex target=$2 declare -n \
_s="cfg[$s_name]" \
_s_type="cfg[${s_name}_type]"
for h in "${uri_handlers[@]}"; do if (( $# )); then
IFS='=' read cmd regex <<< "$h" while (( $# )); do
case $1 in
(bool|str) _s_type="$1";;
if [[ "$target" =~ ${regex} ]]; then (true|false|0|1)
act ${cmd} "$target"; result=$? _s_type='bool'
(( result )) && return 1 _s="$1"
return 0 break
fi ;;
(=)
[[ $_s_type ]] || _s_type='string'
[[ $_s_type == 'bool' ]] && {
case $2 in
(true|false|0|1) true;;
(*)
error "On line $LINENO, in $FUNCNAME: Invalid value for '$s_name' type 'bool': '$2'"
exit 111
;;
esac
}
_s="$2"
break
;;
(*)
error "On line $LINENO, in $FUNCNAME: Syntax error: “cfg $s_name $*”"
exit 115
;;
esac
shift
done done
else
return 3 [[ -n $_s ]] || {
error "On line $LINENO, in $FUNCNAME: invalid option: '$s_name'"
exit 113
} }
# handle_file <res> <file> case $_s_type in
(bool)
case $_s in
(true|1) return 0;;
(false|0) return 1;;
esac
;;
(*)
printf '%s\n' "$_s"
return 0
;;
esac
fi
}
notify() {
cfg notify || return 7 # disabled
[[ $DISPLAY ]] || return 3
[[ $notifier ]] || return 5
"${notifier[@]}" 'sx-open' "$@"
}
error() {
printf '%s\n' "$*" >&2
notify "$*"
}
# handle_target <res> <uri>
# 1: cmd failed # 1: cmd failed
# 2: no such file
# 3: no handler # 3: no handler
handle_file() { handle_target() {
declare -n result=$1 declare -n result=$1
declare m \ declare h cmd regex target_is_file target target_left cmd_is_template
target_mimetype charset \ cmd_append_target=1
cmd regex \
target=$2 target=$2
target_left=$target
[[ -e "$target" ]] || return 3 if [[ -e "$target" ]]; then
target_is_file=1
IFS=';' read target_mimetype charset <<< $( file -ib "$target" ) [[ "$target" =~ ^/.* ]] || { target="${PWD}/${target}"; } # Turn relative paths to absolute ones.
for m in "${mime_handlers[@]}"; do IFS=';' read target_mimetype charset <<< $( file -ibL "$target" )
IFS='=' read cmd regex <<< "$m" target_left=$target_mimetype
if [[ "$target_mimetype" =~ ${regex} ]]; then set -- "${mime_handlers[@]}"
act ${cmd} "$target"; result=$? elif is_uri "$target"; then
set -- "${uri_handlers[@]}"
else
return 2
fi
while (( $# )); do
cmd_str=$1 regex=$2
cmd=()
if [[ $cmd_str == *'%target%'* ]]; then
cmd=( ${cmd_str//%target%/$target} )
else
cmd=( $cmd_str "$target" )
fi
if [[ "$target_left" =~ $regex ]]; then
act "${cmd[@]}"; result=$?
(( result )) && return 1 (( result )) && return 1
return 0 return 0
fi fi
shift 2
done done
return 3 return 3
} }
# DSL # DSL
uri() { uri_handlers+=( "$1=$2" ); } uri() {
mime() { mime_handlers+=( "$1=$2" ); } declare r handler=$1; shift
for r in "$@"; do
uri_handlers+=( "$handler" "$r" )
done
}
mime() {
declare r handler=$1; shift
for r in "$@"; do
mime_handlers+=( "$handler" "$r" )
done
}
scheme() {
declare r handler=$1; shift
for s in "$@"; do
uri_handlers+=( "$handler" "^$s:" )
done
}
is_uri() [[ $1 =~ ^[a-zA-Z][a-zA-Z0-9\+\.\-]+:.+ ]] is_uri() [[ $1 =~ ^[a-zA-Z][a-zA-Z0-9\+\.\-]+:.+ ]]
main() { main() {
declare cmd_result target declare cmd_result target
declare -gA cfg
cfg verbose bool = false
cfg dryrun bool = false
cfg notify bool = true
# Source the config file. # Source the config file.
cfg_file="$HOME/.config/sx-open.cfg" cfg_file="$HOME/.config/sx-open.cfg"
[[ -f "$cfg_file" ]] && { source "$cfg_file"; } [[ -f "$cfg_file" ]] && { source "$cfg_file"; }
@@ -86,15 +191,14 @@ main() {
while (( $# )); do while (( $# )); do
case $1 in case $1 in
(-d) (-d)
printf 'Dry run: not actually running the handler\n' >&2 cfg dryrun = true
cfg verbose = true
dry_run=1
verbose=1
;; ;;
(-h) usage; return 0;; (-v) cfg verbose = true;;
(-n) cfg notify = false;; # disable desktop notifications
(-v) verbose=1;; (-h) usage; return 0;;
(--) shift; break;; (--) shift; break;;
(*) break;; (*) break;;
@@ -103,32 +207,37 @@ main() {
shift shift
done done
target=$1 target=$1; [[ "$target" ]] || { usage; exit; }
[[ "$target" ]] || { usage; exit; } cfg dryrun && {
printf 'Dry run: not actually running the handler\n' >&2
cfg verbose true
}
# Treat file:// as local paths. # Treat file:// as local paths.
[[ "$target" =~ ^file:(//)?(/.+) ]] && target=${BASH_REMATCH[2]} [[ "$target" =~ ^file:(//)?(/.+) ]] && target=${BASH_REMATCH[2]}
if [[ -e "$target" ]]; then # Figure out if we're in X and set $notifier if we are.
[[ "$target" =~ ^/.* ]] || { target="${PWD}/${target}"; } # Turn relative paths to absolute ones. if [[ $DISPLAY ]]; then
# Do we have notify-send?
handle_file cmd_result "$target" notifier=$(type -P 'notify-send')
elif is_uri "$target"; then
handle_uri cmd_result "$target"
else
printf 'No such file or directory: %s\n' "$target" >&2
return 2
fi fi
handle_target cmd_result "$target"
case $? in case $? in
(1) (1)
printf 'Action failed with exit code: %s\n' "$cmd_result" >&2 error "Action on “$target” failed with exit code: $cmd_result"
return 4 return 4
;; ;;
(2)
error "No such file or directory: “$target”"
return 2
;;
(3) (3)
printf 'No handlers found\n' >&2 error "No handlers found for “$target”"
return 3 return 3
;; ;;
esac esac

View File

@@ -1,12 +1,32 @@
#!syntax bash #!syntax bash
# Configuration file for sx-open # Configuration file for sx-open
# Note that as sx-open checks the regexes in order, they should be placed in order from specific to less so. # Note that as sx-open checks the regexes in order, they should be placed in order from specific to less so.
# Format: type cmd regex
# Examples: # Disable desktop notifications
#cfg notify false
#uri browser '^https?:' # Enable verbose mode
#cfg verbose true
# Enable dry run mode (implies verbose)
#cfg dryrun true
# <cmd> macros:
# %target% — The first argument to this script.
# If not found, target is appended to the end of <cmd>
# example:
#uri 'browser %target% --profile=work' '^https://.+\.?workdomain.tld.*'
# scheme <cmd> <scheme>[ <scheme> ...]
#scheme browser http https
#scheme steam steam
# Or you can specify a regex for the entire uri:
# uri <cmd> <regex>[ <regex> ...]
#uri steam '^steam:' #uri steam '^steam:'
#uri browser '^(https?|ftp):'
# Mime types for filesystem targets:
# mime <cmd> <regex>[ <regex> ...]
#mime sxiv '^image/' #mime sxiv '^image/'
#mime libreoffice '^application/msword$' #mime libreoffice '^application/msword$'