# s-lang output for DCT, by Shiar

# 1.12.0 200411032145 - define main loop
#                     - use slang key reading functions
# 1.11.0 200410291300 - 

use strict;
use warnings;

use Term::Slang qw(:all);

push @{$hook{init}}, sub {
	SLtt_get_terminfo and exit;
	SLang_init_tty(-1, 0, 1);
	SLsmg_init_smg;
	
	END { SLsmg_reset_smg; SLang_reset_tty; } # shutdown display system

	# where are $SLtt_Screen_Rows and $SLtt_Screen_Cols?
	($set{height}, $set{width}) = SLtt_get_screen_size;
	$set{height} -= 2;
}; # init

push @{$hook{showerror}}, sub {
	my $error = shift;
	SLsmg_draw_box(0, 0, 3, length($error)+4);
	SLsmg_gotorc(1, 1);
	SLsmg_write_string(" $error ");
	SLsmg_refresh;

	SLang_getkey;  # wait for confirm
	SLang_getkey while SLang_input_pending(0)==1; # clear key buffer
}; # showerror

push @{$hook{showstack}}, sub {
	for (0..@stack-1) {
		SLsmg_gotorc($set{height}-$_, 1);
		SLsmg_write_string("$_: ".showval($stack[$_], $set{base}));  # prompt
		SLsmg_erase_eol;
	} # show stack
	SLsmg_gotorc($set{height}-@stack, 1);
	SLsmg_erase_eol;
}; # showstack

push @{$hook{refresh}}, sub {
	SLsmg_cls;
	SLsmg_gotorc($set{height}+1, 0);
	SLsmg_write_string("> ");  # prompt
}; # refresh

push @{$hook{showentry}}, sub {
	SLsmg_gotorc($set{height}+1, 2);
	SLsmg_write_string($_[0]);
	SLsmg_erase_eol;
	SLsmg_refresh;
}; # showentry

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

		my $key = chr SLang_getkey;  # wait for user input
		if ($key eq chr 27) {
			$key .= chr SLang_getkey while SLang_input_pending(0)==1;  # read additional keys
		} # escape sequence
#		error(join " ", map ord, split //, $key); #debug
		onkey($key);
	} # input loop
}; # main

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

