Choose/Linux.pm

filter: Statementstmt 12.3% 21 / 170 Branchbran 0.0% 0 / 158 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 53.8% 7 / 13 Podpod 0.0% 0 / 1 totaltotal 7.6% 28 / 365 CC 85 SCAR 74.8
CC
85
Cov
6%
__get_key_OS
3080.0
__get_cursor_row
156.0
__set_mode
90.0
CRAP 1781.0
« Choose.pm ↑ Summary Choose/LineFold.pm »
1 package Term::Choose::Linux;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '1.783';
8  
9 3 ▶use Term::Choose::Constants qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Term::Choose::Screen qw( hide_cursor show_cursor normal );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 SET_ANY_EVENT_MOUSE_1003 => "\e[?1003h",
15 SET_SGR_EXT_MODE_MOUSE_1006 => "\e[?1006h",
16 UNSET_ANY_EVENT_MOUSE_1003 => "\e[?1003l",
17 UNSET_SGR_EXT_MODE_MOUSE_1006 => "\e[?1006l",
18 3 };
19  
20  
21 3 my $Stty = '';
22  
23  
24 113 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
25 113 return bless {}, $_[0];
26 }
27  
28  
29 0 ▶sub _getc_wrapper {
Subroutine _getc_wrapper: not called CC=2 SCAR=17.9
30 0 my ( $timeout ) = @_;
31 0 ▶ if ( TERM_READKEY ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&TERM_READKEY()) { } 0 0 0
32 0 return Term::ReadKey::ReadKey( $timeout );
33 }
34 else {
35 # if ( $timeout ) {
36 # my $rin = '';
37 # vec( $rin, fileno( STDIN ), 1 ) = 1;
38 # my $nfount = select( $rin, undef, undef, $timeout );
39 # return if ! $nfount;
40 # }
41 0 return getc();
42 }
43 }
44  
45  
46 0 ▶sub __get_key_OS {
Subroutine __get_key_OS: not called CC=55 SCAR=80.3
47 0 my ( $self, $mouse ) = @_;
48 0 my $c1 = _getc_wrapper( 0 );
49 0 ▶ return if ! defined $c1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c1 0 0 0
50 0 ▶ if ( $c1 eq "\e" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c1 eq "\e") { } 0 0 0
51 0 my $c2 = _getc_wrapper( 0.10 );
52 0 ▶ if ( ! defined $c2 ) { return KEY_ESC; } # unused #\e
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $c2) { } 0 0 0
elsif ($c2 eq 'O') { } 0 0 0
elsif ($c2 eq '[') { } 0 0 0
53 elsif ( $c2 eq 'O' ) {
54 0 my $c3 = _getc_wrapper( 0 );
55 0 ▶ if ( $c3 eq 'A' ) { return VK_UP; } #\eOA
Branch: 11 branches
BranchTrueFalseTotal
if ($c3 eq 'A') { } 0 0 0
elsif ($c3 eq 'B') { } 0 0 0
elsif ($c3 eq 'C') { } 0 0 0
elsif ($c3 eq 'D') { } 0 0 0
elsif ($c3 eq 'F') { } 0 0 0
elsif ($c3 eq 'H') { } 0 0 0
elsif ($c3 eq 'P') { } 0 0 0
elsif ($c3 eq 'Q') { } 0 0 0
elsif ($c3 eq 'R') { } 0 0 0
elsif ($c3 eq 'S') { } 0 0 0
elsif ($c3 eq 'Z') { } 0 0 0
56 0 elsif ( $c3 eq 'B' ) { return VK_DOWN; } #\eOB
57 0 elsif ( $c3 eq 'C' ) { return VK_RIGHT; } #\eOC
58 0 elsif ( $c3 eq 'D' ) { return VK_LEFT; } #\eOD
59 0 elsif ( $c3 eq 'F' ) { return VK_END; } #\eOF
60 0 elsif ( $c3 eq 'H' ) { return VK_HOME; } #\eOH
61 0 elsif ( $c3 eq 'P' ) { return VK_F1; } #\eOP
62 0 elsif ( $c3 eq 'Q' ) { return VK_F2; } #\eOQ
63 0 elsif ( $c3 eq 'R' ) { return VK_F3; } #\eOR
64 0 elsif ( $c3 eq 'S' ) { return VK_F4; } #\eOS
65 0 elsif ( $c3 eq 'Z' ) { return KEY_BTAB; } #\eOZ
66 }
67 elsif ( $c2 eq '[' ) {
68 0 my $c3 = _getc_wrapper( 0 );
69 0 ▶ if ( $c3 eq 'A' ) { return VK_UP; } #\e[A
Branch: 11 branches
BranchTrueFalseTotal
if ($c3 eq 'A') { } 0 0 0
elsif ($c3 eq 'B') { } 0 0 0
elsif ($c3 eq 'C') { } 0 0 0
elsif ($c3 eq 'D') { } 0 0 0
elsif ($c3 eq 'F') { } 0 0 0
elsif ($c3 eq 'H') { } 0 0 0
elsif ($c3 eq 'Z') { } 0 0 0
elsif ($c3 eq '1') { } 0 0 0
elsif ($c3 =~ /^[23456]$/) { } 0 0 0
elsif ($c3 eq 'M' and $mouse) { } 0 0 0
elsif ($c3 eq '<' and $mouse) { } 0 0 0
70 0 elsif ( $c3 eq 'B' ) { return VK_DOWN; } #\e[B
71 0 elsif ( $c3 eq 'C' ) { return VK_RIGHT; } #\e[C
72 0 elsif ( $c3 eq 'D' ) { return VK_LEFT; } #\e[D
73 0 elsif ( $c3 eq 'F' ) { return VK_END; } #\e[F
74 0 elsif ( $c3 eq 'H' ) { return VK_HOME; } #\e[H
75 0 elsif ( $c3 eq 'Z' ) { return KEY_BTAB; } #\e[Z
76 elsif ( $c3 eq '1' ) {
77 0 my $c4 = _getc_wrapper( 0 );
78 0 ▶ if ( $c4 eq 'F' ) { return VK_END; } #\e[1F
Branch: 4 branches
BranchTrueFalseTotal
if ($c4 eq 'F') { } 0 0 0
elsif ($c4 eq 'H') { } 0 0 0
elsif ($c4 =~ /^[1234]$/) { } 0 0 0
elsif ($c4 eq '~') { } 0 0 0
79 0 elsif ( $c4 eq 'H' ) { return VK_HOME; } #\e[1H
80 elsif ( $c4 =~ m/^[1234]$/ ) {
81 0 my $c5 = _getc_wrapper( 0 );
82 0 ▶ if ( $c5 eq '~' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c5 eq "~") 0 0 0
83 0 ▶ if ( $c4 eq '1' ) { return VK_F1; } #\e[11~
Branch: 4 branches
BranchTrueFalseTotal
if ($c4 eq '1') { } 0 0 0
elsif ($c4 eq '2') { } 0 0 0
elsif ($c4 eq '3') { } 0 0 0
elsif ($c4 eq '4') { } 0 0 0
84 0 elsif ( $c4 eq '2' ) { return VK_F2; } #\e[12~
85 0 elsif ( $c4 eq '3' ) { return VK_F3; } #\e[13~
86 0 elsif ( $c4 eq '4' ) { return VK_F4; } #\e[14~
87 }
88 }
89 0 elsif ( $c4 eq '~' ) { return VK_HOME; } #\e[1~
90 }
91 elsif ( $c3 =~ m/^[23456]$/ ) {
92 0 my $c4 = _getc_wrapper( 0 );
93 0 ▶ if ( $c4 eq '~' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c4 eq "~") 0 0 0
94 0 ▶ if ( $c3 eq '2' ) { return VK_INSERT; } #\e[2~
Branch: 5 branches
BranchTrueFalseTotal
if ($c3 eq '2') { } 0 0 0
elsif ($c3 eq '3') { } 0 0 0
elsif ($c3 eq '4') { } 0 0 0
elsif ($c3 eq '5') { } 0 0 0
elsif ($c3 eq '6') { } 0 0 0
95 0 elsif ( $c3 eq '3' ) { return VK_DELETE; } #\e[3~
96 0 elsif ( $c3 eq '4' ) { return VK_END; } #\e[4~ #
97 0 elsif ( $c3 eq '5' ) { return VK_PAGE_UP; } #\e[5~
98 0 elsif ( $c3 eq '6' ) { return VK_PAGE_DOWN; } #\e[6~
99 }
100 }
101 # http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
102 ▶ elsif ( $c3 eq 'M' && $mouse ) {
Condition0%0/3
$c3 eq "M" and $mouse
!ll&&!rl&&r
000
MC/DC0%0/2
$c3 eq "M" and $mouse
$c3 eq "M" $mouse
Truth table0%0/3
A and B
A= $c3 eq "M"B= $mouse
ABresult
0X0
100
111
103 0 my $event_type = ord( _getc_wrapper( 0 ) ) - 32;
104 0 my $x = ord( _getc_wrapper( 0 ) ) - 32;
105 0 my $y = ord( _getc_wrapper( 0 ) ) - 32;
106 0 my $button = $self->__mouse_event_to_button( $event_type );
107 0 ▶ return NEXT_get_key if $button == NEXT_get_key;
Branch: 1 branch
BranchTrueFalseTotal
if $button == -1 0 0 0
108 0 return [ $button, $x, $y ];
109 }
110 ▶ elsif ( $c3 eq '<' && $mouse ) { # SGR 1006
Condition0%0/3
$c3 eq "<" and $mouse
!ll&&!rl&&r
000
MC/DC0%0/2
$c3 eq "<" and $mouse
$c3 eq "<" $mouse
Truth table0%0/3
A and B
A= $c3 eq "<"B= $mouse
ABresult
0X0
100
111
111 0 my $event_type = '';
112 0 my $m1;
113 0 ▶ while ( ( $m1 = _getc_wrapper( 0 ) ) =~ m/^[0-9]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($m1 = _getc_wrapper(0)) =~ /^[0-9]$/) 0 0 0
114 0 $event_type .= $m1;
115 }
116 0 ▶ return NEXT_get_key if $m1 ne ';';
Branch: 1 branch
BranchTrueFalseTotal
if $m1 ne ";" 0 0 0
117 0 my $x = '';
118 0 my $m2;
119 0 ▶ while ( ( $m2 = _getc_wrapper( 0 ) ) =~ m/^[0-9]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($m2 = _getc_wrapper(0)) =~ /^[0-9]$/) 0 0 0
120 0 $x .= $m2;
121 }
122 0 ▶ return NEXT_get_key if $m2 ne ';';
Branch: 1 branch
BranchTrueFalseTotal
if $m2 ne ";" 0 0 0
123 0 my $y = '';
124 0 my $m3;
125 0 ▶ while ( ( $m3 = _getc_wrapper( 0 ) ) =~ m/^[0-9]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($m3 = _getc_wrapper(0)) =~ /^[0-9]$/) 0 0 0
126 0 $y .= $m3;
127 }
128 0 ▶ return NEXT_get_key if $m3 !~ m/^[mM]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $m3 =~ /^[mM]$/ 0 0 0
129 0 ▶ my $button_released = $m3 eq 'm' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$m3 eq 'm' ? : 0 0 0
130 0 ▶ return NEXT_get_key if $button_released;
Branch: 1 branch
BranchTrueFalseTotal
if $button_released 0 0 0
131 0 my $button = $self->__mouse_event_to_button( $event_type );
132 0 ▶ return NEXT_get_key if $button == NEXT_get_key;
Branch: 1 branch
BranchTrueFalseTotal
if $button == -1 0 0 0
133 0 return [ $button, $x, $y ];
134 }
135 }
136 }
137 else {
138 0 return ord $c1;
139 }
140 0 return NEXT_get_key;
141 };
142  
143  
144 0 ▶sub __mouse_event_to_button {
Subroutine __mouse_event_to_button: not called CC=4 SCAR=30.0
145 0 my ( $self, $event_type ) = @_;
146 0 my $button_drag = ( $event_type & 0x20 ) >> 5;
147 0 ▶ return NEXT_get_key if $button_drag;
Branch: 1 branch
BranchTrueFalseTotal
if $button_drag 0 0 0
148 0 my $button;
149 0 my $low_2_bits = $event_type & 0x03;
150 0 ▶ if ( $low_2_bits == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($low_2_bits == 3) { } 0 0 0
151 0 $button = 0;
152 }
153 else {
154 0 ▶ if ( $event_type & 0x40 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event_type & 64) { } 0 0 0
155 0 $button = $low_2_bits + 4; # 4,5
156 }
157 else {
158 0 $button = $low_2_bits + 1; # 1,2,3
159 }
160 }
161 0 return $button;
162 }
163  
164  
165 0 ▶sub __set_mode {
Subroutine __set_mode: not called CC=9 SCAR=45.0
166 0 my ( $self, $config ) = @_;
167 0 ▶ if ( $config->{hide_cursor} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"hide_cursor"}) 0 0 0
168 0 print hide_cursor();
169 }
170 0 my $mode_stty;
171 0 ▶ if ( ! $config->{mode} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $config->{'mode'}) { } 0 0 0
elsif ($config->{'mode'} eq 'ultra-raw') { } 0 0 0
elsif ($config->{'mode'} eq 'cbreak') { } 0 0 0
172 0 die "No mode!";
173 }
174 elsif ( $config->{mode} eq 'ultra-raw' ) {
175 0 $mode_stty = 'raw';
176 }
177 elsif ( $config->{mode} eq 'cbreak' ) {
178 0 $mode_stty = 'cbreak';
179 }
180 else {
181 0 die "Invalid mode!";
182 }
183 0 ▶ if ( TERM_READKEY ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&TERM_READKEY()) { } 0 0 0
184 0 Term::ReadKey::ReadMode( $config->{mode} );
185 }
186 else {
187 0 $Stty = qx(stty --save);
188 0 chomp $Stty;
189 0 ▶ system( "stty -echo $mode_stty" ) == 0 or die $?;
Branch: 1 branch
BranchTrueFalseTotal
system("stty -echo $mode_stty") == 0 or die $? 0 0 0
190 }
191 0 ▶ if ( $config->{mouse} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"mouse"}) 0 0 0
192 0 my $return = binmode STDIN, ':raw';
193 0 ▶ if ( $return ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return) { } 0 0 0
194 0 print SET_ANY_EVENT_MOUSE_1003;
195 0 print SET_SGR_EXT_MODE_MOUSE_1006;
196 }
197 else {
198 0 $config->{mouse} = 0;
199 0 warn "binmode STDIN, :raw: $!\nmouse-mode disabled\n";
200 }
201 }
202 0 return $config->{mouse};
203 };
204  
205  
206 0 ▶sub __reset_mode {
Subroutine __reset_mode: not called CC=8 SCAR=42.8
207 0 my ( $self, $config ) = @_;
208 0 ▶ if ( $config->{mouse} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"mouse"}) 0 0 0
209 0 ▶ binmode STDIN, ':encoding(UTF-8)' or warn "binmode STDIN, :encoding(UTF-8): $!\n";
Branch: 1 branch
BranchTrueFalseTotal
binmode STDIN, ":encoding(UTF-8)" or warn "binmode STDIN, :encoding(UTF-8): $!\n" 0 0 0
210 0 print UNSET_SGR_EXT_MODE_MOUSE_1006;
211 0 print UNSET_ANY_EVENT_MOUSE_1003;
212 }
213 0 print normal();
214 0 ▶ if ( TERM_READKEY ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&TERM_READKEY()) { } 0 0 0
215 0 Term::ReadKey::ReadMode( 'restore' );
216 }
217 else {
218 0 ▶ if ( $Stty ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Stty) { } 0 0 0
219 0 ▶ system( "stty $Stty" ) == 0 or die $?;
Branch: 1 branch
BranchTrueFalseTotal
system("stty $Stty") == 0 or die $? 0 0 0
220 }
221 else {
222 0 ▶ system( "stty sane" ) == 0 or die $?;
Branch: 1 branch
BranchTrueFalseTotal
system("stty sane") == 0 or die $? 0 0 0
223 }
224 }
225 0 ▶ if ( $config->{hide_cursor} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"hide_cursor"}) 0 0 0
226 0 print show_cursor();
227 }
228 }
229  
230  
231 0 ▶sub __get_cursor_row {
Subroutine __get_cursor_row: not called CC=12 SCAR=50.5
232 #my ( $self ) = @_;
233 0 my $abs_curs_y;
234 0 print "\e[6n";
235 0 my $c1 = _getc_wrapper( 0 );
236 0 ▶ if ( defined $c1 && $c1 eq "\e" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c1 and $c1 eq "\e") 0 0 0
Condition0%0/3
defined $c1 and $c1 eq "\e"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $c1 and $c1 eq "\e"
defined $c1 $c1 eq "\e"
Truth table0%0/3
A and B
A= defined $c1B= $c1 eq "\e"
ABresult
0X0
100
111
237 0 my $c2 = _getc_wrapper( 0.10 );
238 0 ▶ if ( defined $c2 && $c2 eq '[' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c2 and $c2 eq "[") 0 0 0
Condition0%0/3
defined $c2 and $c2 eq "["
!ll&&!rl&&r
000
MC/DC0%0/2
defined $c2 and $c2 eq "["
defined $c2 $c2 eq "["
Truth table0%0/3
A and B
A= defined $c2B= $c2 eq "["
ABresult
0X0
100
111
239 0 my $c3 = _getc_wrapper( 0 );
240 0 ▶ if ( $c3 =~ m/^[0-9]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c3 =~ /^[0-9]$/) 0 0 0
241 0 my $c4 = _getc_wrapper( 0 );
242 0 ▶ if ( $c4 =~ m/^[;0-9]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c4 =~ /^[;0-9]$/) 0 0 0
243 0 my $curs_y = $c3;
244 0 my $ry = $c4;
245 0 ▶ while ( $ry =~ m/^[0-9]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ry =~ /^[0-9]$/) 0 0 0
246 0 $curs_y .= $ry;
247 0 $ry = _getc_wrapper( 0 );
248 }
249 0 ▶ if ( $ry eq ';' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ry eq ";") 0 0 0
250 #my $curs_x = ''; # unused
251 0 my $rx = _getc_wrapper( 0 );
252 0 ▶ while ( $rx =~ m/^[0-9]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rx =~ /^[0-9]$/) 0 0 0
253 #$curs_x .= $rx;
254 0 $rx = _getc_wrapper( 0 );
255 }
256 0 ▶ if ( $rx eq 'R' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rx eq "R") 0 0 0
257 0 $abs_curs_y = $curs_y;
258 }
259 }
260 }
261 }
262 }
263 }
264 0 ▶ return $abs_curs_y || 1;
Condition0%0/2
$abs_curs_y || 1
l!l
00
MC/DC0%0/1
$abs_curs_y || 1
$abs_curs_y
Truth table0%0/2
A
A= $abs_curs_y
Aresult
11
00
265 }
266  
267  
268  
269 3 1;
270  
271 __END__
« Choose.pm ↑ Summary Choose/LineFold.pm »