15 Commits
1.1 ... 1.5.0

Author SHA1 Message Date
fbt
b10611f5ae code overhaul
Signed-off-by: fbt <fbt@fleshless.org>
2018-07-27 06:32:29 +03:00
fbt
6afb99d1ce filetype detection fix 2015-07-19 21:51:22 +03:00
fbt
0deb0b449c printf 2015-03-10 01:29:49 +03:00
fbt
597c801bdf Merge branch 'master' of builder:git/sx-open 2015-02-25 08:00:57 +03:00
fbt
0347154d6b wut 2015-02-25 08:00:47 +03:00
fbt
2470681dcb conflict 2015-02-19 14:03:49 +03:00
fbt
6cb89f0cd6 Code cleanup, thx to http://www.shellcheck.net 2015-01-17 16:38:52 +03:00
fbt
958ae3f264 associative arrays mess up sorting 2015-01-13 23:40:37 +03:00
fbt
303bdf5d71 that is also redundant 2014-11-26 16:00:03 +03:00
fbt
d8b9b5dc01 ^file:// is redundant 2014-11-26 15:59:17 +03:00
fbt
7696ea5ce8 ugh, making this work with mimes is annoying 2014-11-25 13:22:15 +03:00
fbt
214ab03647 No no no, that was ugly as hell. Files need to be handled as uris if we want to do so by name 2014-11-21 14:14:01 +03:00
fbt
97b1be6732 the hash table declarations don't need to be in the config. 2014-11-21 13:48:57 +03:00
fbt
545447c9b0 Handling files by name. Takes precedence over mimes. 2014-11-21 13:48:09 +03:00
fbt
93ec32f793 a usage function 2014-11-08 08:03:59 +03:00
3 changed files with 132 additions and 38 deletions

View File

@@ -1,10 +1,22 @@
sx-open # sx-open
=======
sx-open is an attempt to implement a saner alternative to xdg-open. 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.
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
sx-open [-dhv] <uri/file>
Flags:
-d Dry run
-v Verbose
-h Help
### Exit codes
* 1 — action failed
* 2 — file not found
* 3 — no handlers found

148
sx-open
View File

@@ -1,51 +1,137 @@
#!/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() {
cat <<- EOF
sx-open [-dhv] <uri/file>
Flags:
-d Dry run
-v Verbose
-h Help
EOF
}
act() {
(( verbose )) && printf 'CMD: %s\n' "$*" >&2
(( dry_run )) || { "$@"; return $?; }
return 0
}
# handle_uri <res> <uri>
# 1: cmd failed
# 3: no handler
handle_uri() {
declare -n result=$1
declare h cmd regex target=$2
for h in "${uri_handlers[@]}"; do
IFS='=' read cmd regex <<< "$h"
if [[ "$target" =~ ${regex} ]]; then
act ${cmd} "$target"; result=$?
(( result )) && return 1
return 0
fi
done
return 3
}
# handle_file <res> <file>
# 1: cmd failed
# 2: no such file
# 3: no handler
handle_file() {
declare -n result=$1
declare m \
target_mimetype charset \
cmd regex \
target=$2
[[ -e "$target" ]] || return 3
IFS=';' read target_mimetype charset <<< $( file -ib "$target" )
for m in "${mime_handlers[@]}"; do
IFS='=' read cmd regex <<< "$m"
if [[ "$target_mimetype" =~ ${regex} ]]; then
act ${cmd} "$target"; result=$?
(( result )) && return 1
return 0
fi
done
return 3
}
# DSL
uri() { uri_handlers+=( "$1=$2" ); }
mime() { mime_handlers+=( "$1=$2" ); }
is_uri() [[ $1 =~ ^[a-zA-Z][a-zA-Z0-9\+\.\-]+:.+ ]]
main() {
declare cmd_result target
# 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"; }
usage() { echo "usage function not implemented yet."; } while (( $# )); do
case $1 in
(-d)
printf 'Dry run: not actually running the handler\n' >&2
handle_uri() { dry_run=1
local target="$1" verbose=1
;;
for h in "${!uri_handlers[@]}"; do (-h) usage; return 0;;
[[ "$target" =~ ${uri_handlers[${h}]} ]] && {
${h} "$target" & (-v) verbose=1;;
return 0
} (--) shift; break;;
(*) break;;
esac
shift
done done
return 1 target=$1
}
handle_fs_target() {
local target="${1##*file://}"
target_mimetype=$(file -ib "$target")
for m in "${!mime_handlers[@]}"; do
[[ "$target_mimetype" =~ ${mime_handlers[${m}]} ]] && {
${m} "$target" &
return 0
}
done
return 1
}
main() {
target="$1"
[[ "$target" ]] || { usage; exit; } [[ "$target" ]] || { usage; exit; }
if [[ -e "$target" || "$target" == 'file://'* ]]; then # Treat file:// as local paths.
handle_fs_target "$target" [[ "$target" =~ ^file:(//)?(/.+) ]] && target=${BASH_REMATCH[2]}
if [[ -e "$target" ]]; then
[[ "$target" =~ ^/.* ]] || { target="${PWD}/${target}"; } # Turn relative paths to absolute ones.
handle_file cmd_result "$target"
elif is_uri "$target"; then
handle_uri cmd_result "$target"
else else
handle_uri "$target" printf 'No such file or directory: %s\n' "$target" >&2
return 2
fi fi
[[ "$?" -gt 0 ]] && { echo "No handlers found for $target"; } case $? in
(1)
printf 'Action failed with exit code: %s\n' "$cmd_result" >&2
return 4
;;
(3)
printf 'No handlers found\n' >&2
return 3
;;
esac
return 0 return 0
} }

View File

@@ -1,16 +1,12 @@
#!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
declare -A uri_handlers # Examples:
declare -A mime_handlers
uri_handlers=( #uri browser '^https?:'
["steam"]='^steam://.+' #uri steam '^steam:'
["browser"]='.+'
)
mime_handlers=( #mime sxiv '^image/'
["sxiv"]='image/.+' #mime libreoffice '^application/msword$'
)
# vim: syntax=sh