#!/bin/bash -efu

IM_gauge()
{
	IM_start_output

	local title="$1" text="${2-}"
	local height=1 width=$(( 4 + ${#text} ))

	if [ $width -gt ${_IM_max_width} ]; then
		height=$(( $width / ${_IM_max_width} + 1 ))
		width=${_IM_max_width}
	elif [ $width -lt 40 ]; then
		[ $width -ne 4 ] ||
			height=0
		width=40
	fi

	if [ -n "$text" ]; then
		height=$((1 + $height))
		text="\n$text"
	fi

	dialog	$IM_WIDGET_ARGS					\
		${NOLINES:+--ascii-lines}			\
		${IM_BACKTITLE:+--backtitle "$IM_BACKTITLE"}	\
		--title "$title"				\
		--gauge "$text"					\
		$((5 + $height)) $width 2>/dev/null		\
	||:
}
