diff options
| -rwxr-xr-x | bin/swq | 19 | ||||
| -rwxr-xr-x | bin/swx | 99 | ||||
| -rwxr-xr-x | bin/termify | 10 |
3 files changed, 113 insertions, 15 deletions
diff --git a/bin/swq b/bin/swq index 0336f64..a742d08 100755 --- a/bin/swq +++ b/bin/swq @@ -7,7 +7,7 @@ swtree() has_appid() { - swtree | jq "[ .. | select(.app_id?==\"$1\") // select(.window_properties?.class==\"$1\") // select(.window_properties?.instance==\"$1\") ]" + swtree | jq "[ .. | (select(.app_id?) | select(.app_id | test(\"$1\"))) // (select(.window_properties?.class?) | select(.window_properties.class | test(\"$1\"))) ]" } has_pid() @@ -82,16 +82,23 @@ _has() *) printf 'usage: swq has COMMAND <args>\n' printf '\nCOMMANDS:\n' - printf ' appid: returns the json of the window filtered by app_id/class\n' + printf ' appid: returns the json of the window filtered by app_id/class (fuzzy matching)\n' printf ' pid: returns the json of the window filtered by pid\n' printf ' exe: returns the json of the window filtered by process name\n' printf '\n' + exit 2 ;; esac } _latest() { - [ "$1" == "ws" ] && 1="workspace" - latest "$1" + case "$1" in + workspace|ws) latest workspace ;; + container|con) latest con ;; + output) latest output ;; + *) + printf 'usage: swq latest <con|workspace|output>\n\n' + exit 2 ;; + esac } _list() { @@ -109,7 +116,8 @@ _current() workspace) swtree | jq '.. | select(.type?=="workspace") | . as $parent | .nodes[]? | if .focused then $parent else empty end' ;; output) $0 current workspace | jq -r '.output' ;; *) - printf 'usage: swq current <con|workspace|output>\n\n' ;; + printf 'usage: swq current <con|workspace|output>\n\n' + exit 2 ;; esac } @@ -136,4 +144,5 @@ case "$1" in printf ' focus-latest-con\n' printf ' moveto <appid> <ws>: move container with <appid> to <ws>\n' printf '\n' + exit 2 ;; esac diff --git a/bin/swx b/bin/swx new file mode 100755 index 0000000..4d66051 --- /dev/null +++ b/bin/swx @@ -0,0 +1,99 @@ +#!/bin/sh +set -e + +border= +wspace= +output= +geometry= +termify= +appid_override= +while getopts b:w:o:g:I:th name; do + case "$name" in + b) # border + border="${OPTARG}" ;; + w) # move to workspace + wspace="${OPTARG}" ;; + o) # move to output + output="${OPTARG}" ;; + g) # geometry + geometry="${OPTARG}" ;; + t) # termify + termify=1 ;; + I) # override app_id for non-terminal applications + appid_override="${OPTARG}" ;; + h|?) printf "usage: %s [-bwog] COMMAND\n" "$(basename "$0")" + printf "\n" + printf " -t \t\t(run COMMAND in \$TERM)\n" + printf " -b BORDER \t(set border property)\n" + printf " -w WORKSPACE \t(move to workspace)\n" + printf " -o OUTPUT \t(move to output)\n" + printf " -g GEOMETRY \t(resize/position window)\n" + printf " -I APP_ID \t(override app_id used to detect window, only makes sense with -t)\n" + printf "\n" + printf "For this to work swq(1) is required.\n" + printf "See sway(5) for possible options for BORDER, WORKSPACE, OUTPUT and GEOMETRY.\n" + exit 2 ;; + esac +done +shift $((OPTIND - 1)) + +arg0=$(basename "$1") +appid=${arg0} +if [ -z "${arg0}" ]; then + exec "$0" -h +fi +shift + +if [ -n "${termify}" ]; then + case ${TERM} in + alacritty) args="--title=${arg0} --class=${arg0} --command" ;; + *) args="" ;; + esac + command="${TERM} ${args} ${arg0} $*" +else + [ -n "${appid_override}" ] && appid=${appid_override} + command="${arg0} $*" +fi + +sway exec "${command}" >/dev/null + +sway_props_done= +waiting=0 +while [ -z "${sway_props_done}" ]; do + if ! swq has appid "${appid}" | jq -e 'length>0'; then + waiting=$((waiting+1)) + if [ "${waiting}" -gt 20 ]; then + printf "Failed to run %s.\n" "${arg0}" + if [ -z "${termify}" ]; then + printf "If you do see the application try running:\n" + printf "%s -I %s %s\n" \ + "$(basename "$0")" \ + "$(swq current con | jq -r '.app_id')" \ + "${arg0} $*" + fi + exit 1 + fi >&2 + continue + fi + if [ -n "${border}" ]; then + sway border "${border}" + fi + if [ -n "${wspace}" ]; then + sway move workspace "${wspace}" + fi + if [ -n "${output}" ]; then + sway move output "${wspace}" + fi + if [ -n "${geometry}" ]; then + sway floating enable + wh=$(printf "%s" "${geometry}" | cut -d' ' -f2) + sway resize set \ + width "$(printf "%s" "${wh}" | cut -d'x' -f1)" px \ + height "$(printf "%s" "${wh}" | cut -d'x' -f2)" px + xy=$(printf "%s" "${geometry}" | cut -d' ' -f1) + sway move absolute position \ + "$(printf "%s" "${xy}" | cut -d',' -f1)" px \ + "$(printf "%s" "${xy}" | cut -d',' -f2)" px + fi + sway_props_done=1 +done >/dev/null diff --git a/bin/termify b/bin/termify deleted file mode 100755 index 4778470..0000000 --- a/bin/termify +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -arg0=$(basename $1 || echo $1) - -case "$TERM" in - alacritty) args="--title='$arg0' --class='$arg0' --command" ;; - *) args="" ;; -esac - -exec $TERM $args $@ & |