File Coverage

blib/lib/Win32/PrintBox.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Win32::PrintBox;
2            
3 1     1   15556 use warnings;
  1         3  
  1         39  
4 1     1   6 use strict;
  1         2  
  1         40  
5 1     1   3513 use Win32;
  0            
  0            
6             use Win32::Console::ANSI qw/ Cursor /;
7             require Exporter;
8             our @ISA = qw( Exporter );
9             our @EXPORT_OK = qw(set_page_size get_page_size);
10            
11             our $VERSION = sprintf "%d.%03d", q$Revision: 0.004 $ =~ /: (\d+)\.(\d+)/;
12            
13             our @COLLECTED_TEXT = ();
14             our $DEFAULT_PAGE_SIZE = 25;
15             our $PAGE_SIZE = $DEFAULT_PAGE_SIZE;
16             our $PAGE_NUM = 1;
17             our ($WinMode, $CMDExists);
18             # init();
19            
20             END {
21             pause();
22             }
23            
24             ##################################
25             # Put module initialization in a sub, so it can be called in testing
26             sub init {
27             $WinMode = $^X =~ m/wperl/i;
28             # Use fileno(STDOUT) to see if STDOUT is open (-1 for wperl)
29            
30             $CMDExists = CMD_Cursor();
31             # Warn handler
32             if ($WinMode) {
33             tie *MSG_FH, 'Win32::PrintBox::IO';
34             select MSG_FH;
35             }
36            
37             # DIE handler (removed - END block is sufficient)
38             return 1;
39             }
40             ##################################
41             sub pause {
42             return if $WinMode || $CMDExists;
43             # Otherwise, ask the user for permission to close, so any output is acknowledged
44             $|++; # Flush any pending output
45             print STDERR "Press Enter to continue\n";
46             pause_in();
47             }
48             ##################################
49             # put this in a subroutine, so we can override it for testing
50             sub pause_in {
51             ;
52             }
53             ############]######################
54             sub CMD_Cursor {
55             # If a new command prompt window is opened, then the 'y' cursor position will be at the top
56             # This can be faked by running cls & my_perl_script.pl
57             my ($x, $y) = Cursor(); # reads cursor position
58             return $y>1; # Home is 1,1
59             }
60            
61             ##################################
62             # Accessor for number or elements to cache in @COLLECTED_TEXT
63             sub get_page_size {
64             return $PAGE_SIZE;
65             }
66             ##################################
67             # Mutator for number or elements to cache in @COLLECTED_TEXT
68             sub set_page_size {
69             my $temp = shift // $DEFAULT_PAGE_SIZE;
70             $PAGE_SIZE = $temp;
71             }
72            
73             ##################################
74             ##################################
75             package Win32::PrintBox::IO;
76            
77             use constant VB_OK => 0;
78             use constant VB_OK_CANCEL => 1;
79             use constant VB_CANCEL => 2;
80            
81             ##################################
82             sub new {
83             my $self = bless {}, shift;
84            
85             # Do not re-define warn Handler if already called once by this package
86             return if defined $main::W32_PRINT_BOX_OLD_WARN;
87            
88             # Using a "__WARN__" handler provides the way to re-direct warn statements to a MsgBox
89             $main::W32_PRINT_BOX_OLD_WARN = $SIG{__WARN__};
90             $SIG{__WARN__} = sub { WarnBox(@_) };
91             return $self;
92             }
93            
94             ##################################
95             sub WarnBox { # Send Warnings immediately
96             my $msg = join("\n", @_);
97             return if $msg eq '';
98             my $answer = Win32::MsgBox($msg, VB_OK_CANCEL, 'Warning!');
99             die if $answer == VB_CANCEL;
100             my $h = $main::W32_PRINT_BOX_OLD_WARN;
101             $h->(@_) if $h; # invoke the old handler
102             return 1;
103             }
104            
105             ##################################
106             sub PrintMsgBox {
107             my $self = shift;
108             push (@COLLECTED_TEXT, @_);
109             if ($#COLLECTED_TEXT >= $PAGE_SIZE - 1) {
110             my $answer = Win32::MsgBox( join('', @COLLECTED_TEXT), VB_OK_CANCEL, 'Page ' . $PAGE_NUM++);
111             die if $answer == VB_CANCEL;
112             @COLLECTED_TEXT = ();
113             }
114             }
115            
116             sub TIEHANDLE { shift->new(@_) }
117             sub PRINT { shift->PrintMsgBox(@_) }
118             sub PRINTF { shift->PrintMsgBox(sprintf(@_)) }
119             # DESTROY only applies in WinMode; in DOS mode, we didnt tie anything, so nothing to destroy
120             sub DESTROY { Win32::MsgBox( join('', @COLLECTED_TEXT), VB_OK, 'Done') if @COLLECTED_TEXT}
121            
122             1;
123             __END__