8f94960af14927a24e17b0a85787fe1b5de0aef8
[descalc.git] / 03_disp_curses.pm
1 # ncurses output for DCT, by Shiar
2
3 # 1.12.0 200410312200 - define main loop (get input from Term::ReadKey)
4 # 1.11.0 200410152225 - uses class in filename instead of $set{display} check
5 # 1.10.0 200410140120 - all output functions seperated from main
6
7 use strict;
8 use warnings;
9
10 use Curses;
11 use Term::ReadKey;
12
13 push @{$hook{init}}, sub {
14         initscr;
15         ReadMode 3;  # cbreak mode
16
17         END {
18                 ReadMode 0;
19                 endwin;
20         } # restore terminal on quit
21
22         $set{height} = $LINES-2 if $LINES>=3;
23         $set{width} = $COLS if $COLS;
24 }; # init
25
26 push @{$hook{showerror}}, sub {
27         attron(A_REVERSE);
28         addstr(0, 0, shift);
29         attroff(A_REVERSE);
30         clrtoeol;
31         refresh;
32
33         ReadKey; # wait for confirm
34         1 while defined ReadKey(-1); # clear key buffer
35 }; # showerror
36
37 push @{$hook{showstack}}, sub {
38         for (0..@stack-1) {
39                 addstr($set{height}-$_, 1, "$_: ".showval($stack[$_], $set{base}));
40                 clrtoeol;
41         } # show stack
42         clrtoeol($set{height}-@stack, 1);
43 }; # showstack
44
45 push @{$hook{refresh}}, sub {
46         clear;
47         addstr($set{height}+1, 0, "> ");  # prompt
48 }; # refresh
49
50 push @{$hook{showentry}}, sub {
51         addstr($set{height}+1, 2, $_[0]);
52         clrtoeol;
53         refresh;
54 }; # showentry
55
56 $hook{main} = sub {
57         while (1) {
58                 draw();
59
60                 my $key = ReadKey;  # wait for user input
61                 if ($key eq chr 27) {
62                         $key .= $_ while defined ($_ = ReadKey(-1));  # read additional keys
63                 } # escape sequence
64
65                 onkey($key);
66         } # input loop
67 }; # main
68
69 return {
70         author  => "Shiar",
71         title   => "curses output",
72         version => "1.12",
73 };
74