1 # ncurses output for DCT, by Shiar
3 # 1.13.0 200411042100 - hook to display and handle menu
4 # - submenus are named instead of numbered
5 # - refresh hook renamed to showall
6 # 1.12.0 200410312200 - define main loop (get input from Term::ReadKey)
7 # 1.11.0 200410152225 - uses class in filename instead of $set{display} check
8 # 1.10.0 200410140120 - all output functions seperated from main
16 use vars qw(%falias $path);
17 require $path."termcommon.pm";
20 $set{height} = $LINES-3 if $LINES>=3;
21 $set{width} = $COLS if $COLS;
22 $set{menushow} = int($set{width}/(4+$set{width}/20))+1; # menu items to show simultaneously
25 push @{$hook{init}}, sub {
27 ReadMode 3; # cbreak mode
32 } # restore terminal on quit
36 refresh; # setup for new screen size
37 setsize(); # adjust for new sizes
38 redraw(all=>1); # queue complete refresh
39 draw(); # redraw rightnow
45 push @{$hook{showerror}}, sub {
52 ReadKey; # wait for confirm
53 1 while defined ReadKey(-1); # clear key buffer
56 push @{$hook{showstack}}, sub {
58 addstr($set{height}-$_, 1, "$_: ".showval($stack[$_], $set{base}));
61 clrtoeol($set{height}-@stack, 1);
64 push @{$hook{showmenu}}, sub {
65 clrtoeol($set{height}+2, 1);
67 for (grep exists $menu[0][$_], $menu[0][0]+1..$menu[0][0]+$set{menushow}) {
69 next unless defined $menu[0][$_];
70 my $sub = (my $s = $menu[0][$_]) =~ s/>\w+$//;
71 addstr($set{height}+2, $set{width}/$set{menushow}*$nr, $_);
75 addch('>') if $sub; # indicate submenu
79 $action{more} = [-1, sub {
80 $menu[0][0] += $set{menushow};
81 $menu[0][0] = 0 if $menu[0][0] > @{$menu[0]};
82 $_->() for @{$hook{showmenu}};
85 unshift @{$hook{precmd}}, sub {
86 exists $falias{$_} or return; # handle function key
88 shift @menu if @menu>1; # remove current submenu
91 } # escape (go to parent)
92 $_ = $menu[0][$falias{$_}] and return; # execute found menu item instead
93 error("no such menu entry");
95 }; # precmd (menu item selection)
97 push @{$hook{showall}}, sub {
99 addstr($set{height}+1, 0, "> "); # prompt
102 push @{$hook{showentry}}, sub {
103 addstr($set{height}+1, 2, $_[0]);
112 my $key = ReadKey; # wait for user input
113 if ($key eq chr 27) {
114 $key .= $_ while defined ($_ = ReadKey(-1)); # read additional keys
123 title => "curses output",