Compare commits
8 Commits
Author | SHA1 | Date |
---|---|---|
Jack L. Frost | cd53817db7 | |
Jack L. Frost | 8f4b3af31c | |
Jack L. Frost | 27801d670f | |
Jack L. Frost | d706122511 | |
Jack L. Frost | bdfa690762 | |
Jack L. Frost | 509f11cd87 | |
Jack L. Frost | ada3aafd2c | |
Jack L. Frost | beae2098fb |
|
@ -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
|
||||||
|
|
22
sx-open
22
sx-open
|
@ -19,6 +19,8 @@ act() {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
urldecode() { : "${*//+/ }"; printf '%b\n' "${_//%/\\x}"; }
|
||||||
|
|
||||||
# cfg foo bool = [true|1]
|
# cfg foo bool = [true|1]
|
||||||
# cfg foo [string] = 'bar'
|
# cfg foo [string] = 'bar'
|
||||||
# cfg foo
|
# cfg foo
|
||||||
|
@ -108,6 +110,7 @@ handle_target() {
|
||||||
declare -n result=$1
|
declare -n result=$1
|
||||||
declare h cmd regex target_is_file target target_left cmd_is_template
|
declare h cmd regex target_is_file target target_left cmd_is_template
|
||||||
cmd_append_target=1
|
cmd_append_target=1
|
||||||
|
match=0
|
||||||
target=$2
|
target=$2
|
||||||
target_left=$target
|
target_left=$target
|
||||||
|
|
||||||
|
@ -116,9 +119,12 @@ handle_target() {
|
||||||
|
|
||||||
[[ "$target" =~ ^/.* ]] || { target="${PWD}/${target}"; } # Turn relative paths to absolute ones.
|
[[ "$target" =~ ^/.* ]] || { target="${PWD}/${target}"; } # Turn relative paths to absolute ones.
|
||||||
|
|
||||||
IFS=';' read target_mimetype charset <<< $( file -ib "$target" )
|
IFS=';' read target_mimetype _ <<< $( file -ib "$target" )
|
||||||
target_left=$target_mimetype
|
target_left=$target_mimetype
|
||||||
|
|
||||||
|
[[ $target_mimetype == 'inode/symlink' ]] && \
|
||||||
|
IFS=';' read target_mimetype_true _ <<< $( file -ibL "$target" )
|
||||||
|
|
||||||
set -- "${mime_handlers[@]}"
|
set -- "${mime_handlers[@]}"
|
||||||
elif is_uri "$target"; then
|
elif is_uri "$target"; then
|
||||||
set -- "${uri_handlers[@]}"
|
set -- "${uri_handlers[@]}"
|
||||||
|
@ -130,13 +136,22 @@ handle_target() {
|
||||||
cmd_str=$1 regex=$2
|
cmd_str=$1 regex=$2
|
||||||
cmd=()
|
cmd=()
|
||||||
|
|
||||||
|
# Fix the regex
|
||||||
|
[[ $regex =~ \^?([^\$]+)\$? ]] && regex="^${BASH_REMATCH[1]}$"
|
||||||
|
|
||||||
if [[ $cmd_str == *'%target%'* ]]; then
|
if [[ $cmd_str == *'%target%'* ]]; then
|
||||||
cmd=( ${cmd_str//%target%/$target} )
|
cmd=( ${cmd_str//%target%/$target} )
|
||||||
else
|
else
|
||||||
cmd=( $cmd_str "$target" )
|
cmd=( $cmd_str "$target" )
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "$target_left" =~ $regex ]]; then
|
if [[ $target_left =~ $regex ]]; then
|
||||||
|
match=1
|
||||||
|
elif [[ $target_mimetype_true ]]; then
|
||||||
|
[[ $target_mimetype_true =~ $regex ]] && match=1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if (( match )); then
|
||||||
act "${cmd[@]}"; result=$?
|
act "${cmd[@]}"; result=$?
|
||||||
(( result )) && return 1
|
(( result )) && return 1
|
||||||
|
|
||||||
|
@ -170,7 +185,7 @@ scheme() {
|
||||||
declare r handler=$1; shift
|
declare r handler=$1; shift
|
||||||
|
|
||||||
for s in "$@"; do
|
for s in "$@"; do
|
||||||
uri_handlers+=( "$handler" "^$s:" )
|
uri_handlers+=( "$handler" "$s:.+" )
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,6 +223,7 @@ main() {
|
||||||
done
|
done
|
||||||
|
|
||||||
target=$1; [[ "$target" ]] || { usage; exit; }
|
target=$1; [[ "$target" ]] || { usage; exit; }
|
||||||
|
# target=$(urldecode "$target") # No idea why I thought this was necessary.
|
||||||
|
|
||||||
cfg dryrun && {
|
cfg dryrun && {
|
||||||
printf 'Dry run: not actually running the handler\n' >&2
|
printf 'Dry run: not actually running the handler\n' >&2
|
||||||
|
|
11
sx-open.cfg
11
sx-open.cfg
|
@ -1,6 +1,7 @@
|
||||||
#!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.
|
||||||
|
# Regexes imply '^$'
|
||||||
|
|
||||||
# Disable desktop notifications
|
# Disable desktop notifications
|
||||||
#cfg notify false
|
#cfg notify false
|
||||||
|
@ -15,7 +16,7 @@
|
||||||
# %target% — The first argument to this script.
|
# %target% — The first argument to this script.
|
||||||
# If not found, target is appended to the end of <cmd>
|
# If not found, target is appended to the end of <cmd>
|
||||||
# example:
|
# example:
|
||||||
#uri 'browser %target% --profile=work' '^https://.+\.?workdomain.tld.*'
|
#uri 'browser %target% --profile=work' 'https://.+\.?workdomain\.tld\..*'
|
||||||
|
|
||||||
# scheme <cmd> <scheme>[ <scheme> ...]
|
# scheme <cmd> <scheme>[ <scheme> ...]
|
||||||
#scheme browser http https
|
#scheme browser http https
|
||||||
|
@ -23,10 +24,10 @@
|
||||||
|
|
||||||
# Or you can specify a regex for the entire uri:
|
# Or you can specify a regex for the entire uri:
|
||||||
# uri <cmd> <regex>[ <regex> ...]
|
# uri <cmd> <regex>[ <regex> ...]
|
||||||
#uri steam '^steam:'
|
#uri steam 'steam:.+'
|
||||||
#uri browser '^(https?|ftp):'
|
#uri browser '(https?|ftp):.+'
|
||||||
|
|
||||||
# Mime types for filesystem targets:
|
# Mime types for filesystem targets:
|
||||||
# mime <cmd> <regex>[ <regex> ...]
|
# mime <cmd> <regex>[ <regex> ...]
|
||||||
#mime sxiv '^image/'
|
#mime sxiv 'image/.+'
|
||||||
#mime libreoffice '^application/msword$'
|
#mime libreoffice 'application/msword'
|
||||||
|
|
Loading…
Reference in New Issue