summary refs log tree commit diff
path: root/bin/swx
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2020-01-29 16:09:39 +0100
committerRobert Günzler <r@gnzler.io>2020-01-29 17:55:11 +0100
commit5f2dbebf65dc5a2e528072a8eb0bac524d86257d (patch)
treeef44a5412e1397b5ddcffcb8c0dfd11d89901202 /bin/swx
parentf5d56602df70950de6b7d40966b00c9939c81763 (diff)
Replace termify with swx
Make swq match app_ids using jq's test() which allows "fuzzy" matching
Diffstat (limited to 'bin/swx')
-rwxr-xr-xbin/swx99
1 files changed, 99 insertions, 0 deletions
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