release 1.14pre1
[descalc.git] / 05_disp_stdout.pm
1 # console output for DCT, by Shiar
2
3 # 1.12.0 200411032130 - handle input via Term::ReadKey; define main loop
4 # 1.11.0 200410152225 - class in file name, so check is not needed anymore
5 # 1.10.1 200410142200 - startup message omitted (now shown by main)
6 # 1.10.0 200410140120 - never clear screen (just let it scroll)
7 #                     - try to get width/height from environment vars
8 #                     - use escape sequences for clear/reposition/invert
9 #                     - print everything to STDOUT
10
11 use strict;
12 use warnings;
13
14 use Term::ReadKey;
15
16 push @{$hook{init}}, sub {
17         ReadMode 3;  # cbreak mode
18
19 #       print "\e[4mDCT $::VERSION\e[24m ";  # print intro (underlined)
20         END {
21                 ReadMode 0;
22                 print "\n";
23         }
24
25         $set{height} = $ENV{LINES}-2 if $ENV{LINES} and $ENV{LINES}>=3;
26         $set{width} = $ENV{COLUMNS} if $ENV{COLUMNS};
27 }; # init
28
29 push @{$hook{showerror}}, sub {
30         print "\n\a\e[7m$_[0]\e[27m";  # bell and reverse video
31 }; # showerror
32
33 push @{$hook{showstack}}, sub {
34         for (reverse 0..@stack-1) {
35                 print "\n$_: ", showval($stack[$_], $set{base});
36         } # show stack
37         print "\n> ";  # prompt
38 }; # showstack
39
40 push @{$hook{showmenu}}, sub {
41         my $nr = -1;
42         for (grep exists $menu[0][$_], $menu[0][0]+1..$menu[0][0]+$set{menushow}) {
43                 $nr++;
44                 next unless defined $menu[0][$_];
45                 my $sub = (my $s = $menu[0][$_]) =~ s/>[\w ]+$//;
46                 print " $_:$s";
47                 print ">" if $sub;  # indicate submenu
48         } # display menu txts
49         print "\n> ";
50 }; # showmenu
51
52 push @{$hook{showall}}, sub {
53 #       print "\ec";  # reset (clear screen, go home)
54 }; # showall
55
56 push @{$hook{showentry}}, sub {
57         print "\e[3G\e[K", $_[0];  # cursor to column #3; erase line
58 }; # showentry
59
60 $hook{main} = sub {
61         while (1) {
62                 draw();
63
64                 my $key = ReadKey;  # wait for user input
65                 if ($key eq chr 27) {
66                         $key .= $_ while defined ($_ = ReadKey(-1));  # read additional keys
67                 } # escape sequence
68                 onkey($key);
69         } # input loop
70 }; # main
71
72 return {
73         author  => "Shiar",
74         title   => "console output",
75         version => "1.12",
76 };
77