1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#!/usr/bin/perl
use v5.26;
use warnings;
use Test2::V0;
use Tickit::Test;
use Tickit::RenderBuffer;
use String::Tagged;
use Tickit::Widget::Scroller::Item::RichText;
my $term = mk_term;
my $rb = Tickit::RenderBuffer->new( lines => $term->lines, cols => $term->cols );
my $str = String::Tagged->new( "My message here" );
$str->apply_tag( 3, 7, b => 1 );
$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' );
is( [ $item->chunks ],
# Stringify the pens so Test2 will compare the stringified versions
[ [ "My ", 3, pen => "".Tickit::Pen::Immutable->new() ],
[ "message", 7, pen => "".Tickit::Pen::Immutable->new( b => 1 ) ],
[ " ", 1, pen => "".Tickit::Pen::Immutable->new() ],
[ "here", 4, pen => "".Tickit::Pen::Immutable->new( u => 1 ) ] ],
'$item->chunks' );
is( $item->height_for_width( 80 ), 1, 'height_for_width 80' );
$item->render( $rb, top => 0, firstline => 0, lastline => 0, width => 80, height => 25 );
$rb->flush_to_term( $term );
flush_tickit;
is_termlog( [ GOTO(0,0),
SETPEN,
PRINT("My "),
SETPEN(b => 1),
PRINT("message"),
SETPEN,
PRINT(" "),
SETPEN(u => 1),
PRINT("here"),
SETBG(undef),
ERASECH(65) ],
'Termlog for render fullwidth' );
is_display( [ [TEXT("My "), TEXT("message",b=>1), BLANK(1), TEXT("here",u=>1)] ],
'Display for render fullwidth' );
# Linefeeds
{
$term->clear;
drain_termlog;
my $str = String::Tagged->new( "Another message\nwith linefeeds" );
$str->apply_tag( 8, 12, b => 1 );
my $item = Tickit::Widget::Scroller::Item::RichText->new( $str );
is( [ $item->chunks ],
[ [ "Another ", 8, pen => "".Tickit::Pen::Immutable->new() ],
[ "message", 7, pen => "".Tickit::Pen::Immutable->new( b => 1 ), linebreak => 1 ],
[ "with", 4, pen => "".Tickit::Pen::Immutable->new( b => 1 ) ],
[ " linefeeds", 10, pen => "".Tickit::Pen::Immutable->new() ] ],
'$item->chunks with linefeeds' );
}
# Word wrapping on pen changes
{
$term->clear;
drain_termlog;
my $str = String::Tagged->new;
foreach my $colour (qw( red blue green yellow )) {
$str->append_tagged( $colour, fg => $colour );
$str->append( " " );
}
my $item = Tickit::Widget::Scroller::Item::RichText->new( $str );
is( $item->height_for_width( 18 ), 2, 'height_for_width 18 for wrapping pen change' );
$item->render( $rb, top => 0, firstline => 0, lastline => 1, width => 18, height => 2 );
$rb->flush_to_term( $term );
flush_tickit;
is_termlog( [ GOTO(0,0),
SETPEN(fg=>1),
PRINT("red"),
SETPEN,
PRINT(" "),
SETPEN(fg=>4),
PRINT("blue"),
SETPEN,
PRINT(" "),
SETPEN(fg=>2),
PRINT("green"),
SETPEN,
PRINT(" "),
SETPEN,
ERASECH(3),
GOTO(1,0),
SETPEN(fg=>3),
PRINT("yellow"),
SETPEN,
PRINT(" "),
SETPEN,
ERASECH(11) ],
'Termlog for render wrapping pen change' );
is_display( [ [TEXT("red",fg=>1), BLANK(1), TEXT("blue",fg=>4), BLANK(1), TEXT("green",fg=>2)],
[TEXT("yellow",fg=>3)] ],
'Display for render wrapping pen change' );
}
done_testing;
|