#!/bin/zsh
if [[ -z "$DISPLAY" ]]; then
	echo "no DISPLAY set" >/dev/stderr
	exit 1
fi

if [[ -n "$XDG_DATA_HOME" ]]; then
    tmux_sesssion_dir=$XDG_DATA_HOME/tmux_session
else
    tmux_sesssion_dir=$HOME/.local/share/tmux_session
fi

typeset -A sessions

if [[ -f $tmux_sesssion_dir/$DISPLAY ]]; then
	while IFS=: read session workspace; do
		sessions[${session}]=$workspace
	done <$tmux_sesssion_dir/$DISPLAY
fi

wmctrl -l | while read winid workspace host title; do
	case $title in
		(*tmux::*:*)
			session=${${title##*tmux::}%%:*}
			sessions[${session}]=$workspace
			;;
	esac
done

for session in ${(ko)sessions}; do
	print -r -- "${session}:${sessions[${session}]}"
done >$tmux_sesssion_dir/$DISPLAY