blib/lib/App/optex/scroll.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package App::optex::scroll;
2 1 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use Term::ANSIColor::Concise qw(:all);
10 use List::Util qw(first pairmap);
11 use Scalar::Util;
12 *is_number = \&Scalar::Util::looks_like_number;
13  
14 our $VERSION = "0.9902";
15  
16 use App::optex::util::filter qw(interval);
17  
18 my %opt = (
19 line => 10,
20 wait => \(our $wait = 1),
21 debug => \(our $debug = undef),
22 timeout => \(our $timeout = 0.1),
23 interval => 0,
24 );
25  
26 sub hash_to_spec {
27 pairmap {
28 $a = "$a|${\(uc(substr($a, 0, 1)))}";
29 my $ref = ref $b;
30 if (not defined $b) { "$a!" }
31 elsif ($ref eq 'SCALAR') { "$a!" }
32 elsif (is_number($b)) { "$a=i" }
33 else { "$a=s" }
34 } shift->%*;
35 }
36  
37 sub flush {
38 STDERR->printflush(@_);
39 }
40  
41 sub set_region {
42 flush join('',
43 csi_code('DECSC'),
44 csi_code('STBM', @_),
45 csi_code('DECRC'));
46 }
47  
48 END {
49 close STDOUT if $wait;
50 set_region();
51 }
52  
53 sub finalize {
54 our($mod, $argv) = @_;
55 #
56 # private option handling
57 #
58 if (@$argv and $argv->[0] !~ /^-M/ and
59 defined(my $i = first { $argv->[$_] eq '--' } keys @$argv)) {
60 splice @$argv, $i, 1; # remove '--'
61 if (local @ARGV = splice @$argv, 0, $i) {
62 use Getopt::Long qw(GetOptionsFromArray);
63 Getopt::Long::Configure qw(bundling);
64 GetOptions \%opt, hash_to_spec \%opt or die "Option parse error.\n";
65 }
66 }
67 my $i = first { $argv->[$_] eq '--' } keys @$argv;
68 if (defined $i and $argv->[0] !~ /^-M/) {
69 splice @$argv, $i, 1; # remove '--'
70 if (local @ARGV = splice @$argv, 0, $i) {
71 use Getopt::Long qw(GetOptionsFromArray);
72 Getopt::Long::Configure qw(bundling);
73 GetOptions \%opt, hash_to_spec \%opt or die "Option parse error.\n";
74 }
75 }
76  
77 my $region = $opt{line};
78 flush "\n" x $region;
79 flush csi_code(CPL => $region); # CPL: Cursor Previous Line
80 my($l, $c) = cursor_position() or return;
81 set_region($l, $l + $region);
82  
83 if (my $time = $opt{interval}) {
84 interval(time => $time);
85 }
86 }
87  
88 sub cursor_position {
89 my $answer = ask(csi_code(DSR => 6), qr/R\z/); # DSR: Device Status Report
90 csi_report(CPR => 2, $answer); # CPR: Cursor Position Report
91 }
92  
93 sub uncntrl {
94 $_[0] =~ s/([^\040-\176])/sprintf "\\%03o", ord $1/gear;
95 }
96  
97 sub ask {
98 my($request, $end_re) = @_;
99 if ($debug) {
100 flush sprintf "[%s] Request: %s\n",
101 __PACKAGE__,
102 uncntrl $request;
103 }
104 open my $tty, "+<", "/dev/tty" or return;
105 ReadMode "cbreak", $tty;
106 $tty->printflush($request);
107 my $answer = '';
108 while (defined (my $key = ReadKey $timeout, $tty)) {
109 if ($debug) {
110 flush sprintf "[%s] ReadKey: \"%s\"\n",
111 __PACKAGE__,
112 $key =~ /\P{Cc}/ ? $key : uncntrl $key;
113 }
114 $answer .= $key;
115 last if $answer =~ /$end_re/;
116 }
117 ReadMode "restore", $tty;
118 if ($debug) {
119 flush sprintf "[%s] Answer: %s\n",
120 __PACKAGE__,
121 uncntrl $answer;
122 }
123 return $answer;
124 }
125  
126 sub set {
127 pairmap {
128 if (ref $opt{$a} eq 'SCALAR') {
129 $opt{$a}->$* = $b;
130 }
131 elsif (ref $opt{$a} eq 'ARRAY') {
132 push $opt{$a}->@*, $b;
133 }
134 else {
135 $opt{$a} = $b;
136 }
137 } @_;
138 }
139  
140 1;
141  
142 __END__
↑ Summary