diff options
author | gregor herrmann <gregoa@debian.org> | 2023-09-02 16:38:52 +0200 |
---|---|---|
committer | gregor herrmann <gregoa@debian.org> | 2023-09-02 16:38:52 +0200 |
commit | 96f8e2b8e7fc5abbdc220748545b1627855f3932 (patch) | |
tree | 2a116b800efafa28e9f783dd5d695ee9858002f8 | |
parent | bca9d190c689810c1ef97a16c23c1728d078fa11 (diff) | |
parent | 2d308e4721a575e15d7069e9f4c6c0e14683e42a (diff) |
Update upstream source from tag 'upstream/0.31'
Update to upstream version '0.31'
with Debian dir d68280195e290b95d115fccd990aad8cda133bd1
-rw-r--r-- | .editorconfig | 4 | ||||
-rw-r--r-- | Build.PL | 3 | ||||
-rw-r--r-- | Changes | 16 | ||||
-rw-r--r-- | LICENSE | 6 | ||||
-rw-r--r-- | MANIFEST | 2 | ||||
-rw-r--r-- | META.json | 12 | ||||
-rw-r--r-- | META.yml | 12 | ||||
-rw-r--r-- | README | 12 | ||||
-rw-r--r-- | examples/richtext.pl | 2 | ||||
-rw-r--r-- | examples/text.pl | 2 | ||||
-rw-r--r-- | lib/Tickit/Widget/Scroller.pm | 65 | ||||
-rw-r--r-- | lib/Tickit/Widget/Scroller/Item/RichText.pm | 4 | ||||
-rw-r--r-- | lib/Tickit/Widget/Scroller/Item/Text.pm | 61 | ||||
-rw-r--r-- | t/00use.t | 10 | ||||
-rw-r--r-- | t/01item-text.t | 78 | ||||
-rw-r--r-- | t/02item-richtext.t | 31 | ||||
-rw-r--r-- | t/03item-richtext-formatting.t | 4 | ||||
-rw-r--r-- | t/04item-margins.t | 138 | ||||
-rw-r--r-- | t/10initial.t | 30 | ||||
-rw-r--r-- | t/11scroll.t | 16 | ||||
-rw-r--r-- | t/12resize-bottom.t | 4 | ||||
-rw-r--r-- | t/12resize-top.t | 4 | ||||
-rw-r--r-- | t/20push-bottom.t | 8 | ||||
-rw-r--r-- | t/20push-top.t | 8 | ||||
-rw-r--r-- | t/21shift-bottom.t | 8 | ||||
-rw-r--r-- | t/21shift-top.t | 8 | ||||
-rw-r--r-- | t/22unshift-bottom.t | 4 | ||||
-rw-r--r-- | t/22unshift-top.t | 4 | ||||
-rw-r--r-- | t/23pop-bottom.t | 4 | ||||
-rw-r--r-- | t/23pop-top.t | 4 | ||||
-rw-r--r-- | t/30indicator.t | 134 | ||||
-rw-r--r-- | t/99pod.t | 4 |
32 files changed, 493 insertions, 209 deletions
diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..24b6e3b --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +root = true + +[*.{pm,pl,t}] +indent_size = 3 @@ -1,3 +1,4 @@ +use v5; use strict; use warnings; @@ -16,7 +17,7 @@ my $build = Module::Build->new( 'Tickit::Pen' => '0.19', }, test_requires => { - 'Test::More' => '0.88', # done_testing + 'Test2::V0' => 0, 'Tickit::Test' => '0.12', }, auto_configure_requires => 0, # Don't add M::B to configure_requires @@ -1,5 +1,21 @@ Revision history for Tickit-Widget-Scroller +0.31 2023-08-30 + [CHANGES] + * Added `->items` accessor for querying the number of stored items + + [BUGFIXES] + * Force a redraw of the indicators after a resize + +0.30 2023-08-26 + [CHANGES] + * Use `Tickit::Style` to handle keybindings + * Add margin and pen parameters to Text items + * Swap all unit tests from `Test::More` to `Test2::V0` + + [BUGFIXES] + * Avoid warning from Text items with empty string + 0.29 2022-12-14 [CHANGES] * Updated for Object::Pad v0.73 @@ -1,4 +1,4 @@ -This software is copyright (c) 2022 by Paul Evans <leonerd@leonerd.org.uk>. +This software is copyright (c) 2023 by Paul Evans <leonerd@leonerd.org.uk>. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. @@ -12,7 +12,7 @@ b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- -This software is Copyright (c) 2022 by Paul Evans <leonerd@leonerd.org.uk>. +This software is Copyright (c) 2023 by Paul Evans <leonerd@leonerd.org.uk>. This is free software, licensed under: @@ -272,7 +272,7 @@ That's all there is to it! --- The Artistic License 1.0 --- -This software is Copyright (c) 2022 by Paul Evans <leonerd@leonerd.org.uk>. +This software is Copyright (c) 2023 by Paul Evans <leonerd@leonerd.org.uk>. This is free software, licensed under: @@ -1,3 +1,4 @@ +.editorconfig Build.PL Changes examples/richtext.pl @@ -15,6 +16,7 @@ t/00use.t t/01item-text.t t/02item-richtext.t t/03item-richtext-formatting.t +t/04item-margins.t t/10initial.t t/11scroll.t t/12resize-bottom.t @@ -4,7 +4,7 @@ "Paul Evans <leonerd@leonerd.org.uk>" ], "dynamic_config" : 1, - "generated_by" : "Module::Build version 0.4231", + "generated_by" : "Module::Build version 0.4234", "license" : [ "perl_5" ], @@ -27,7 +27,7 @@ }, "test" : { "requires" : { - "Test::More" : "0.88", + "Test2::V0" : "0", "Tickit::Test" : "0.12" } } @@ -35,15 +35,15 @@ "provides" : { "Tickit::Widget::Scroller" : { "file" : "lib/Tickit/Widget/Scroller.pm", - "version" : "0.29" + "version" : "0.31" }, "Tickit::Widget::Scroller::Item::RichText" : { "file" : "lib/Tickit/Widget/Scroller/Item/RichText.pm", - "version" : "0.29" + "version" : "0.31" }, "Tickit::Widget::Scroller::Item::Text" : { "file" : "lib/Tickit/Widget/Scroller/Item/Text.pm", - "version" : "0.29" + "version" : "0.31" } }, "release_status" : "stable", @@ -53,6 +53,6 @@ ], "x_IRC" : "irc://irc.freenode.net/#tickit" }, - "version" : "0.29", + "version" : "0.31", "x_serialization_backend" : "JSON::PP version 4.07" } @@ -3,10 +3,10 @@ abstract: 'a widget displaying a scrollable collection of' author: - 'Paul Evans <leonerd@leonerd.org.uk>' build_requires: - Test::More: '0.88' + Test2::V0: '0' Tickit::Test: '0.12' dynamic_config: 1 -generated_by: 'Module::Build version 0.4231, CPAN::Meta::Converter version 2.150010' +generated_by: 'Module::Build version 0.4234, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html @@ -15,13 +15,13 @@ name: Tickit-Widget-Scroller provides: Tickit::Widget::Scroller: file: lib/Tickit/Widget/Scroller.pm - version: '0.29' + version: '0.31' Tickit::Widget::Scroller::Item::RichText: file: lib/Tickit/Widget/Scroller/Item/RichText.pm - version: '0.29' + version: '0.31' Tickit::Widget::Scroller::Item::Text: file: lib/Tickit/Widget/Scroller/Item/Text.pm - version: '0.29' + version: '0.31' requires: Object::Pad: '0.74' String::Tagged: '0' @@ -33,5 +33,5 @@ requires: resources: IRC: irc://irc.freenode.net/#tickit license: http://dev.perl.org/licenses/ -version: '0.29' +version: '0.31' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' @@ -116,6 +116,16 @@ METHODS be clipped if this would scroll past the beginning or end of the display. + items + + $count = scalar $scroller->items; + + Since version 0.31. + + In scalar context, returns the number of items currently stored in the + Scroller. The behaviour of this method in non-scalar context is + currently unspecified. + push $scroller->push( @items ) @@ -286,8 +296,6 @@ TODO Implement more storage models, such as database-driven ones.. more dynamic. - * Keybindings - AUTHOR Paul Evans <leonerd@leonerd.org.uk> diff --git a/examples/richtext.pl b/examples/richtext.pl index 70be7d6..db31598 100644 --- a/examples/richtext.pl +++ b/examples/richtext.pl @@ -1,4 +1,4 @@ -use strict; +use v5.26; use warnings; use Tickit; diff --git a/examples/text.pl b/examples/text.pl index 00ceb35..dfbf532 100644 --- a/examples/text.pl +++ b/examples/text.pl @@ -1,4 +1,4 @@ -use strict; +use v5.26; use warnings; use Tickit; diff --git a/lib/Tickit/Widget/Scroller.pm b/lib/Tickit/Widget/Scroller.pm index 2db2c77..b2f374c 100644 --- a/lib/Tickit/Widget/Scroller.pm +++ b/lib/Tickit/Widget/Scroller.pm @@ -1,12 +1,13 @@ # You may distribute under the terms of either the GNU General Public License # or the Artistic License (the same terms as Perl itself) # -# (C) Paul Evans, 2011-2022 -- leonerd@leonerd.org.uk +# (C) Paul Evans, 2011-2023 -- leonerd@leonerd.org.uk use v5.26; # signatures +use warnings; use Object::Pad 0.73 ':experimental(adjust_params init_expr)'; -package Tickit::Widget::Scroller 0.29; +package Tickit::Widget::Scroller 0.31; class Tickit::Widget::Scroller :strict(params) :isa(Tickit::Widget); @@ -71,9 +72,17 @@ display =cut style_definition base => - indicator_rv => 1; + indicator_rv => 1, + "<Down>" => "scroll_down_line", + "<Up>" => "scroll_up_line", + "<PageDown>" => "scroll_down_halfpage", + "<PageUp>" => "scroll_up_halfpage", + "<C-Home>" => "scroll_to_top", + "<C-End>" => "scroll_to_bottom", + ; use constant WIDGET_PEN_FROM_STYLE => 1; +use constant KEYPRESSES_FROM_STYLE => 1; =head1 KEYBINDINGS @@ -149,7 +158,6 @@ field $_start_partial = 0; # accessor methods for t/30indicator.t to use # TODO: Should think about whether these should be made public -method _items { @_items } method _start_item { $_start_item } method _start_partial { $_start_partial } @@ -217,7 +225,7 @@ method reshape () $self->scroll_to_top; } - $self->update_indicators; + $self->update_indicators( 1 ); } method window_lost @@ -265,6 +273,20 @@ clipped if this would scroll past the beginning or end of the display. # generated accessors +=head2 items + + $count = scalar $scroller->items; + +I<Since version 0.31.> + +In scalar context, returns the number of items currently stored in the +Scroller. The behaviour of this method in non-scalar context is currently +unspecified. + +=cut + +method items { return scalar @_items; } + =head2 push $scroller->push( @items ) @@ -902,26 +924,17 @@ method render_to_rb ( $rb, $rect ) } } -my %bindings = ( - Down => sub { $_[0]->scroll( +1 ) }, - Up => sub { $_[0]->scroll( -1 ) }, +method key_scroll_to_top { $self->scroll_to_top } +method key_scroll_to_bottom { $self->scroll_to_bottom } - PageDown => sub { $_[0]->scroll( +int( $_[0]->window->lines / 2 ) ) }, - PageUp => sub { $_[0]->scroll( -int( $_[0]->window->lines / 2 ) ) }, +method key_scroll_up_line { $self->scroll( -1 ) } +method key_scroll_down_line { $self->scroll( +1 ) } - 'C-Home' => sub { $_[0]->scroll_to_top }, - 'C-End' => sub { $_[0]->scroll_to_bottom }, -); +method key_scroll_up_halfpage { $self->scroll( -int( $self->window->lines / 2 ) ) } +method key_scroll_down_halfpage { $self->scroll( +int( $self->window->lines / 2 ) ) } -method on_key ( $ev ) -{ - if( $ev->type eq "key" and my $code = $bindings{$ev->str} ) { - $code->( $self ); - return 1; - } - - return 0; -} +method key_scroll_up_page { $self->scroll( $self->window->lines ) } +method key_scroll_down_page { $self->scroll( $self->window->lines ) } method on_mouse ( $ev ) { @@ -981,7 +994,7 @@ return different text now. field %_indicator_win; field %_indicator_text; -method update_indicators () +method update_indicators ( $force = 0 ) { my $win = $self->window or return; @@ -992,7 +1005,7 @@ method update_indicators () my $text = $gen_indicator ? $self->$gen_indicator : undef; $text //= ""; - next if $text eq ( $_indicator_text{$edge} // "" ); + next if !$force and $text eq ( $_indicator_text{$edge} // "" ); $_indicator_text{$edge} = $text; @@ -1035,10 +1048,6 @@ method update_indicators () Abstract away the "item storage model" out of the actual widget. Implement more storage models, such as database-driven ones.. more dynamic. -=item * - -Keybindings - =back =cut diff --git a/lib/Tickit/Widget/Scroller/Item/RichText.pm b/lib/Tickit/Widget/Scroller/Item/RichText.pm index 85a8236..c101041 100644 --- a/lib/Tickit/Widget/Scroller/Item/RichText.pm +++ b/lib/Tickit/Widget/Scroller/Item/RichText.pm @@ -3,9 +3,11 @@ # # (C) Paul Evans, 2011-2021 -- leonerd@leonerd.org.uk +use v5.26; +use warnings; use Object::Pad 0.57; -package Tickit::Widget::Scroller::Item::RichText 0.29; +package Tickit::Widget::Scroller::Item::RichText 0.31; class Tickit::Widget::Scroller::Item::RichText :strict(params) :isa(Tickit::Widget::Scroller::Item::Text); diff --git a/lib/Tickit/Widget/Scroller/Item/Text.pm b/lib/Tickit/Widget/Scroller/Item/Text.pm index b5fd0e7..8a514cd 100644 --- a/lib/Tickit/Widget/Scroller/Item/Text.pm +++ b/lib/Tickit/Widget/Scroller/Item/Text.pm @@ -1,12 +1,13 @@ # You may distribute under the terms of either the GNU General Public License # or the Artistic License (the same terms as Perl itself) # -# (C) Paul Evans, 2011-2022 -- leonerd@leonerd.org.uk +# (C) Paul Evans, 2011-2023 -- leonerd@leonerd.org.uk use v5.26; +use warnings; use Object::Pad 0.70 ':experimental(adjust_params)'; -package Tickit::Widget::Scroller::Item::Text 0.29; +package Tickit::Widget::Scroller::Item::Text 0.31; class Tickit::Widget::Scroller::Item::Text :strict(params); @@ -55,23 +56,52 @@ The following options are recognised in C<%opts>: If the text item needs to wrap, indent the second and subsequent lines by this amount. Does not apply to the first line. +=item margin_left => INT + +=item margin_right => INT + +I<Since version 0.30.> + +A number of columns to leave blank at the left and right edge of the item. +This applies outside of any additional space added by C<indent>. + +=item margin => INT + +I<Since version 0.30.> + +Shortcut to setting both C<margin_left> and C<margin_right> to the same value. + +=item pen => Tickit::Pen + +A pen to set for rendering the item, including erasing its background. This +will not be set while erasing area required for its margin. + =back =cut sub BUILDARGS ( $class, $text, %opts ) { return ( text => $text, %opts ) } -field $_indent; +field $_indent :param = undef; +field $_margin_left = 0; +field $_margin_right = 0; field @_chunks; +field $_pen :param = undef; field $_width; # width for which the @_lineruns are valid field @_lineruns; ADJUST :params ( - :$indent = undef, - :$text = undef, + :$margin_left = undef, + :$margin_right = undef, + :$margin = undef, + :$text = undef, ) { - $_indent = $indent if defined $indent; + $margin_left //= $margin; + $margin_right //= $margin; + + $_margin_left = $margin_left if defined $margin_left; + $_margin_right = $margin_right if defined $margin_right; @_chunks = $self->_build_chunks_for( $text ); } @@ -110,6 +140,7 @@ following line. method _build_chunks_for ( $text ) { my @lines = split m/\n/, $text, -1; + @lines or @lines = ( "" ); # if blank my $lastline = pop @lines; return ( map { [ $_, textwidth( $_ ), linebreak => 1 ] } @lines ), [ $lastline, textwidth( $lastline ) ]; @@ -119,6 +150,9 @@ method chunks { @_chunks } method height_for_width ( $width ) { + # Just pretend the width doesn't include the margins + $width -= ( $_margin_left + $_margin_right ); + $_width = $width; my @chunks = $self->chunks; @@ -181,6 +215,12 @@ method render ( $rb, %args ) my $indent = ( $lineidx && $_indent ) ? $_indent : 0; $rb->goto( $lineidx, 0 ); + $rb->erase( $_margin_left ) if $_margin_left; + + if( $_pen ) { + $rb->savepen; + $rb->setpen( $_pen ); + } $rb->erase( $indent ) if $indent; foreach my $chunk ( $_lineruns[$lineidx]->@* ) { @@ -188,7 +228,14 @@ method render ( $rb, %args ) $rb->text( $text, $chunkpen ); } - $rb->erase_to( $cols ); + if( $_pen ) { + $rb->erase_to( $cols - $_margin_right ); + $rb->restore; + $rb->erase_to( $cols ) if $_margin_right; + } + else { + $rb->erase_to( $cols ); + } } } @@ -1,10 +1,14 @@ #!/usr/bin/perl -use strict; +use v5.26; use warnings; -use Test::More; +use Test2::V0; -use_ok( 'Tickit::Widget::Scroller' ); +require Tickit::Widget::Scroller; +require Tickit::Widget::Scroller::Item::Text; +require Tickit::Widget::Scroller::Item::RichText; + +pass( 'Modules loaded' ); done_testing; diff --git a/t/01item-text.t b/t/01item-text.t index ca5a5fe..900c8db 100644 --- a/t/01item-text.t +++ b/t/01item-text.t @@ -1,11 +1,12 @@ #!/usr/bin/perl -use strict; +use v5.26; use warnings; -use Test::More; +use Test2::V0; use Tickit::Test 0.12; +use Tickit::Pen; use Tickit::RenderBuffer; use Tickit::Widget::Scroller::Item::Text; @@ -14,11 +15,11 @@ my $term = mk_term; my $item = Tickit::Widget::Scroller::Item::Text->new( "My message here" ); -isa_ok( $item, "Tickit::Widget::Scroller::Item::Text", '$item' ); +isa_ok( $item, [ "Tickit::Widget::Scroller::Item::Text" ], '$item' ); -is_deeply( [ $item->chunks ], - [ [ "My message here", 15 ] ], - '$item->chunks' ); +is( [ $item->chunks ], + [ [ "My message here", 15 ] ], + '$item->chunks' ); is( $item->height_for_width( 80 ), 1, 'height_for_width 80' ); @@ -142,10 +143,10 @@ drain_termlog; my $item = Tickit::Widget::Scroller::Item::Text->new( "Some more text\nwith linefeeds" ); - is_deeply( [ $item->chunks ], - [ [ "Some more text", 14, linebreak => 1 ], - [ "with linefeeds", 14 ] ], - '$item->chunks with linefeeds' ); + is( [ $item->chunks ], + [ [ "Some more text", 14, linebreak => 1 ], + [ "with linefeeds", 14 ] ], + '$item->chunks with linefeeds' ); is( $item->height_for_width( 80 ), 2, 'height_for_width 2 with linefeeds' ); @@ -180,9 +181,9 @@ drain_termlog; my $item = Tickit::Widget::Scroller::Item::Text->new( "(ノಠ益ಠ)ノ彡┻━┻" ); - is_deeply( [ $item->chunks ], - [ [ "(ノಠ益ಠ)ノ彡┻━┻", 15 ] ], - '$item->chunks with Unicode' ); + is( [ $item->chunks ], + [ [ "(ノಠ益ಠ)ノ彡┻━┻", 15 ] ], + '$item->chunks with Unicode' ); is( $item->height_for_width( 80 ), 1, 'height_for_width 2 with Unicode' ); @@ -202,4 +203,55 @@ drain_termlog; 'Display for render with Unicode' ); } +# Empty +{ + $term->clear; + drain_termlog; + + my $item = Tickit::Widget::Scroller::Item::Text->new( "" ); + + is( [ $item->chunks ], + [ [ "", 0 ] ], + '$item->chunks for empty string' ); + + is( $item->height_for_width( 80 ), 1, 'height_for_width 1 for empty string' ); + + $item->render( $rb, top => 0, firstline => 0, lastline => 0, width => 80, height => 1 ); + $rb->flush_to_term( $term ); + + flush_tickit; + + is_termlog( [ GOTO(0,0), + SETPEN, + ERASECH(80) ], + 'Termlog for render for empty string' ); + + is_display( [ ], + 'Display for render for empty string' ); +} + +# With pen +{ + $term->clear; + drain_termlog; + + my $item = Tickit::Widget::Scroller::Item::Text->new( "Red with green BG", + pen => Tickit::Pen->new( fg => "red", bg => "green" ) + ); + + $item->height_for_width( 80 ); + + $item->render( $rb, top => 0, firstline => 0, lastline => 0, width => 80, height => 1 ); + $rb->flush_to_term( $term ); + + flush_tickit; + + is_termlog( [ GOTO(0,0), + SETPEN(fg=>1,bg=>2), + PRINT("Red with green BG"), + SETPEN(fg=>1,bg=>2), + ERASECH(63) ], + 'Termlog for render with pen' ); +} + done_testing; diff --git a/t/02item-richtext.t b/t/02item-richtext.t index a081465..a1a8ffd 100644 --- a/t/02item-richtext.t +++ b/t/02item-richtext.t @@ -1,9 +1,9 @@ #!/usr/bin/perl -use strict; +use v5.26; use warnings; -use Test::More; +use Test2::V0; use Tickit::Test; use Tickit::RenderBuffer; @@ -21,14 +21,15 @@ $str->apply_tag( 11, 4, u => 1 ); my $item = Tickit::Widget::Scroller::Item::RichText->new( $str ); -isa_ok( $item, "Tickit::Widget::Scroller::Item::Text", '$item' ); +isa_ok( $item, [ "Tickit::Widget::Scroller::Item::Text" ], '$item' ); -is_deeply( [ $item->chunks ], - [ [ "My ", 3, pen => Tickit::Pen->new() ], - [ "message", 7, pen => Tickit::Pen->new( b => 1 ) ], - [ " ", 1, pen => Tickit::Pen->new() ], - [ "here", 4, pen => Tickit::Pen->new( u => 1 ) ] ], - '$item->chunks' ); +is( [ $item->chunks ], + # Stringify the pens so Test2 will compare the stringified versions + [ [ "My ", 3, pen => "".Tickit::Pen->new() ], + [ "message", 7, pen => "".Tickit::Pen->new( b => 1 ) ], + [ " ", 1, pen => "".Tickit::Pen->new() ], + [ "here", 4, pen => "".Tickit::Pen->new( u => 1 ) ] ], + '$item->chunks' ); is( $item->height_for_width( 80 ), 1, 'height_for_width 80' ); @@ -63,12 +64,12 @@ is_display( [ [TEXT("My "), TEXT("message",b=>1), BLANK(1), TEXT("here",u=>1)] ] my $item = Tickit::Widget::Scroller::Item::RichText->new( $str ); - is_deeply( [ $item->chunks ], - [ [ "Another ", 8, pen => Tickit::Pen->new() ], - [ "message", 7, pen => Tickit::Pen->new( b => 1 ), linebreak => 1 ], - [ "with", 4, pen => Tickit::Pen->new( b => 1 ) ], - [ " linefeeds", 10, pen => Tickit::Pen->new() ] ], - '$item->chunks with linefeeds' ); + is( [ $item->chunks ], + [ [ "Another ", 8, pen => "".Tickit::Pen->new() ], + [ "message", 7, pen => "".Tickit::Pen->new( b => 1 ), linebreak => 1 ], + [ "with", 4, pen => "".Tickit::Pen->new( b => 1 ) ], + [ " linefeeds", 10, pen => "".Tickit::Pen->new() ] ], + '$item->chunks with linefeeds' ); } # Word wrapping on pen changes diff --git a/t/03item-richtext-formatting.t b/t/03item-richtext-formatting.t index 192ceec..b749dc6 100644 --- a/t/03item-richtext-formatting.t +++ b/t/03item-richtext-formatting.t @@ -1,9 +1,9 @@ #!/usr/bin/perl -use strict; +use v5.26; use warnings; -use Test::More; +use Test2::V0; plan skip_all => "Convert::Color::XTerm is not available" unless eval { require Convert::Color::XTerm }; diff --git a/t/04item-margins.t b/t/04item-margins.t new file mode 100644 index 0000000..9e80d65 --- /dev/null +++ b/t/04item-margins.t @@ -0,0 +1,138 @@ +#!/usr/bin/perl + +use v5.26; +use warnings; + +use Test2::V0; + +use Tickit::Test 0.12; +use Tickit::RenderBuffer; + +use Tickit::Widget::Scroller::Item::Text; + +my $term = mk_term; + +my $item = Tickit::Widget::Scroller::Item::Text->new( "My message here" ); + +# margin_left +{ + my $item = Tickit::Widget::Scroller::Item::Text->new( "ABCDE " x 10, margin_left => 5 ); + + is( $item->height_for_width( 40 ), 2, 'height_for_width 40' ); + + my $rb = Tickit::RenderBuffer->new( lines => $term->lines, cols => $term->cols ); + + $item->render( $rb, top => 0, firstline => 0, lastline => 1, width => 40, height => 25 ); + $rb->flush_to_term( $term ); + + is_termlog( [ GOTO(0,0), + SETPEN, + ERASECH(5,1), + SETPEN, + PRINT("ABCDE "x5 . "ABCDE"), + GOTO(1,0), + SETPEN, + ERASECH(5,1), + SETPEN, + PRINT("ABCDE "x4), + SETPEN, + ERASECH(11) ], + 'Termlog for render margin_left' ); + + is_display( [ [TEXT(" ABCDE ABCDE ABCDE ABCDE ABCDE ABCDE")], + [TEXT(" ABCDE ABCDE ABCDE ABCDE")]], + 'Display for render margin_left' ); +} + +# margin_right +{ + my $item = Tickit::Widget::Scroller::Item::Text->new( "ABCDE " x 10, margin_right => 5 ); + + is( $item->height_for_width( 40 ), 2, 'height_for_width 40' ); + + my $rb = Tickit::RenderBuffer->new( lines => $term->lines, cols => $term->cols ); + + $item->render( $rb, top => 0, firstline => 0, lastline => 1, width => 40, height => 25 ); + $rb->flush_to_term( $term ); + + is_termlog( [ GOTO(0,0), + SETPEN, + PRINT("ABCDE "x5 . "ABCDE"), + SETPEN, + ERASECH(5), + GOTO(1,0), + SETPEN, + PRINT("ABCDE "x4), + SETPEN, + ERASECH(16) ], + 'Termlog for render margin_right' ); + + is_display( [ [TEXT("ABCDE ABCDE ABCDE ABCDE ABCDE ABCDE")], + [TEXT("ABCDE ABCDE ABCDE ABCDE")]], + 'Display for render margin_right' ); +} + +# margin sets both +{ + my $item = Tickit::Widget::Scroller::Item::Text->new( "ABCDE " x 10, margin => 5 ); + + is( $item->height_for_width( 40 ), 2, 'height_for_width 40' ); + + my $rb = Tickit::RenderBuffer->new( lines => $term->lines, cols => $term->cols ); + + $item->render( $rb, top => 0, firstline => 0, lastline => 1, width => 40, height => 25 ); + $rb->flush_to_term( $term ); + + is_termlog( [ GOTO(0,0), + SETPEN, + ERASECH(5,1), + SETPEN, + PRINT("ABCDE "x5), + SETPEN, + ERASECH(5), + GOTO(1,0), + SETPEN, + ERASECH(5,1), + SETPEN, + PRINT("ABCDE "x5), + SETPEN, + ERASECH(5) ], + 'Termlog for render margin' ); + + is_display( [ [TEXT(" ABCDE ABCDE ABCDE ABCDE ABCDE")], + [TEXT(" ABCDE ABCDE ABCDE ABCDE ABCDE")]], + 'Display for render margin' ); +} + +# margin excludes pen +{ + $term->clear; + drain_termlog; + + my $item = Tickit::Widget::Scroller::Item::Text->new( "Red with green BG", + margin => 10, + pen => Tickit::Pen->new( fg => "red", bg => "green" ) + ); + + $item->height_for_width( 80 ); + + my $rb = Tickit::RenderBuffer->new( lines => $term->lines, cols => $term->cols ); + + $item->render( $rb, top => 0, firstline => 0, lastline => 0, width => 80, height => 1 ); + $rb->flush_to_term( $term ); + + flush_tickit; + + is_termlog( [ GOTO(0,0), + SETPEN, + ERASECH(10,1), + SETPEN(fg=>1,bg=>2), + PRINT("Red with green BG"), + SETPEN(fg=>1,bg=>2), + ERASECH(43,1), + SETPEN, + ERASECH(10) ], + 'Termlog for render with pen and margin' ); +} + +done_testing; diff --git a/t/10initial.t b/t/10initial.t index e42d82f..be1d567 100644 --- a/t/10initial.t +++ b/t/10initial.t @@ -1,9 +1,9 @@ #!/usr/bin/perl -use strict; +use v5.26; use warnings; -use Test::More; +use Test2::V0; use Tickit::Test; @@ -51,13 +51,13 @@ is_display( [ "The first line", 'Display initially' ); is( scalar $scroller->line2item( 0 ), 0, 'scalar line2item 0' ); -is_deeply( [ $scroller->line2item( 0 ) ], [ 0, 0 ], 'line2item 0' ); -is_deeply( [ $scroller->line2item( 1 ) ], [ 1, 0 ], 'line2item 1' ); -is_deeply( [ $scroller->line2item( 2 ) ], [ 2, 0 ], 'line2item 2' ); -is_deeply( [ $scroller->line2item( 3 ) ], [ ], 'line2item 3' ); +is( [ $scroller->line2item( 0 ) ], [ 0, 0 ], 'line2item 0' ); +is( [ $scroller->line2item( 1 ) ], [ 1, 0 ], 'line2item 1' ); +is( [ $scroller->line2item( 2 ) ], [ 2, 0 ], 'line2item 2' ); +is( [ $scroller->line2item( 3 ) ], [ ], 'line2item 3' ); -is_deeply( [ $scroller->line2item( -1 ) ], [ ], 'line2item -1' ); -is_deeply( [ $scroller->line2item( -23 ) ], [ 2, 0 ], 'line2item -23' ); +is( [ $scroller->line2item( -1 ) ], [ ], 'line2item -1' ); +is( [ $scroller->line2item( -23 ) ], [ 2, 0 ], 'line2item -23' ); is( $scroller->item2line( 0 ), 0, 'item2line 0' ); is( $scroller->item2line( 0, -1 ), 0, 'item2line 0, -1' ); @@ -97,14 +97,14 @@ is_display( [ "The first line", "The third line" ], 'Display after narrowing' ); -is_deeply( [ $scroller->line2item( 0 ) ], [ 0, 0 ], 'line2item 0' ); -is_deeply( [ $scroller->line2item( 1 ) ], [ 1, 0 ], 'line2item 1' ); -is_deeply( [ $scroller->line2item( 2 ) ], [ 1, 1 ], 'line2item 2' ); -is_deeply( [ $scroller->line2item( 3 ) ], [ 2, 0 ], 'line2item 3' ); -is_deeply( [ $scroller->line2item( 4 ) ], [ ], 'line2item 4' ); +is( [ $scroller->line2item( 0 ) ], [ 0, 0 ], 'line2item 0' ); +is( [ $scroller->line2item( 1 ) ], [ 1, 0 ], 'line2item 1' ); +is( [ $scroller->line2item( 2 ) ], [ 1, 1 ], 'line2item 2' ); +is( [ $scroller->line2item( 3 ) ], [ 2, 0 ], 'line2item 3' ); +is( [ $scroller->line2item( 4 ) ], [ ], 'line2item 4' ); -is_deeply( [ $scroller->line2item( -1 ) ], [ ], 'line2item -1' ); -is_deeply( [ $scroller->line2item( -22 ) ], [ 2, 0 ], 'line2item -22' ); +is( [ $scroller->line2item( -1 ) ], [ ], 'line2item -1' ); +is( [ $scroller->line2item( -22 ) ], [ 2, 0 ], 'line2item -22' ); is( $scroller->item2line( 0 ), 0, 'item2line 0' ); is( $scroller->item2line( 0, -1 ), 0, 'item2line 0, -1' ); diff --git a/t/11scroll.t b/t/11scroll.t index 0d97fb2..00ee609 100644 --- a/t/11scroll.t +++ b/t/11scroll.t @@ -1,9 +1,9 @@ #!/usr/bin/perl -use strict; +use v5.26; use warnings; -use Test::More; +use Test2::V0; use Tickit::Test 0.12; @@ -62,8 +62,8 @@ is( $scroller->item2line( 1, -1 ), 3, 'item2line 1, -1 initially' ); is( $scroller->item2line( 2, 0 ), 4, 'item2line 2, 0 initially' ); is( $scroller->item2line( 2, -1 ), undef, 'item2line 2, -1 initially offscreen' ); -is_deeply( [ $scroller->item2line( 2, -1 ) ], [ undef, "below" ], 'list item2line 2, -1 initially below screen' ); -is_deeply( [ $scroller->item2line( 2, -1, 1 ) ], [ 5, "below" ], 'list item2line 2, -1 initially below screen with count_offscreen' ); +is( [ $scroller->item2line( 2, -1 ) ], [ undef, "below" ], 'list item2line 2, -1 initially below screen' ); +is( [ $scroller->item2line( 2, -1, 1 ) ], [ 5, "below" ], 'list item2line 2, -1 initially below screen with count_offscreen' ); is( $scroller->lines_above, 0, 'lines_above initially' ); is( $scroller->lines_below, 13, 'lines_below initially' ); @@ -108,10 +108,10 @@ is( $scroller->item2line( 5, 0 ), 0, 'item2line 5, 0 after scroll +10' ); is( $scroller->item2line( 5, -1 ), 1, 'item2line 5, -1 after scroll +10' ); is( $scroller->item2line( 8, 0 ), undef, 'item2line 8, 0 offscreen after scroll +10' ); -is_deeply( [ $scroller->item2line( 0, 0 ) ], [ undef, "above" ], 'list item2line 0, 0 above screen after scroll +10' ); -is_deeply( [ $scroller->item2line( 0, 0, 1 ) ], [ -10, "above" ], 'list item2line 0, 0 above screen after scroll +10 with count_offscreen' ); -is_deeply( [ $scroller->item2line( 8, 0 ) ], [ undef, "below" ], 'list item2line 8, 0 below screen after scroll +10' ); -is_deeply( [ $scroller->item2line( 8, 0, 1 ) ], [ 6, "below" ], 'list item2line 8, 0 below screen after scroll +10 with count_offscreen' ); +is( [ $scroller->item2line( 0, 0 ) ], [ undef, "above" ], 'list item2line 0, 0 above screen after scroll +10' ); +is( [ $scroller->item2line( 0, 0, 1 ) ], [ -10, "above" ], 'list item2line 0, 0 above screen after scroll +10 with count_offscreen' ); +is( [ $scroller->item2line( 8, 0 ) ], [ undef, "below" ], 'list item2line 8, 0 below screen after scroll +10' ); +is( [ $scroller->item2line( 8, 0, 1 ) ], [ 6, "below" ], 'list item2line 8, 0 below screen after scroll +10 with count_offscreen' ); is( $scroller->lines_above, 10, 'lines_above after scroll +10' ); is( $scroller->lines_below, 3, 'lines_below after scroll +10' ); diff --git a/t/12resize-bottom.t b/t/12resize-bottom.t index 28b2634..52c5ee9 100644 --- a/t/12resize-bottom.t +++ b/t/12resize-bottom.t @@ -1,9 +1,9 @@ #!/usr/bin/perl -use strict; +use v5.26; use warnings; -use Test::More; +use Test2::V0; use Tickit::Test; diff --git a/t/12resize-top.t b/t/12resize-top.t index 2f32438..4f5438a 100644 --- a/t/12resize-top.t +++ b/t/12resize-top.t @@ -1,9 +1,9 @@ #!/usr/bin/perl -use strict; +use v5.26; use warnings; -use Test::More; +use Test2::V0; use Tickit::Test; diff --git a/t/20push-bottom.t b/t/20push-bottom.t index 4185ac3..27415aa 100644 --- a/t/20push-bottom.t +++ b/t/20push-bottom.t @@ -1,9 +1,9 @@ #!/usr/bin/perl -use strict; +use v5.26; use warnings; -use Test::More; +use Test2::V0; use Tickit::Test 0.12; @@ -33,10 +33,14 @@ is_display( [ ], is_cursorpos( 7, 0, 'Cursor position intially' ); +is( scalar $scroller->items, 0, '$scroller->items is 0 before ->push' ); + $scroller->push( Tickit::Widget::Scroller::Item::Text->new( "A line of text" ), ); +is( scalar $scroller->items, 1, '$scroller->items is 1 after ->push' ); + flush_tickit; is_termlog( [ GOTO(0,0), diff --git a/t/20push-top.t b/t/20push-top.t index b900c6a..ffe8d6a 100644 --- a/t/20push-top.t +++ b/t/20push-top.t @@ -1,9 +1,9 @@ #!/usr/bin/perl -use strict; +use v5.26; use warnings; -use Test::More; +use Test2::V0; use Tickit::Test 0.12; @@ -33,10 +33,14 @@ is_display( [ ], is_cursorpos( 7, 0, 'Cursor position intially' ); +is( scalar $scroller->items, 0, '$scroller->items is 0 before ->push' ); + $scroller->push( Tickit::Widget::Scroller::Item::Text->new( "A line of text" ), ); +is( scalar $scroller->items, 1, '$scroller->items is 1 after ->push' ); + flush_tickit; is_termlog( [ GOTO(0,0), diff --git a/t/21shift-bottom.t b/t/21shift-bottom.t index 22ef5a7..a51c18f 100644 --- a/t/21shift-bottom.t +++ b/t/21shift-bottom.t @@ -1,9 +1,9 @@ #!/usr/bin/perl -use strict; +use v5.26; use warnings; -use Test::More; +use Test2::V0; use Tickit::Test 0.12; @@ -46,8 +46,12 @@ is_display( [ [TEXT("Existing line 1")], is_cursorpos( 7, 0, 'Cursor position intially' ); +is( scalar $scroller->items, 20, '$scroller->items is 20 before ->shift' ); + $scroller->shift; +is( scalar $scroller->items, 19, '$scroller->items is 19 after ->shift' ); + flush_tickit; is_termlog( [ SETBG(undef), diff --git a/t/21shift-top.t b/t/21shift-top.t index f40ebe6..5c57476 100644 --- a/t/21shift-top.t +++ b/t/21shift-top.t @@ -1,9 +1,9 @@ #!/usr/bin/perl -use strict; +use v5.26; use warnings; -use Test::More; +use Test2::V0; use Tickit::Test 0.12; @@ -46,10 +46,14 @@ is_display( [ [TEXT("Existing line 1")], is_cursorpos( 7, 0, 'Cursor position intially' ); +is( scalar $scroller->items, 20, '$scroller->items is 20 before ->shift' ); + my ( $item ) = $scroller->shift; isa_ok( $item, "Tickit::Widget::Scroller::Item::Text" ); +is( scalar $scroller->items, 19, '$scroller->items is 19 after ->shift' ); + flush_tickit; is_termlog( [ SETBG(undef), diff --git a/t/22unshift-bottom.t b/t/22unshift-bottom.t index 4e56a46..7c11fb3 100644 --- a/t/22unshift-bottom.t +++ b/t/22unshift-bottom.t @@ -1,9 +1,9 @@ #!/usr/bin/perl -use strict; +use v5.26; use warnings; -use Test::More; +use Test2::V0; use Tickit::Test 0.12; diff --git a/t/22unshift-top.t b/t/22unshift-top.t index 9fe302f..5cb8483 100644 --- a/t/22unshift-top.t +++ b/t/22unshift-top.t @@ -1,9 +1,9 @@ #!/usr/bin/perl -use strict; +use v5.26; use warnings; -use Test::More; +use Test2::V0; use Tickit::Test 0.12; diff --git a/t/23pop-bottom.t b/t/23pop-bottom.t index f9da224..82d3cdc 100644 --- a/t/23pop-bottom.t +++ b/t/23pop-bottom.t @@ -1,9 +1,9 @@ #!/usr/bin/perl -use strict; +use v5.26; use warnings; -use Test::More; +use Test2::V0; use Tickit::Test 0.12; diff --git a/t/23pop-top.t b/t/23pop-top.t index 9e1e2e9..4f86041 100644 --- a/t/23pop-top.t +++ b/t/23pop-top.t @@ -1,9 +1,9 @@ #!/usr/bin/perl -use strict; +use v5.26; use warnings; -use Test::More; +use Test2::V0; use Tickit::Test 0.12; diff --git a/t/30indicator.t b/t/30indicator.t index cec9424..c1ac8e6 100644 --- a/t/30indicator.t +++ b/t/30indicator.t @@ -1,18 +1,16 @@ #!/usr/bin/perl -use strict; +use v5.26; use warnings; -use Test::More; +use Test2::V0; use Tickit::Test; use Tickit::Widget::Scroller; use Tickit::Widget::Scroller::Item::Text; -# These tests depend on the new Window rendering behaviour added in Tickit 0.43 -# but the actual functionallity will work fine before that -eval { require Tickit::Window; Tickit::Window->VERSION( '0.43' ) } - or plan skip_all => "Tickit::Window older than 0.43; these tests won't work"; +# At Tickit::Window 0.44, rendering is done in one go. +use Tickit::Window 0.44; my $win = mk_window; @@ -21,7 +19,7 @@ my $scroller = Tickit::Widget::Scroller->new( my $self = shift; # TODO: This is a fragile API, needs fixing return sprintf "-- Start{%d/%d} items{%d} --", - $self->_start_item, $self->_start_partial, scalar $self->_items; + $self->_start_item, $self->_start_partial, scalar $self->items; }, ); @@ -32,39 +30,20 @@ $scroller->push( $scroller->set_window( $win ); flush_tickit; -# At Tickit::Window 0.44, rendering is done in one go. -if( $Tickit::Window::VERSION >= '0.44' ) { - is_termlog( [ GOTO(0,0), - SETPEN, - PRINT("Line 1 of content"), - SETBG(undef), - ERASECH(37,1), - SETPEN(rv=>1), - PRINT("-- Start{0/0} items{50} --"), - - ( map { GOTO($_-1,0), - SETPEN, - PRINT("Line $_ of content"), - SETBG(undef), - ERASECH(64-length $_), } 2 .. 25 ) ], - 'Termlog initially' ); -} -else { - is_termlog( [ GOTO(0,0), - SETPEN, - PRINT("Line 1 of content"), - SETBG(undef), - ERASECH(37), - ( map { GOTO($_-1,0), - SETPEN, - PRINT("Line $_ of content"), - SETBG(undef), - ERASECH(64-length $_), } 2 .. 25 ), - GOTO(0,54), - SETPEN(rv=>1), - PRINT("-- Start{0/0} items{50} --") ], - 'Termlog initially' ); -} +is_termlog( [ GOTO(0,0), + SETPEN, + PRINT("Line 1 of content"), + SETBG(undef), + ERASECH(37,1), + SETPEN(rv=>1), + PRINT("-- Start{0/0} items{50} --"), + + ( map { GOTO($_-1,0), + SETPEN, + PRINT("Line $_ of content"), + SETBG(undef), + ERASECH(64-length $_), } 2 .. 25 ) ], + 'Termlog initially' ); is_display( [ [TEXT("Line 1 of content" . (" "x37)), TEXT("-- Start{0/0} items{50} --",rv=>1) ], map { "Line $_ of content" } 2 .. 25 ], @@ -73,41 +52,21 @@ is_display( [ [TEXT("Line 1 of content" . (" "x37)), TEXT("-- Start{0/0} items{5 $scroller->scroll( 2 ); flush_tickit; -if( $Tickit::Window::VERSION >= '0.44' ) { - is_termlog( [ SETPEN, - SCROLLRECT(1,0,24,80,2,0), - GOTO(0,0), - SETPEN, - PRINT("Line 3 of content"), - SETBG(undef), - ERASECH(37,1), - SETPEN(rv=>1), - PRINT("-- Start{2/0} items{50} --"), - ( map { GOTO($_-3,0), - SETPEN, - PRINT("Line $_ of content"), - SETBG(undef), - ERASECH(64-length $_), } 26 .. 27 ) ], - 'Termlog after ->scroll' ); -} -else { - is_termlog( [ SETPEN, - SCROLLRECT(1,0,24,80,2,0), - GOTO(0,0), - SETPEN, - PRINT("Line 3 of content"), - SETBG(undef), - ERASECH(37), - GOTO(0,54), - SETPEN(rv=>1), - PRINT("-- Start{2/0} items{50} --"), - ( map { GOTO($_-3,0), - SETPEN, - PRINT("Line $_ of content"), - SETBG(undef), - ERASECH(64-length $_), } 26 .. 27 ) ], - 'Termlog after ->scroll' ); -} +is_termlog( [ SETPEN, + SCROLLRECT(1,0,24,80,2,0), + GOTO(0,0), + SETPEN, + PRINT("Line 3 of content"), + SETBG(undef), + ERASECH(37,1), + SETPEN(rv=>1), + PRINT("-- Start{2/0} items{50} --"), + ( map { GOTO($_-3,0), + SETPEN, + PRINT("Line $_ of content"), + SETBG(undef), + ERASECH(64-length $_), } 26 .. 27 ) ], + 'Termlog after ->scroll' ); is_display( [ [TEXT("Line 3 of content" . (" "x37)), TEXT("-- Start{2/0} items{50} --",rv=>1) ], map { "Line $_ of content" } 4 .. 27 ], @@ -140,4 +99,29 @@ is_display( [ ( map { "Line $_ of content" } 3 .. 26 ), [TEXT("Line 27 of content" . (" "x52)), TEXT("-- more --",rv=>1) ] ], 'Display after setting bottom indicator' ); +clear_term; +drain_termlog; + +$win->resize( 15, 80 ); + +flush_tickit; + +is_termlog( [ ( map { GOTO($_-3,0), + SETPEN, + PRINT("Line $_ of content"), + SETBG(undef), + ERASECH(64-length $_) } 3 .. 16 ), + GOTO(14,0), + SETPEN, + PRINT("Line 17 of content"), + SETBG(undef), + ERASECH(52,1), + SETPEN(rv=>1), + PRINT("-- more --") ], + 'Termlog after window resize' ); + +is_display( [ ( map { "Line $_ of content" } 3 .. 16 ), + [TEXT("Line 17 of content" . (" "x52)), TEXT("-- more --",rv=>1) ] ], + 'Display after window resize' ); + done_testing; @@ -1,9 +1,9 @@ #!/usr/bin/perl -use strict; +use v5.26; use warnings; -use Test::More; +use Test2::V0; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; |