# s-lang output for DCT, by Shiar

# 1.13.0 200411042100 - menu i/o functions
#                     - refresh hook renamed to showall
# 1.12.0 200411032145 - define main loop
#                     - use slang key reading functions
# 1.11.0 200410291300 - basic output using Term::Slang (ported from Curses)

use strict;
use warnings;

use Term::Slang qw(:all);

use vars qw(%falias $path);
require $path."termcommon.pm";

push @{$hook{init}}, sub {
	SLtt_get_terminfo and exit;
	SLang_init_tty(-1, 0, 1);
	SLsmg_init_smg;
	SLtt_set_color(1, 0, 'black', 'lightgray');
	
	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} -= 3;
	$set{menushow} = int($set{width}/(4+$set{width}/20))+1;  # menu items to show simultaneously
}; # 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{showmenu}}, sub {
	SLsmg_gotorc($set{height}+2, 1);
	SLsmg_erase_eol;
	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 ]+$//;
		SLsmg_gotorc($set{height}+2, $set{width}/$set{menushow}*$nr);
		SLsmg_write_string($_);
		SLsmg_reverse_video; # reverse
		SLsmg_write_string($s);
		SLsmg_normal_video;
		SLsmg_write_string('>') if $sub;  # indicate submenu
	} # display menu txts
}; # showmenu

$action{more} = [-1, sub {
	$menu[0][0] += $set{menushow};
	$menu[0][0] = 0 if $menu[0][0] > @{$menu[0]};
	$_->() for @{$hook{showmenu}};
}]; # tab

unshift @{$hook{precmd}}, sub {
	exists $falias{$_} or return;  # handle function key
	if ($falias{$_}==0) {
		shift @menu if @menu>1;  # remove current submenu
		redraw(menu=>1);
		return 1;
	} # escape (go to parent)
	$_ = $menu[0][$falias{$_}] and return;  # execute found menu item instead
	error("no such menu entry");
	return 1;
}; # precmd

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

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.13",
};

