# console output for DCT, by Shiar

# 1.12.0 200411032130 - handle input via Term::ReadKey; define main loop
# 1.11.0 200410152225 - class in file name, so check is not needed anymore
# 1.10.1 200410142200 - startup message omitted (now shown by main)
# 1.10.0 200410140120 - never clear screen (just let it scroll)
#                     - try to get width/height from environment vars
#                     - use escape sequences for clear/reposition/invert
#                     - print everything to STDOUT

use strict;
use warnings;

use Term::ReadKey;

push @{$hook{init}}, sub {
	ReadMode 3;  # cbreak mode

#	print "\e[4mDCT $::VERSION\e[24m ";  # print intro (underlined)
	END {
		ReadMode 0;
		print "\n";
	}

	$set{height} = $ENV{LINES}-2 if $ENV{LINES} and $ENV{LINES}>=3;
	$set{width} = $ENV{COLUMNS} if $ENV{COLUMNS};
}; # init

push @{$hook{showerror}}, sub {
	print "\n\a\e[7m$_[0]\e[27m";  # bell and reverse video
}; # showerror

push @{$hook{showstack}}, sub {
	for (reverse 0..@stack-1) {
		print "\n$_: ", showval($stack[$_], $set{base});
	} # show stack
	print "\n> ";  # prompt
}; # showstack

push @{$hook{showmenu}}, sub {
	my $nr = -1;
	for (grep exists $menu[0][$_], $menu[0][0]+1..$menu[0][0]+$set{menushow}) {
		$nr++;
		next unless defined $menu[0][$_];
		my $sub = (my $s = $menu[0][$_]) =~ s/>[\w ]+$//;
		print " $_:$s";
		print ">" if $sub;  # indicate submenu
	} # display menu txts
	print "\n> ";
}; # showmenu

push @{$hook{showall}}, sub {
#	print "\ec";  # reset (clear screen, go home)
}; # showall

push @{$hook{showentry}}, sub {
	print "\e[3G\e[K", $_[0];  # cursor to column #3; erase line
}; # showentry

$hook{main} = sub {
	while (1) {
		draw();

		my $key = ReadKey;  # wait for user input
		if ($key eq chr 27) {
			$key .= $_ while defined ($_ = ReadKey(-1));  # read additional keys
		} # escape sequence
		onkey($key);
	} # input loop
}; # main

return {
	author  => "Shiar",
	title   => "console output",
	version => "1.12",
};

