Tickit-Widget-Scroller
view release on metacpan or search on metacpan
lib/Tickit/Widget/Scroller/Item/RichText.pm view on Meta::CPAN
=cut
=head2 new_from_formatting
$item = Tickit::Widget::Scroller::Item::RichText->new_from_formatting( $str, %opts )
Constructs a new item containing the text given by the L<String::Tagged>
instance, converting the tags from the L<String::Tagged::Formatting>
convention into native L<Tickit::Pen> format.
=cut
sub _convert_color_tag ($n, $v)
{
return $n => $v->as_xterm->index;
}
my %convert_tags = (
bold => "b",
under => "u",
italic => "i",
strike => "strike",
blink => "blink",
monospace => sub ($, $v) { "af" => ( $v ? 1 : 0 ) },
reverse => "rv",
fg => \&_convert_color_tag,
bg => \&_convert_color_tag,
);
sub new_from_formatting ( $class, $str, %opts )
{
return $class->new(
# TODO: Maybe this should live somewhere more fundamental in Tickit itself?
$str->clone(
only_tags => [ keys %convert_tags ],
convert_tags => \%convert_tags,
),
%opts
);
}
my %PENCACHE;
method pen_for_tags ( $tags )
{
# Cache the pens
my $key = join "|", map { "$_=" . ( $tags->{$_} // "" ) } sort keys %$tags;
# Don't worry if extra tags left over, they just aren't rendering attributes
return $PENCACHE{$key} //= Tickit::Pen::Immutable->new_from_attrs( $tags );
}
method _build_chunks_for ( $str )
{
my @chunks;
$str->iter_substr_nooverlap(
sub {
my ( $substr, %tags ) = @_;
my $pen = $self->pen_for_tags( \%tags );
my @lines = split m/\n/, $substr, -1 or return;
my $lastline = pop @lines;
push @chunks, [ $_, textwidth( $_ ), pen => $pen, linebreak => 1 ] for @lines;
push @chunks, [ $lastline, textwidth( $lastline ), pen => $pen ];
},
);
return @chunks;
}
=head1 AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
=cut
0x55AA;
( run in 1.918 second using v1.01-cache-2.11-cpan-71847e10f99 )