Circle/WindowItem.pm

filter: Statementstmt 53.1% 68 / 128 Branchbran 35.2% 12 / 34 Conditioncond 30.0% 3 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 68.0% 17 / 25 Podpod 0.0% 0 / 15 totaltotal 45.8% 100 / 218 CC 28 SCAR 38.8
CC
28
Cov
48%
respond_table
182.0
command_clear
12.0
respondwarn
6.0
CRAP 48.2
« Circle/GlobalRules.pm ↑ Summary Circle/Loggable.pm »
1 # You may distribute under the terms of the GNU General Public License
2 #
3 # (C) Paul Evans, 2008-2014 -- leonerd@leonerd.org.uk
4  
5 package Circle::WindowItem;
6  
7 # An abstract role used by objects that should be placed in FE windows or tabs
8 # Combines the behaviours of:
9 # having display events
10 # responding to typed lines of text
11  
12 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use base qw( Circle::Commandable Circle::Configurable Circle::Loggable );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 our $VERSION = '0.173320';
19  
20 4 ▶use Circle::TaggedString;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 4 ▶use Circle::Widget::Box;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Circle::Widget::Scroller;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 sub init_prop_level
26 5 ▶{
Subroutine init_prop_level: called CC=1 SCAR=0.0
Pod undocumented
27 5 return 0;
28 }
29  
30 sub bump_level
31 3 ▶{
Subroutine bump_level: called CC=2 SCAR=6.9
Pod undocumented
32 3 my $self = shift;
33 3 my ( $newlevel ) = @_;
34  
35 3 ▶ return if $self->get_prop_level >= $newlevel;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_prop_level >= $newlevel 1 2 3
36  
37 2 $self->set_prop_level( $newlevel );
38 }
39  
40 sub method_reset_level
41 0 ▶{
Subroutine method_reset_level: not called CC=1 SCAR=6.9
Pod undocumented
42 0 my $self = shift;
43  
44 0 $self->set_prop_level( 0 );
45 }
46  
47 sub push_displayevent
48 8 ▶{
Subroutine push_displayevent: called CC=5 SCAR=16.3
Pod undocumented
49 8 my $self = shift;
50 8 my ( $event, $args, %opts ) = @_;
51  
52 8 foreach ( values %$args ) {
53 8 ▶ if( !ref $_ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $_) { } 5 3 8
elsif (eval { do { $_->isa('Circle::TaggedString') } }) { } 3 0 3
54 5 next;
55 }
56 3 elsif( eval { $_->isa( "Circle::TaggedString" ) } ) {
57 3 $_ = $_->squash;
58 }
59 else {
60 0 $_ = "[[TODO: Not sure how to handle $_]]";
61 }
62 }
63  
64 8 ▶ my $time = $opts{time} // time();
Condition50%1/2
$opts{"time"} // time
l!l
08
MC/DC0%0/1
$opts{"time"} // time
$opts{"time"}
Truth table50%1/2
A
A= $opts{"time"}
Aresult
11
00
65  
66 8 my $scroller = $self->get_widget_scroller;
67 8 $scroller->push_event( $event, $time, $args );
68  
69 8 $self->push_log( $event, $time, $args );
70 }
71  
72 sub respond
73 5 ▶{
Subroutine respond: called CC=2 SCAR=6.9
Pod undocumented
74 5 my $self = shift;
75 5 my ( $text, %opts ) = @_;
76  
77 5 $self->push_displayevent( "response", { text => $text } );
78 5 ▶ $self->bump_level( $opts{level} ) if $opts{level};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"level"} 1 4 5
79  
80 5 return;
81 }
82  
83 sub respondwarn
84 0 ▶{
Subroutine respondwarn: not called CC=2 SCAR=17.9
Pod undocumented
85 0 my $self = shift;
86 0 my ( $text, %opts ) = @_;
87  
88 0 $self->push_displayevent( "warning", { text => $text } );
89 0 ▶ $self->bump_level( $opts{level} ) if $opts{level};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"level"} 0 0 0
90  
91 0 return;
92 }
93  
94 sub responderr
95 0 ▶{
Subroutine responderr: not called CC=2 SCAR=17.9
Pod undocumented
96 0 my $self = shift;
97 0 my ( $text, %opts ) = @_;
98  
99 0 $self->push_displayevent( "error", { text => $text } );
100 0 ▶ $self->bump_level( $opts{level} ) if $opts{level};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"level"} 0 0 0
101  
102 0 return;
103 }
104  
105 sub respond_table
106 0 ▶{
Subroutine respond_table: not called CC=13 SCAR=52.0
Pod undocumented
107 0 my $self = shift;
108 0 my ( $tableref, %opts ) = @_;
109  
110 # We need to avoid using join() or sprintf() here, because any of the table
111 # cell arguments might be TaggedString objects. The CORE functions won't
112 # respect this taggnig.
113  
114 0 ▶ my $colsep = exists $opts{colsep} ? delete $opts{colsep} : " ";
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'colsep'} ? : 0 0 0
115  
116 0 my $headings = delete $opts{headings};
117  
118 0 my @table = @$tableref;
119  
120 0 my @width;
121  
122 0 foreach my $r ( $headings, @table ) {
123 0 ▶ next unless defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 0 0 0
124  
125 0 foreach my $c ( 0 .. $#$r ) {
126 0 my $d = $r->[$c];
127 0 ▶ $width[$c] = length $d if !defined $width[$c] or length $d > $width[$c];
Branch: 1 branch
BranchTrueFalseTotal
if not defined $width[$c] or length $d > $width[$c] 0 0 0
Condition0%0/3
not defined $width[$c] or length $d > $width[$c]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $width[$c] or length $d > $width[$c]
not defined $width[$c] length $d > $width[$c]
Truth table0%0/3
not(A) or B
A= not defined $width[$c]B= length $d > $width[$c]
ABresult
1X1
011
000
128 }
129 }
130  
131 0 ▶ if( $headings ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headings) 0 0 0
132 0 my $text = Circle::TaggedString->new();
133 0 foreach my $c ( 0 .. $#$headings ) {
134 0 ▶ $text->append( $colsep ) if $c > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $c > 0 0 0 0
135  
136 0 my $col = $headings->[$c];
137 0 $text->append_tagged( $col . ( " " x ( $width[$c] - length $col ) ),
138 u => 1 );
139 }
140 0 $self->respond( $text, %opts );
141 }
142  
143 0 foreach my $tr ( @table ) {
144 0 my $text = Circle::TaggedString->new();
145 0 foreach my $c ( 0 .. $#width ) {
146 0 ▶ $text->append( $colsep ) if $c > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $c > 0 0 0 0
147  
148 0 my $col = $tr->[$c];
149 0 $text->append( $col . ( " " x ( $width[$c] - length $col ) ) );
150 }
151 0 $self->respond( $text, %opts );
152 }
153 }
154  
155 sub command_clear
156 0 ▶ : Command_description("Clear the scrollback buffer")
Subroutine command_clear: not called CC=3 SCAR=24.8
Pod undocumented
157 : Command_opt('keeplines=$', desc => "keep this number of lines")
158 {
159 0 my $self = shift;
160 0 my ( $opts, $cinv ) = @_;
161  
162 0 ▶ my $keeplines = $opts->{keeplines} || 0;
Condition0%0/2
$opts->{'keeplines'} || 0
l!l
00
MC/DC0%0/1
$opts->{'keeplines'} || 0
$opts->{'keeplines'}
Truth table0%0/2
A
A= $opts->{'keeplines'}
Aresult
11
00
163  
164 0 my $scroller = $self->get_widget_scroller;
165  
166 0 my $to_delete = scalar @{ $scroller->get_prop_displayevents } - $keeplines;
167  
168 0 ▶ $scroller->shift_prop_displayevents( $to_delete ) if $to_delete > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $to_delete > 0 0 0 0
169  
170 0 return;
171 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
172  
173 sub command_dumpevents
174 0 ▶ : Command_description("Dump a log of the raw event buffer")
Subroutine command_dumpevents: not called CC=1 SCAR=6.9
Pod undocumented
175 : Command_arg('filename')
176 {
177 0 my $self = shift;
178 0 my ( $filename, $cinv ) = @_;
179  
180 0 my $scroller = $self->get_widget_scroller;
181 0 YAML::DumpFile( $filename, $scroller->get_prop_displayevents );
182  
183 0 $cinv->respond( "Dumped event log to $filename" );
184 0 return;
185 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
186  
187 ###
188 # Widget
189 ###
190  
191 sub method_get_widget
192 3 ▶{
Subroutine method_get_widget: called CC=2 SCAR=7.1
Pod undocumented
193 3 my $self = shift;
194  
195 3 ▶ return $self->{widget} ||= $self->make_widget();
Condition66%2/3
$self->{"widget"} ||= $self->make_widget
l!l&&r!l&&!r
120
MC/DC0%0/2
$self->{"widget"} ||= $self->make_widget
$self->{"widget"} $self->make_widget
Truth table66%2/3
A ||= B
A= $self->{"widget"}B= $self->make_widget
ABresult
1X1
011
000
196 }
197  
198 # Useful for debugging and live-development
199 sub command_rewidget
200 0 ▶ : Command_description("Destroy the cached widget tree so it will be recreated")
Subroutine command_rewidget: not called CC=1 SCAR=6.9
Pod undocumented
201 {
202 0 my $self = shift;
203  
204 0 delete $self->{widget};
205 0 $self->respond( "Destroyed existing widget tree. You will have to restart the frontend now" );
206  
207 0 return;
208 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
209  
210 # Subclasses might override this, but we'll provide a default
211 sub make_widget
212 2 ▶{
Subroutine make_widget: called CC=3 SCAR=11.0
Pod undocumented
213 2 my $self = shift;
214  
215 2 my $registry = $self->{registry};
216  
217 2 my $box = $registry->construct(
218 "Circle::Widget::Box",
219 orientation => "vertical",
220 );
221  
222 2 ▶ $self->make_widget_pre_scroller( $box ) if $self->can( "make_widget_pre_scroller" );
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("make_widget_pre_scroller") 0 2 2
223  
224 2 $box->add( $self->get_widget_scroller, expand => 1 );
225  
226 2 ▶ $box->add( $self->get_widget_statusbar ) if $self->can( "get_widget_statusbar" );
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("get_widget_statusbar") 1 1 2
227  
228 2 $box->add( $self->get_widget_commandentry );
229  
230 2 return $box;
231 }
232  
233 sub get_widget_scroller
234 11 ▶{
Subroutine get_widget_scroller: called CC=2 SCAR=6.9
Pod undocumented
235 11 my $self = shift;
236  
237 11 ▶ return $self->{widget_displayevents} if defined $self->{widget_displayevents};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"widget_displayevents"} 7 4 11
238  
239 4 my $registry = $self->{registry};
240  
241 4 my $widget = $registry->construct(
242 "Circle::Widget::Scroller",
243 scrollback => 1000, # TODO
244 );
245  
246 4 return $self->{widget_displayevents} = $widget;
247 }
248  
249 sub enumerable_path
250 0 ▶{
Subroutine enumerable_path: not called CC=2 SCAR=17.9
Pod undocumented
251 0 my $self = shift;
252  
253 0 ▶ if( my $parent = $self->parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $self->parent) { } 0 0 0
254 0 return $parent->enumerable_path . "/" . $self->enumerable_name;
255 }
256 else {
257 0 return $self->enumerable_name;
258 }
259 }
260  
261 4 0x55AA;
« Circle/GlobalRules.pm ↑ Summary Circle/Loggable.pm »