#!/bin/bash

# basic scratchpad functionality for dk
# spawns a set command with a known title if not already open
# if open toggle between the current workspace and the last
# written by Nathaniel Maia - 2021

# example rule for the below script to be placed in your dkrc
#    dkcmd rule class="^scratchpad$" float=true

# Dinamically get both class and command of the needed scratchpad
title="$1"
shift
cmd="$@"

# window ID, we need to printf it as 8 hex digits to later match with dk status
win=$(printf '0x%08x' "$(xwininfo -root -children | awk '/'"$title"'/ {print $1}')")
stat=$(dkcmd status num=1 type=full)

if (( win != 0 )); then
	# window is already open so toggle it
	ws=$(awk '/^workspaces:/ { for (i = 1; i <= NF; i++) { if ($i ~ "*") print i - 1 } }' <<< "$stat")
	wins=$(sed -n '/^windows:/,/^$/p' <<< "$stat")
	win_ws=$(grep "^\s*${win}" <<< "$wins" | awk -F'" ' '{print $4}' | cut -d' ' -f1)

	if (( win_ws == ws )); then
		# hide it
		# we could create a new workspace and place it there instead to not mess with the users existing workspaces
		dkcmd ws send "$win" "$(awk '/numws/{print $2}' <<< "$stat")"
	else
		# show it
		dkcmd ws send "$win" "$ws"
	fi
else
	# the window is not yet spawned so do so
	${cmd} &>/dev/null & disown
fi