# curses-style cusror control for ANSI screens
# window-like primatives
# april 1993, vic spicer (vspicer@ccu.umanitoba.ca)

# clear screen
sub CLS { print "\e[;H\e[2J"; }

# inverse screen
sub INVERSE { print "\e[7m"; }

# normal screen
sub NORMAL { print "\e[0m"; }
sub RESET { print "\e[0m"; }
sub BOLD { print "\e[1m"; }
sub DARK { print "\e[2m"; }
sub UNDERLINE { print "\e[4m"; }
sub BLINK { print "\e[5m"; }
sub CONCEAL { print "\e[8m"; }

# couleurs
sub BLACK { print "\e[1;30m"; }  
sub RED { print "\e[1;31m"; }  
sub GREEN { print "\e[1;32"; }  
sub YELLOW { print "\e[1;33"; }  
sub BLUE { print "\e[1;34m"; }  
sub MAGENTA { print "\e[1;35m"; }  
sub CYAN { print "\e[1;36m"; }  
sub WHITE { print "\e[1;37m"; }  

sub ON_BLACK { print "\e[1;40m"; }  
sub ON_RED { print "\e[1;41m"; }  
sub ON_GREEN { print "\e[1;42m"; }  
sub ON_YELLOW { print "\e[1;43m"; }  
sub ON_BLUE { print "\e[1;44m"; }  
sub ON_MAGENTA { print "\e[1;45m"; }  
sub ON_CYAN { print "\e[1;46m"; }  
sub ON_WHITE { print "\e[1;47m"; }  

sub BRIGHT_BLACK { print "\e[1;90m"; }  
sub BRIGHT_RED { print "\e[1;91m"; }  
sub BRIGHT_GREEN { print "\e[1;92"; }  
sub BRIGHT_YELLOW { print "\e[1;93"; }  
sub BRIGHT_BLUE { print "\e[1;94m"; }  
sub BRIGHT_MAGENTA { print "\e[1;95m"; }  
sub BRIGHT_CYAN { print "\e[1;96m"; }  
sub BRIGHT_WHITE { print "\e[1;97m"; }  

sub ON_BRIGHT_BLACK { print "\e[1;100m"; }  
sub ON_BRIGHT_RED { print "\e[1;101m"; }  
sub ON_BRIGHT_GREEN { print "\e[1;102"; }  
sub ON_BRIGHT_YELLOW { print "\e[1;103"; }  
sub ON_BRIGHT_BLUE { print "\e[1;104m"; }  
sub ON_BRIGHT_MAGENTA { print "\e[1;105m"; }  
sub ON_BRIGHT_CYAN { print "\e[1;106m"; }  
sub ON_BRIGHT_WHITE { print "\e[1;107m"; }  

# erase line
sub EL { print "\e[K"; }

# position cursor (y,x) 
sub CPOS { local($y,$x)=@_; print "\e[$y;$x"; print "H"; }

# make box (upper left y, upper left x, lower right y, lower right x,
#		title)
sub BOX
{
	local($y1,$x1,$y2,$x2,$title)=@_;
	local($lines)=$x2-$x1;	
	local($j);	
	&CPOS($y1,$x1); &INVERSE; 
	print "+"; print "-" x ($lines-1); print "+";
	for ($j=$y1+1;$j<$y2;$j++)
	{ &CPOS($j,$x1); print "|"; &NORMAL;
	print " " x ($lines-1) ; &INVERSE; print "|"; }
	&CPOS($y2,$x1); print "+"; print "-" x ($lines-1); print "+";
	&NORMAL;
	&CPOS($y1,$x1+5); print $title;
}

# erase box (upper left y, upper left x, lower right y, lower right x)
sub UNBOX
{
	local($y1,$x1,$y2,$x2)=@_;
	local($j);
	for ($j=$y1;$j<=$y2;$j++)
	{ &CPOS($j,$x1); print " " x ($x2-$x1); }
}

# put message (upper left y, upper left x, message);
sub MSG
{
	local($y1,$x1,$mess)=@_;
	&BOX($y1,$x1,$y1+2,$x1+length($mess)+1);
	&CPOS($y1+1,$x1+1); print $mess;
}

# put many lines to window (upper left y, upper left x, x-width, 
#	array of lines)
sub TEXT
{
	local($y1,$x1,$wide,@mess)=@_;
	local($j);
	&BOX($y1,$x1,$y1+2+$#mess,$x1+$wide);
	for ($j=0;$j<=$#mess;$j++)
	{
		&CPOS($y1+1+$j,$x1+1); print $mess[$j];
	}
}

# get many lines from window (upper left y, upper left x, x-width, lines)
sub MLINES
{
	local($y1,$x1,$wide,$lin,$title)=@_;
	local($j,@line);
	&BOX($y1,$x1,$y1+2+$lin,$x1+$wide,$title);
	for ($j=0;$j<=$lin;$j++)
	{
		&CPOS($j+$y1+1,$x1+1); print ">"; $moo=<>; chop($moo);
		$line[$j]=$moo;
	}
	return @line;
}

# get input in window (upper left y, upper left x, prompt, length of input,
#		default input)
sub INPUT
{
	local($y1,$x1,$mess,$length,$default)=@_;
	local($mess2)=$mess . $default . (" " x ($length-length($default)));
	&MSG($y1,$x1,$mess2);
	&CPOS($y1+1,$x1+length($mess)+1);
	local($d);	
	$d=<>; chop($d);
	if ($d eq "") { $d=$default; }
	return $d;
}

# select from options in window (upper left y, upper left x, width-x, title,
#		array of options)
# returns number for selected option, -1 for abort
sub SELECT
{
	local($y1,$x1,$wide,$title,@prompts)=@_;
	local($lls)=$#prompts;
	&BOX($y1,$x1,$y1+$lls+3,$x1+$wide,$title);
	local($j,$letter); $letter="1";
	local($option);

	for ($j=0;$j<=$lls;$j++)
	{
		&CPOS($y1+$j+1,$x1+1); print "$letter: $prompts[$j]";
		$letter++;
	}
	
	$option=0;
	while (($option<1 | $option>$lls+1) & $option ne "q") 
		{ 
			&CPOS($y1+$lls+2,$x1+1);
			print "SELECT:     ";
			&CPOS($y1+$lls+2,$x1+8);
			$option=<>; chop($option); 
	}

	if ($option eq "q") { $option=-1; }
	return $option;
}

1;

