07be1da3ab411396d5bf3e9ceadbaec7573f2fd3
[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 "\ec";  # reset (clear screen, go home)
20 #       print "\e[4mDCT $::VERSION\e[24m ";  # print intro (underlined)
21         END {
22                 ReadMode 0;
23                 print "\n";
24         }
25
26         $set{height} = $ENV{LINES}-2 if $ENV{LINES} and $ENV{LINES}>=3;
27         $set{width} = $ENV{COLUMNS} if $ENV{COLUMNS};
28 }; # init
29
30 push @{$hook{showerror}}, sub {
31         print "\n\a\e[7m$_[0]\e[27m";  # bell and reverse video
32 }; # showerror
33
34 push @{$hook{showstack}}, sub {
35         for (reverse 0..@stack-1) {
36                 print "\n$_: ", showval($stack[$_], $set{base});
37         } # show stack
38         print "\n> ";  # prompt
39 }; # showstack
40
41 push @{$hook{showentry}}, sub {
42         print "\e[3G\e[K", $_[0];  # cursor to column #3; erase line
43 }; # showentry
44
45 $hook{main} = sub {
46         while (1) {
47                 draw();
48
49                 my $key = ReadKey;  # wait for user input
50                 if ($key eq chr 27) {
51                         $key .= $_ while defined ($_ = ReadKey(-1));  # read additional keys
52                 } # escape sequence
53                 onkey($key);
54         } # input loop
55 }; # main
56
57 return {
58         author  => "Shiar",
59         title   => "console output",
60         version => "1.12",
61 };
62