App-Lorem-Tickit

 view release on metacpan or  search on metacpan

Tickit/TextWidget.pm  view on Meta::CPAN

package App::Lorem::Tickit::TextWidget;

use parent 'Tickit::Widget';
use strict;
use warnings;

use Tickit::Pen;

our $VERSION = 0.01;

sub new {
	my ($class, %args) = @_;

	my $self = $class->SUPER::new(%args);
	$self->{'_lines'} = [''];
	$self->{'_width'} = 76;

	return $self;
}

sub lines {
	my $self = shift;

	return scalar @{$self->{'_lines'}};
}

sub cols {
	my $self = shift;

	return $self->{'_width'};
}

sub set_text {
	my ($self, $text, $width) = @_;

	$width = 10 if ! defined $width || $width < 10;
	my $old_lines = $self->lines;
	my $old_cols = $self->cols;
	$self->{'_width'} = $width;
	$self->{'_lines'} = [_wrap_text($text, $width)];
	$self->{'_lines'} = [''] if ! @{$self->{'_lines'}};

	$self->resized if $self->lines != $old_lines || $self->cols != $old_cols;
	$self->redraw;

	return;
}

sub render_to_rb {
	my $self = shift;
	my ($rb, $rect) = @_;
	my $win = $self->window;

	return if ! $win;

	my $cols = $win->cols;
	my $pen = Tickit::Pen->new('fg' => 'white', 'bg' => 'black');

	$rb->eraserect($rect, Tickit::Pen->new('bg' => 'black'));
	for my $line_no ($rect->linerange) {
		last if $line_no >= @{$self->{'_lines'}};
		my $line = $self->{'_lines'}->[$line_no];
		my $col = int(($cols - length $line) / 2);
		$col = 0 if $col < 0;
		$rb->text_at($line_no, $col, $line, $pen);
	}

	return;
}

sub _wrap_text {
	my ($text, $width) = @_;

	my @lines;



( run in 0.875 second using v1.01-cache-2.11-cpan-3c2a17b8caa )