| 1 | package App::Lorem::Tickit::TextWidget; | ||
| 2 | |||
| 3 | 10 | ▶ | use parent 'Tickit::Widget'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 7 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 7 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 0 | ▶ | use Tickit::Pen; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | |||
| 9 | our $VERSION = 0.01; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my ($class, %args) = @_; | ||
| 13 | |||
| 14 | my $self = $class->SUPER::new(%args); | ||
| 15 | $self->{'_lines'} = ['']; | ||
| 16 | $self->{'_width'} = 76; | ||
| 17 | |||
| 18 | return $self; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub lines { | ||
| 22 | my $self = shift; | ||
| 23 | |||
| 24 | return scalar @{$self->{'_lines'}}; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub cols { | ||
| 28 | my $self = shift; | ||
| 29 | |||
| 30 | return $self->{'_width'}; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub set_text { | ||
| 34 | my ($self, $text, $width) = @_; | ||
| 35 | |||
| 36 | $width = 10 if ! defined $width || $width < 10; | ||
| 37 | my $old_lines = $self->lines; | ||
| 38 | my $old_cols = $self->cols; | ||
| 39 | $self->{'_width'} = $width; | ||
| 40 | $self->{'_lines'} = [_wrap_text($text, $width)]; | ||
| 41 | $self->{'_lines'} = [''] if ! @{$self->{'_lines'}}; | ||
| 42 | |||
| 43 | $self->resized if $self->lines != $old_lines || $self->cols != $old_cols; | ||
| 44 | $self->redraw; | ||
| 45 | |||
| 46 | return; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub render_to_rb { | ||
| 50 | my $self = shift; | ||
| 51 | my ($rb, $rect) = @_; | ||
| 52 | my $win = $self->window; | ||
| 53 | |||
| 54 | return if ! $win; | ||
| 55 | |||
| 56 | my $cols = $win->cols; | ||
| 57 | my $pen = Tickit::Pen->new('fg' => 'white', 'bg' => 'black'); | ||
| 58 | |||
| 59 | $rb->eraserect($rect, Tickit::Pen->new('bg' => 'black')); | ||
| 60 | for my $line_no ($rect->linerange) { | ||
| 61 | last if $line_no >= @{$self->{'_lines'}}; | ||
| 62 | my $line = $self->{'_lines'}->[$line_no]; | ||
| 63 | my $col = int(($cols - length $line) / 2); | ||
| 64 | $col = 0 if $col < 0; | ||
| 65 | $rb->text_at($line_no, $col, $line, $pen); | ||
| 66 | } | ||
| 67 | |||
| 68 | return; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub _wrap_text { | ||
| 72 | my ($text, $width) = @_; | ||
| 73 | |||
| 74 | my @lines; | ||
| 75 | foreach my $paragraph (split /\n\n+/, $text) { | ||
| 76 | my @words = split /\s+/, $paragraph; | ||
| 77 | my $line = ''; | ||
| 78 | foreach my $word (@words) { | ||
| 79 | while (length $word > $width) { | ||
| 80 | push @lines, substr($word, 0, $width, ''); | ||
| 81 | } | ||
| 82 | if ($line eq '') { | ||
| 83 | $line = $word; | ||
| 84 | } elsif (length($line) + 1 + length($word) <= $width) { | ||
| 85 | $line .= ' '.$word; | ||
| 86 | } else { | ||
| 87 | push @lines, $line; | ||
| 88 | $line = $word; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | push @lines, $line if $line ne ''; | ||
| 92 | push @lines, ''; | ||
| 93 | } | ||
| 94 | pop @lines if @lines && $lines[-1] eq ''; | ||
| 95 | |||
| 96 | return @lines; | ||
| 97 | } | ||
| 98 | |||
| 99 | 1; | ||
| 100 | |||
| 101 | __END__ | ||