blib/lib/Circle/WindowItem.pm

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