#!/bin/zsh
if ! zmodload zsh/zselect; then
	shift
	exec "$@"
fi

if (($+commands[tailf])); then
	coproc tailf -0 $1
else
	coproc tail -F -n 0 $1
fi
shift
"$@" | {
	exec 3<& p
	while true; do
		unset FDS
		if zselect -A FDS -r 0 3; then
			if ((${+FDS[3]})); then
				if IFS= read -t 1 -r -u 3 line; then
					printf >&2 ">> %s\n" "$line"
				fi
			fi
			if ((${+FDS[0]})); then
				IFS= read line || break
				printf "%s\n" "$line"
			fi
		elif (($#FDS)); then
			break
		fi
	done
}
exit $pipestatus[1]