#!/bin/sh

swaymsg -t get_tree | jq -r '
	# descend to workspace or scratchpad
	.nodes[].nodes[]
	# save workspace name as .w
	| {"w": .name} + (
		if .nodes then # workspace
			[recurse(.nodes[])]
		else # scratchpad
			[]
		end
		+ .floating_nodes
		| .[]
		# select nodes with no children (windows)
		| select(.nodes==[])
	)
	| ((.id | tostring) + "  "
	# remove markup and index from workspace name, replace scratch with "[S]"
	+ (.w | gsub("^[^:]*:|<[^>]*>"; "") | sub("__i3_scratch"; "[S]"))
	+ "  " +  .name)
	' | grep -v "__i3_scratch" | {{ launcher }} {{ dmenu_flag }} -p 'Window{{ (launcher == 'fuzzel') | ternary(': ', '') }}' | {
	read -r id name
	swaymsg "[con_id=$id]" focus
}
