Kit/Screen.pm

filter: Statementstmt 95.9% 94 / 98 Branchbran 91.0% 51 / 56 Conditioncond 88.4% 46 / 52 MC/DCmcdc 79.3% 23 / 29 Subroutinesub 100% 21 / 21 Podpod 93.7% 15 / 16 totaltotal 91.9% 250 / 272 CC 48 SCAR 17.8
CC
48
Cov
93%
measured
15.5
too_small
8.0
BUILD
7.0
CRAP 5.9
« Kit/Line.pm ↑ Summary Kit/Keys.pm »
1 package Game::Terminal::Kit::Screen;
2  
3 11 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 our $VERSION = '0.02';
10  
11 has out => (
12 is => 'rw'
13 11 );
14  
15 has [qw/interactive colour columns rows/] => (
16 is => 'rw'
17 11 );
18  
19 has [qw/ascii cursor_hidden/] => (
20 is => 'rw',
21 isa => Bool,
22 default => 0
23 11 );
24  
25 50 ▶sub BUILD {
Subroutine BUILD: called CC=7 SCAR=19.5
Pod undocumented
26 50 my ($self) = @_;
27 50 ▶ $self->out(\*STDOUT) unless $self->out;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->out 3 47 50
28 50 ▶ $self->interactive(-t $self->out ? 1 : 0) unless defined $self->interactive;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->interactive 13 37 50
-t $self->out ? : 0 13 13
29 50 ▶ $self->colour($self->interactive && !no_colour() ? 1 : 0)
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->colour 14 36 50
$self->interactive && !no_colour() ? : 3 11 14
Condition100%3/3
$self->interactive and not no_colour()
!ll&&!rl&&r
833
MC/DC100%2/2
$self->interactive and not no_colour()
$self->interactive not no_colour()
Truth table100%3/3
A and not(B)
A= $self->interactiveB= not no_colour()
ABresult
0X0
100
111
30 unless defined $self->colour;
31 50 return $self;
32 }
33  
34 6 ▶sub no_colour {
Subroutine no_colour: called CC=3 SCAR=11.0
Pod documented
35 6 ▶ return defined $ENV{NO_COLOR} && length $ENV{NO_COLOR} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'NO_COLOR'} && length $ENV{'NO_COLOR'} ? : 3 3 6
Condition100%3/3
defined $ENV{"NO_COLOR"} and length $ENV{"NO_COLOR"}
!ll&&!rl&&r
213
MC/DC100%2/2
defined $ENV{"NO_COLOR"} and length $ENV{"NO_COLOR"}
defined $ENV{"NO_COLOR"} length $ENV{"NO_COLOR"}
Truth table100%3/3
A and B
A= defined $ENV{"NO_COLOR"}B= length $ENV{"NO_COLOR"}
ABresult
0X0
100
111
36 }
37  
38 106 ▶sub _print {
Subroutine _print: called CC=1 SCAR=0.0
39 106 my ($self, @text) = @_;
40 106 my $out = $self->out;
41 106 print {$out} @text;
42 106 return $self;
43 }
44  
45 1 ▶sub flush_on {
Subroutine flush_on: called CC=1 SCAR=0.0
Pod documented
46 1 my ($self) = @_;
47 1 my $previous = select $self->out;
48 1 $| = 1;
49 1 select $previous;
50 1 return $self;
51 }
52  
53 3 ▶sub utf8 {
Subroutine utf8: called CC=4 SCAR=13.9
Pod documented
54 3 my ($self) = @_;
55 3 ▶ return $self if $self->ascii;
Branch: 1 branch
BranchTrueFalseTotal
if $self->ascii 1 2 3
56 2 my @layers = eval { PerlIO::get_layers($self->out) };
57 2 ▶ return $self if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
58 2 ▶ return $self if grep { m/\A(?:utf8|encoding)/ } @layers;
Branch: 1 branch
BranchTrueFalseTotal
if grep {/\A(?:utf8|encoding)/u;} @layers 1 1 2
59 1 binmode $self->out, ':encoding(UTF-8)';
60 1 return $self;
61 }
62  
63 4 ▶sub say {
Subroutine say: called CC=3 SCAR=11.0
Pod documented
64 4 my ($self, @lines) = @_;
65 4 ▶ @lines = ('') unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 1 3 4
66 4 ▶ return $self->_print(map { (defined $_ ? $_ : '') . "\n" } @lines);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 5 1 6
67 }
68  
69 5 ▶sub clear {
Subroutine clear: called CC=3 SCAR=11.0
Pod documented
70 5 my ($self, %option) = @_;
71 5 ▶ return $self unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 3 2 5
72 2 ▶ return $self->_print($option{scrollback} ? "\e[2J\e[3J\e[H" : "\e[2J\e[H");
Branch: 1 branch
BranchTrueFalseTotal
$option{'scrollback'} ? : 1 1 2
73 }
74  
75 87 ▶sub show {
Subroutine show: called CC=2 SCAR=6.9
Pod documented
76 87 my ($self, $lines) = @_;
77 87 ▶ return $self->_print("\e[H", (map { "$_\e[K\n" } @{$lines}), "\e[J")
Branch: 1 branch
BranchTrueFalseTotal
if $self->interactive 5 82 87
78 if $self->interactive;
79 82 return $self->_print(map { "$_\n" } @{$lines});
80 }
81  
82 6 ▶sub rewind {
Subroutine rewind: called CC=4 SCAR=13.9
Pod documented
83 6 my ($self, $count) = @_;
84 6 ▶ return $self unless $self->interactive && $count && $count > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive and $count and $count > 0 5 1 6
Condition100%6/6
$self->interactive and $count and $count > 0
!ll&&!rl&&r
411
$self->interactive and $count
!ll&&!rl&&r
222
MC/DC100%3/3
$self->interactive and $count and $count > 0
$self->interactive $count $count > 0
Truth table100%4/4
A and B and C
A= $self->interactiveB= $countC= $count > 0
ABCresult
0XX0
10X0
1100
1111
85 1 return $self->_print("\e[${count}A\r");
86 }
87  
88 3 ▶sub at {
Subroutine at: called CC=2 SCAR=6.9
Pod documented
89 3 my ($self, $row, $column) = @_;
90 3 ▶ return $self unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 2 1 3
91 1 return $self->_print("\e[${row};${column}H");
92 }
93  
94 8 ▶sub paint {
Subroutine paint: called CC=4 SCAR=13.9
Pod documented
95 8 my ($self, $text, $code) = @_;
96 8 ▶ return $text unless $self->colour && defined $code && length $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour and defined $code and length $code 5 3 8
Condition100%6/6
$self->colour and defined $code and length $code
!ll&&!rl&&r
413
$self->colour and defined $code
!ll&&!rl&&r
314
MC/DC100%3/3
$self->colour and defined $code and length $code
$self->colour defined $code length $code
Truth table100%4/4
A and B and C
A= $self->colourB= defined $codeC= length $code
ABCresult
0XX0
10X0
1100
1111
97 3 return "\e[${code}m" . $text . "\e[0m";
98 }
99  
100 11 ▶sub hide_cursor {
Subroutine hide_cursor: called CC=3 SCAR=11.0
Pod documented
101 11 my ($self) = @_;
102 11 ▶ return $self unless $self->interactive && !$self->cursor_hidden;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive and not $self->cursor_hidden 6 5 11
Condition100%3/3
$self->interactive and not $self->cursor_hidden
!ll&&!rl&&r
515
MC/DC100%2/2
$self->interactive and not $self->cursor_hidden
$self->interactive not $self->cursor_hidden
Truth table100%3/3
A and not(B)
A= $self->interactiveB= not $self->cursor_hidden
ABresult
0X0
100
111
103 5 $self->cursor_hidden(1);
104 5 return $self->_print("\e[?25l");
105 }
106  
107 8 ▶sub show_cursor {
Subroutine show_cursor: called CC=2 SCAR=6.9
Pod documented
108 8 my ($self) = @_;
109 8 ▶ return $self unless $self->cursor_hidden;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cursor_hidden 3 5 8
110 5 $self->cursor_hidden(0);
111 5 return $self->_print("\e[?25h");
112 }
113  
114 11 ▶sub restore {
Subroutine restore: called CC=3 SCAR=11.0
Pod documented
115 11 my ($self) = @_;
116 11 ▶ return $self unless $self->cursor_hidden;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cursor_hidden 7 4 11
117 4 $self->show_cursor;
118 4 ▶ $self->_print("\e[0m") if $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
if $self->colour 1 3 4
119 4 return $self;
120 }
121  
122 18 ▶sub measured {
Subroutine measured: called CC=11 SCAR=27.4
Pod documented
123 18 my ($self) = @_;
124 18 ▶ return ($self->columns, $self->rows || 0) if $self->columns;
Branch: 1 branch
BranchTrueFalseTotal
if $self->columns 7 11 18
Condition100%2/2
$self->rows || 0
l!l
61
MC/DC100%1/1
$self->rows || 0
$self->rows
Truth table100%2/2
A
A= $self->rows
Aresult
11
00
125 11 ▶ if ($self->interactive && eval { require Term::ReadKey; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->interactive and eval { do { require Term::ReadKey; 1 } }) 0 11 11
Condition66%2/3
$self->interactive and eval { do { require Term::ReadKey; 1 } }
!ll&&!rl&&r
920
MC/DC0%0/2
$self->interactive and eval { do { require Term::ReadKey; 1 } }
$self->interactive eval { do { require Term::ReadKey; 1 } }
Truth table66%2/3
A and B
A= $self->interactiveB= eval { do { require Term::ReadKey; 1 } }
ABresult
0X0
100
111
126 0 my ($columns, $rows) = eval { Term::ReadKey::GetTerminalSize($self->out) };
127 0 ▶ return ($columns, $rows || 0) if $columns;
Branch: 1 branch
BranchTrueFalseTotal
if $columns 0 0 0
Condition0%0/2
$rows || 0
l!l
00
MC/DC0%0/1
$rows || 0
$rows
Truth table0%0/2
A
A= $rows
Aresult
11
00
128 }
129 return ($ENV{COLUMNS} + 0, ($ENV{LINES} || 0) + 0)
130 11 ▶ if defined $ENV{COLUMNS} && $ENV{COLUMNS} =~ m/^\d+$/ && $ENV{COLUMNS} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"COLUMNS"} and $ENV{"COLUMNS"} =~ /^\d+$/u and $ENV{"COLUMNS"} > 0 3 8 11
Condition87%7/8
defined $ENV{"COLUMNS"} and $ENV{"COLUMNS"} =~ /^\d+$/u and $ENV{"COLUMNS"} > 0
!ll&&!rl&&r
803
defined $ENV{"COLUMNS"} and $ENV{"COLUMNS"} =~ /^\d+$/u
!ll&&!rl&&r
713
$ENV{'LINES'} || 0
l!l
12
MC/DC66%2/3
defined $ENV{"COLUMNS"} and $ENV{"COLUMNS"} =~ /^\d+$/u and $ENV{"COLUMNS"} > 0
defined $ENV{"COLUMNS"} $ENV{"COLUMNS"} =~ /^\d+$/u $ENV{"COLUMNS"} > 0
MC/DC100%1/1
$ENV{'LINES'} || 0
$ENV{'LINES'}
Truth table75%3/4
A and B and C
A= defined $ENV{"COLUMNS"}B= $ENV{"COLUMNS"} =~ /^\d+$/uC= $ENV{"COLUMNS"} > 0
ABCresult
0XX0
10X0
1100
1111
Truth table100%2/2
A
A= $ENV{'LINES'}
Aresult
11
00
131 8 return ();
132 }
133  
134 8 ▶sub size {
Subroutine size: called CC=3 SCAR=11.0
Pod documented
135 8 my ($self) = @_;
136 8 my ($columns, $rows) = $self->measured;
137 8 ▶ return ($columns || 80, $rows || 24);
Condition100%4/4
$columns || 80
l!l
44
$rows || 24
l!l
26
MC/DC100%1/1
$columns || 80
$columns
MC/DC100%1/1
$rows || 24
$rows
Truth table100%2/2
A
A= $columns
Aresult
11
00
Truth table100%2/2
A
A= $rows
Aresult
11
00
138 }
139  
140 7 ▶sub too_small {
Subroutine too_small: called CC=8 SCAR=20.8
Pod documented
141 7 my ($self, $need_columns, $need_rows) = @_;
142 7 my ($columns, $rows) = $self->measured;
143 7 ▶ return undef unless $columns;
Branch: 1 branch
BranchTrueFalseTotal
unless $columns 2 5 7
144 5 my $narrow = $columns < $need_columns;
145 5 ▶ my $short = $need_rows && $rows && $rows < $need_rows;
Condition83%5/6
$need_rows && $rows && $rows < $need_rows
!ll&&!rl&&r
221
$need_rows and $rows
!ll&&!rl&&r
203
MC/DC66%2/3
$need_rows && $rows && $rows < $need_rows
$need_rows $rows $rows < $need_rows
Truth table75%3/4
A and B && C
A= $need_rowsB= $rowsC= $rows < $need_rows
ABCresult
0XX0
10X0
1100
1111
146 5 ▶ return undef unless $narrow || $short;
Branch: 1 branch
BranchTrueFalseTotal
unless $narrow or $short 2 3 5
Condition100%3/3
$narrow or $short
l!l&&r!l&&!r
212
MC/DC100%2/2
$narrow or $short
$narrow $short
Truth table100%3/3
A or B
A= $narrowB= $short
ABresult
1X1
011
000
147 3 ▶ return "This window is $columns columns by $rows lines and $need_columns by $need_rows are needed."
Branch: 1 branch
BranchTrueFalseTotal
if $need_rows and $rows 2 1 3
Condition66%2/3
$need_rows and $rows
!ll&&!rl&&r
102
MC/DC50%1/2
$need_rows and $rows
$need_rows $rows
Truth table66%2/3
A and B
A= $need_rowsB= $rows
ABresult
0X0
100
111
148 if $need_rows && $rows;
149 1 return "This window is $columns columns wide and $need_columns are needed.";
150 }
151  
152 11 1;
153  
154 __END__
« Kit/Line.pm ↑ Summary Kit/Keys.pm »