Term/Widget/Scroller.pm

filter: Statementstmt 22.7% 35 / 154 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 48.0% 12 / 25 Podpod n/a totaltotal 17.3% 47 / 271 CC 43 SCAR 50.7
CC
43
Cov
15%
_apply_formatting
462.0
insert_event
72.0
maybe_request_more
30.0
CRAP 158.9
↑ Summary Term.pm »
1 # You may distribute under the terms of the GNU General Public License
2 #
3 # (C) Paul Evans, 2010-2023 -- leonerd@leonerd.org.uk
4  
5 package Circle::FE::Term::Widget::Scroller 0.240250;
6  
7 1 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use constant type => "Scroller";
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Syntax::Keyword::Match;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Circle::FE::Term;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use Convert::Color 0.06;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Convert::Color::XTerm;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use POSIX qw( strftime );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use String::Tagged;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Text::Balanced qw( extract_bracketed );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Tangence::ObjectProxy '0.16'; # watch with iterators
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # Guess that we can do 256 colours on xterm or any -256color terminal
24 my $AS_TERM = ( $ENV{TERM} eq "xterm" or $ENV{TERM} =~ m/-256color$/ ) ? "as_xterm" : "as_vga";
25  
26 sub build
27 0 ▶{
Subroutine build: not called CC=1 SCAR=6.9
28 0 my $class = shift;
29 0 my ( $obj, $tab ) = @_;
30  
31 0 my $widget = Circle::FE::Term::Widget::Scroller::Widget->new(
32 classes => $obj->prop( "classes" ),
33 gravity => "bottom",
34 );
35  
36 0 my $self = bless {
37 tab => $tab,
38 widget => $widget,
39 last_datestamp => "",
40 last_datestamp_top => "",
41 };
42  
43 0 ▶ $widget->set_on_scrolled( sub { $self->maybe_request_more if $_[1] < 0 } );
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] < 0 0 0 0
44  
45 $tab->adopt_future(
46 $obj->watch_property_with_iter(
47 "displayevents", "last",
48 0 ▶ on_set => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 die "This should not happen\n";
50 },
51 0 ▶ on_push => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
52 0 $self->insert_event( bottom => $_ ) for @_;
53 },
54 0 ▶ on_shift => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
55 0 my ( $count ) = @_;
56 0 $count -= $self->{iter_idx};
57 0 ▶ $widget->shift( $count ) if $count > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count > 0 0 0 0
58 },
59 0 ▶ )->then( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 0 ( $self->{iter}, undef, my $max ) = @_;
61 0 $self->{iter_idx} = $max + 1;
62  
63 0 $self->maybe_request_more;
64 })
65 0 );
66  
67 0 return $widget;
68 }
69  
70 sub maybe_request_more
71 0 ▶{
Subroutine maybe_request_more: not called CC=5 SCAR=34.0
72 0 my $self = shift;
73  
74 0 my $widget = $self->{widget};
75 0 my $idx = $self->{iter_idx};
76  
77 0 my $height = $widget->window->lines;
78  
79 0 ▶ return if $self->{iter_fetching};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"iter_fetching"} 0 0 0
80  
81 # Stop if we've got at least 2 screenfuls more, or we're out of things to iterate
82 0 ▶ if( $widget->lines_above > $height * 2 or !$idx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($widget->lines_above > $height * 2 or not $idx) 0 0 0
Condition0%0/3
$widget->lines_above > $height * 2 or not $idx
l!l&&r!l&&!r
000
MC/DC0%0/2
$widget->lines_above > $height * 2 or not $idx
$widget->lines_above > $height * 2 not $idx
Truth table0%0/3
A or not(B)
A= $widget->lines_above > $height * 2B= not $idx
ABresult
1X1
011
000
83 0 $widget->set_loading( 0 );
84 0 return;
85 }
86  
87 0 my $more = $height * 3;
88 0 ▶ $more = $idx if $more > $idx;
Branch: 1 branch
BranchTrueFalseTotal
if $more > $idx 0 0 0
89  
90 0 $self->{iter_fetching} = 1;
91 0 $widget->set_loading( 1 );
92  
93 my $f = $self->{iter}->next_backward( $more )
94 0 ▶ ->on_done( sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
95 0 ( $self->{iter_idx}, my @more ) = @_;
96  
97 0 $self->{iter_fetching} = 0;
98  
99 0 $self->insert_event( top => $_ ) for reverse @more;
100 0 $self->maybe_request_more;
101 0 });
102  
103 0 $self->{tab}->adopt_future( $f );
104 }
105  
106 sub insert_event
107 0 ▶{
Subroutine insert_event: not called CC=8 SCAR=42.8
108 0 my $self = shift;
109 0 my ( $end, $ev ) = @_;
110  
111 0 my ( $event, $time, $args ) = @$ev;
112  
113 0 my $tab = $self->{tab};
114  
115 0 my @time = localtime( $time );
116  
117 0 my $datestamp = strftime( Circle::FE::Term->get_theme_var( "datestamp" ), @time );
118 0 my $timestamp = strftime( Circle::FE::Term->get_theme_var( "timestamp" ), @time );
119  
120 0 my $format = Circle::FE::Term->get_theme_var( $event );
121 0 ▶ defined $format or $format = "No format defined for event $event";
Branch: 1 branch
BranchTrueFalseTotal
defined $format or $format = "No format defined for event $event" 0 0 0
122  
123 0 my @items = ( $self->format_event( $timestamp . $format, $args ) );
124  
125 0 my $widget = $self->{widget};
126 match( $end : eq ) {
127 case( "bottom" ) {
128 unshift @items, $self->format_event( Circle::FE::Term->get_theme_var( "datemessage" ), { datestamp => $datestamp } )
129 0 ▶ if $datestamp ne $self->{last_datestamp};
Branch: 1 branch
BranchTrueFalseTotal
if $datestamp ne $self->{"last_datestamp"} 0 0 0
130  
131 0 $widget->push( @items );
132 0 ▶ $self->{last_datestamp} = $datestamp;
Condition0%0/3
$(Syntax::Keyword::Match/topic) eq "top" and do { push @items, $self->format_event("Circle::FE::Term"->get_theme_var("datemessage"), {"datestamp", $self->{"last_datestamp_top"}}) if $datestamp ne $self->{"last_datestamp_top"} and length $self->{"last_datestamp_top"}; $widget->unshift(@items); $self->{"last_datestamp_top"} = $datestamp; $self->{"last_datestamp"} = $datestamp if not length $self->{"last_datestamp"} }
!ll&&!rl&&r
000
MC/DC0%0/2
$(Syntax::Keyword::Match/topic) eq "top" and do { push @items, $self->format_event("Circle::FE::Term"->get_theme_var("datemessage"), {"datestamp", $self->{"last_datestamp_top"}}) if $datestamp ne $self->{"last_datestamp_top"} and length $self->{"last_datestamp_top"}; $widget->unshift(@items); $self->{"last_datestamp_top"} = $datestamp; $self->{"last_datestamp"} = $datestamp if not length $self->{"last_datestamp"} }
$(Syntax::Keyword::Match/topic) eq "top" do { push @items, $self->format_event("Circle::FE::Term"->get_theme_var("datemessage"), {"datestamp", $self->{"last_datestamp_top"}}) if $datestamp ne $self->{"last_datestamp_top"} and length $self->{"last_datestamp_top"}; $widget->unshift(@items); $self->{"last_datestamp_top"} = $datestamp; $self->{"last_datestamp"} = $datestamp if not length $self->{"last_datestamp"} }
Truth table0%0/3
A and B
A= $(Syntax::Keyword::Match/topic) eq "top"B= do { push @items, $self->format_event("Circle::FE::Term"->get_theme_var("datemessage"), {"datestamp", $self->{"last_datestamp_top"}}) if $datestamp ne $self->{"last_datestamp_top"} and length $self->{"last_datestamp_top"}; $widget->unshift(@items); $self->{"last_datestamp_top"} = $datestamp; $self->{"last_datestamp"} = $datestamp if not length $self->{"last_datestamp"} }
ABresult
0X0
100
111
133 }
134 0 ▶ case( "top" ) {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'bottom' ? : 0 0 0
135 push @items, $self->format_event( Circle::FE::Term->get_theme_var( "datemessage" ), { datestamp => $self->{last_datestamp_top} } )
136 0 ▶ if $datestamp ne $self->{last_datestamp_top} and length $self->{last_datestamp_top};
Branch: 1 branch
BranchTrueFalseTotal
if $datestamp ne $self->{"last_datestamp_top"} and length $self->{"last_datestamp_top"} 0 0 0
Condition0%0/3
$datestamp ne $self->{"last_datestamp_top"} and length $self->{"last_datestamp_top"}
!ll&&!rl&&r
000
MC/DC0%0/2
$datestamp ne $self->{"last_datestamp_top"} and length $self->{"last_datestamp_top"}
$datestamp ne $self->{"last_datestamp_top"} length $self->{"last_datestamp_top"}
Truth table0%0/3
A and B
A= $datestamp ne $self->{"last_datestamp_top"}B= length $self->{"last_datestamp_top"}
ABresult
0X0
100
111
137  
138 0 $widget->unshift( @items );
139 0 $self->{last_datestamp_top} = $datestamp;
140 0 ▶ $self->{last_datestamp} = $datestamp if !length $self->{last_datestamp};
Branch: 1 branch
BranchTrueFalseTotal
if not length $self->{"last_datestamp"} 0 0 0
141 }
142 }
143 }
144  
145 sub format_event
146 0 ▶{
Subroutine format_event: not called CC=3 SCAR=24.8
147 0 my $self = shift;
148 0 my ( $format, $args ) = @_;
149  
150 0 my $str = String::Tagged->new();
151 0 $self->_apply_formatting( $format, $args, $str );
152  
153 0 my $indent = 4;
154 0 ▶ if( grep { $_ eq "indent" } $str->tagnames and
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "indent";} $str->tagnames and my $extent = $str->get_tag_missing_extent(0, "indent")) 0 0 0
Condition0%0/3
grep {$_ eq "indent";} $str->tagnames and my $extent = $str->get_tag_missing_extent(0, "indent")
!ll&&!rl&&r
000
MC/DC0%0/2
grep {$_ eq "indent";} $str->tagnames and my $extent = $str->get_tag_missing_extent(0, "indent")
grep {$_ eq "indent";} $str->tagnames my $extent = $str->get_tag_missing_extent(0, "indent")
Truth table0%0/3
A and B
A= grep {$_ eq "indent";} $str->tagnamesB= my $extent = $str->get_tag_missing_extent(0, "indent")
ABresult
0X0
100
111
155 my $extent = $str->get_tag_missing_extent( 0, "indent" ) ) {
156 # TODO: Should use textwidth not just char. count
157 0 $indent = $extent->end;
158 }
159  
160 0 return Tickit::Widget::Scroller::Item::RichText->new( $str, indent => $indent );
161 }
162  
163 my %colourcache;
164 sub _convert_colour
165 0 ▶{
Subroutine _convert_colour: not called CC=3 SCAR=24.8
166 0 my $self = shift;
167 0 my ( $colspec ) = @_;
168  
169 0 ▶ return undef if !defined $colspec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $colspec 0 0 0
170  
171 0 ▶ return $colourcache{$colspec} ||= sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
172 0 ▶ return Convert::Color->new( "rgb8:$1$1$2$2$3$3" )->$AS_TERM->index if $colspec =~ m/^#([0-9A-F])([0-9A-F])([0-9A-F])$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $colspec =~ /^#([0-9A-F])([0-9A-F])([0-9A-F])$/iu 0 0 0
173 0 ▶ return Convert::Color->new( "rgb8:$1$2$3" )->$AS_TERM->index if $colspec =~ m/^#([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $colspec =~ /^#([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/iu 0 0 0
174 0 ▶ return Convert::Color->new( "vga:$colspec" )->index if $colspec =~ m/^[a-z]+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $colspec =~ /^[a-z]+$/u 0 0 0
175  
176 0 print STDERR "TODO: Unknown colour spec $colspec\n";
177 0 6; # TODO
178 0 ▶ }->();
Condition0%0/3
$colourcache{$colspec} ||= sub { return "Convert::Color"->new("rgb8:$1$1$2$2$3$3")->$AS_TERM->index if $colspec =~ /^#([0-9A-F])([0-9A-F])([0-9A-F])$/iu; return "Convert::Color"->new("rgb8:$1$2$3")->$AS_TERM->index if $colspec =~ /^#([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/iu; return "Convert::Color"->new("vga:$colspec")->index if $colspec =~ /^[a-z]+$/u; print STDERR "TODO: Unknown colour spec $colspec\n"; 6; } ->()
l!l&&r!l&&!r
000
MC/DC0%0/2
$colourcache{$colspec} ||= sub { return "Convert::Color"->new("rgb8:$1$1$2$2$3$3")->$AS_TERM->index if $colspec =~ /^#([0-9A-F])([0-9A-F])([0-9A-F])$/iu; return "Convert::Color"->new("rgb8:$1$2$3")->$AS_TERM->index if $colspec =~ /^#([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/iu; return "Convert::Color"->new("vga:$colspec")->index if $colspec =~ /^[a-z]+$/u; print STDERR "TODO: Unknown colour spec $colspec\n"; 6; } ->()
$colourcache{$colspec} sub { return "Convert::Color"->new("rgb8:$1$1$2$2$3$3")->$AS_TERM->index if $colspec =~ /^#([0-9A-F])([0-9A-F])([0-9A-F])$/iu; return "Convert::Color"->new("rgb8:$1$2$3")->$AS_TERM->index if $colspec =~ /^#([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/iu; return "Convert::Color"->new("vga:$colspec")->index if $colspec =~ /^[a-z]+$/u; print STDERR "TODO: Unknown colour spec $colspec\n"; 6; } ->()
Truth table0%0/3
A ||= B
A= $colourcache{$colspec}B= sub { return "Convert::Color"->new("rgb8:$1$1$2$2$3$3")->$AS_TERM->index if $colspec =~ /^#([0-9A-F])([0-9A-F])([0-9A-F])$/iu; return "Convert::Color"->new("rgb8:$1$2$3")->$AS_TERM->index if $colspec =~ /^#([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/iu; return "Convert::Color"->new("vga:$colspec")->index if $colspec =~ /^[a-z]+$/u; print STDERR "TODO: Unknown colour spec $colspec\n"; 6; } ->()
ABresult
1X1
011
000
179 }
180  
181 sub _apply_formatting
182 0 ▶{
Subroutine _apply_formatting: not called CC=21 SCAR=61.4
183 0 my $self = shift;
184 0 my ( $format, $args, $str ) = @_;
185  
186 0 ▶ while( length $format ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $format) 0 0 0
187 0 ▶ if( $format =~ s/^\$(\w+)// ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($format =~ s/^\$(\w+)//u) { } 0 0 0
elsif ($format =~ /^\{/u) { } 0 0 0
188 0 ▶ my $val = exists $args->{$1} ? $args->{$1} : "<No such variable $1>";
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{$1} ? : 0 0 0
189 0 ▶ defined $val or $val = "<Variable $1 is not defined>";
Branch: 1 branch
BranchTrueFalseTotal
defined $val or $val = "<Variable $1 is not defined>" 0 0 0
190  
191 0 ▶ my @parts = ref $val eq "ARRAY" ? @$val : ( $val );
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 0 0 0
192  
193 0 my $is_initial = 1;
194 0 my $needs_linefeed;
195  
196 0 foreach my $part ( @parts ) {
197 0 ▶ my ( $text, %format ) = ref $part eq "ARRAY" ? @$part : ( $part );
Branch: 1 branch
BranchTrueFalseTotal
ref $part eq 'ARRAY' ? : 0 0 0
198  
199 0 ▶ $str->append( "\n" ) if $needs_linefeed; $needs_linefeed = 0;
Branch: 1 branch
BranchTrueFalseTotal
if $needs_linefeed 0 0 0
200  
201 # Convert some tags
202 0 ▶ if( delete $format{m} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $format{"m"}) 0 0 0
203 # Monospace
204 0 $format{af} = 1;
205 0 $format{bg} = "#303030";
206 }
207 0 ▶ if( delete $format{bq} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $format{"bq"}) 0 0 0
208 # Quoted text
209 0 $format{bg} = "#303030";
210 0 $format{fg} = "#00C0C0";
211  
212 # blockquotes get to be on their own line, with "> " prefixed on each
213 0 $text = join( "\n", map { "> $_" } split m/\n/, $text );
214  
215 # surround the text by linefeeds
216 0 ▶ $str->append( "\n" ) if !$is_initial;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_initial 0 0 0
217 0 $needs_linefeed++;
218 }
219  
220 # Tickit::Widget::Scroller::Item::Text doesn't like C0, C1 or DEL
221 # control characters. Replace them with U+FFFD
222 # Be sure to leave linefeed alone
223 0 $text =~ s/[\x00-\x09\x0b-\x1f\x80-\x9f\x7f]/\x{fffd}/g;
224  
225 0 foreach (qw( fg bg )) {
226 0 ▶ defined $format{$_} or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $format{$_} or next 0 0 0
227 0 $format{$_} = $self->_convert_colour( Circle::FE::Term->translate_theme_colour( $format{$_} ) );
228 }
229  
230 0 $str->append_tagged( $text, %format );
231  
232 0 $is_initial = 0;
233 }
234 }
235 elsif( $format =~ m/^\{/ ) {
236 0 my $piece = extract_bracketed( $format, "{}" );
237 0 s/^{//, s/}$// for $piece;
238  
239 0 ▶ if( $piece =~ m/^\?\$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($piece =~ /^\?\$/u) { } 0 0 0
elsif ($piece =~ / /u) { } 0 0 0
240 # A conditional expansion in three parts
241 # {?$varname|IFTRUE|IFFALSE}
242 0 my ( $varname, $iftrue, $iffalse ) = split( m/\|/, $piece, 3 );
243 0 $varname =~ s/^\?\$//;
244  
245 0 ▶ if( defined $args->{$varname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{$varname}) { } 0 0 0
246 0 $self->_apply_formatting( $iftrue, $args, $str );
247 }
248 else {
249 0 $self->_apply_formatting( $iffalse, $args, $str );
250 }
251 }
252 elsif( $piece =~ m/ / ) {
253 0 my ( $code, $content ) = split( m/ /, $piece, 2 );
254  
255 0 my ( $type, $arg ) = split( m/:/, $code, 2 );
256  
257 0 my $start = length $str->str;
258  
259 0 $self->_apply_formatting( $content, $args, $str );
260  
261 0 my $end = length $str->str;
262  
263 0 ▶ $arg = $self->_convert_colour( $arg ) if $type eq "fg" or $type eq "bg";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "fg" or $type eq "bg" 0 0 0
Condition0%0/3
$type eq "fg" or $type eq "bg"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "fg" or $type eq "bg"
$type eq "fg" $type eq "bg"
Truth table0%0/3
A or B
A= $type eq "fg"B= $type eq "bg"
ABresult
1X1
011
000
264 0 $str->apply_tag( $start, $end - $start, $type => $arg );
265 }
266 else {
267 0 $self->_apply_formatting( $piece, $args, $str );
268 }
269 }
270 else {
271 0 $format =~ s/^([^\$\{]+)//;
272 0 my $val = $1;
273 0 $str->append( $val );
274 }
275 }
276 }
277  
278 package Circle::FE::Term::Widget::Scroller::Widget;
279  
280 1 ▶use base qw( Tickit::Widget::Scroller );
Subroutine BEGIN: called CC=1 SCAR=0.0
281 Tickit::Widget::Scroller->VERSION( 0.15 ); # on_scrolled
282 use Tickit::Widget::Scroller::Item::RichText;
283  
284 sub new
285 {
286 my $class = shift;
287 return $class->SUPER::new( @_,
288 gen_bottom_indicator => "gen_bottom_indicator",
289 gen_top_indicator => "gen_top_indicator",
290 );
291 }
292  
293 sub clear_lines
294 {
295 my $self = shift;
296  
297 undef @{ $self->{lines} };
298  
299 my $window = $self->window or return;
300 $window->clear;
301 $window->restore;
302 }
303  
304 sub push
305 {
306 my $self = shift;
307 my $below_before = $self->lines_below;
308 $self->SUPER::push( @_ );
309 if( $below_before ) {
310 $self->{more_count} += $self->lines_below - $below_before;
311 $self->update_indicators;
312 }
313 }
314  
315 sub set_loading
316 {
317 my $self = shift;
318 my ( $loading ) = @_;
319  
320 return if $loading == ( $self->{loading} // 0 );
321  
322 $self->{loading} = $loading;
323 $self->update_indicators;
324 }
325  
326 sub gen_bottom_indicator
327 {
328 my $self = shift;
329 my $below = $self->lines_below;
330 if( !$below ) {
331 undef $self->{more_count};
332 return;
333 }
334  
335 if( $self->{more_count} ) {
336 return sprintf "-- +%d [%d more] --", $below - $self->{more_count}, $self->{more_count};
337 }
338 else {
339 return sprintf "-- +%d --", $below;
340 }
341 }
342  
343 sub gen_top_indicator
344 {
345 my $self = shift;
346 return $self->{loading} ? " Loading... " : undef;
347 }
348  
349 0x55AA;
↑ Summary Term.pm »