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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
# 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-2017 -- leonerd@leonerd.org.uk
package Tickit::Widget::Scroller::Item::Text;
use strict;
use warnings;
our $VERSION = '0.23';
use Tickit::Utils qw( textwidth cols2chars );
=head1 NAME
C<Tickit::Widget::Scroller::Item::Text> - add static text to a Scroller
=head1 SYNOPSIS
use Tickit::Widget::Scroller;
use Tickit::Widget::Scroller::Item::Text;
my $scroller = Tickit::Widget::Scroller->new;
$scroller->push(
Tickit::Widget::Scroller::Item::Text->new( "Hello world" )
);
=head1 DESCRIPTION
This implementation of L<Tickit::Widget::Scroller::Item> displays a simple
static piece of text. It will be wrapped on whitespace (characters matching
the C</\s/> regexp pattern).
=cut
=head1 CONSTRUCTOR
=cut
=head2 new
$item = Tickit::Widget::Scroller::Item::Text->new( $text, %opts )
Constructs a new text item, containing the given string of text. Once
constructed, the item is immutable.
The following options are recognised in C<%opts>:
=over 4
=item indent => INT
If the text item needs to wrap, indent the second and subsequent lines by this
amount. Does not apply to the first line.
=back
=cut
sub new
{
my $class = shift;
my ( $text, %opts ) = @_;
my $self = bless {
lineruns => [],
}, $class;
$self->{indent} = $opts{indent} if defined $opts{indent};
$self->{chunks} = [ $self->_build_chunks_for( $text ) ];
return $self;
}
=head1 METHODS
=cut
=head2 chunks
@chunks = $item->chunks
Returns the chunks of text displayed by this item. Each chunk is represented
by an ARRAY reference of three fields, giving the text string, its width in
columns, and various options
[ $text, $width, %opts ]
Recognised options are:
=over 8
=item pen => Tickit::Pen
Pen to render the chunk with.
=item linebreak => BOOL
If true, force a linebreak after this chunk; the next one starts on the
following line.
=back
=cut
sub _build_chunks_for
{
my $self = shift;
my ( $text ) = @_;
my @lines = split m/\n/, $text, -1;
my $lastline = pop @lines;
return ( map { [ $_, textwidth( $_ ), linebreak => 1 ] } @lines ),
[ $lastline, textwidth( $lastline ) ];
}
sub chunks
{
my $self = shift;
return @{ $self->{chunks} };
}
sub height_for_width
{
my $self = shift;
my ( $width ) = @_;
$self->{width} = $width;
my @chunks = $self->chunks;
$self->{lineruns} = \my @lineruns;
push @lineruns, my $thisline = [];
my $line_remaining = $width;
while( @chunks ) {
my $chunk = shift @chunks;
my ( $text, $textwidth, %opts ) = @$chunk;
if( $textwidth <= $line_remaining ) {
push @$thisline, [ $text, $textwidth, $opts{pen} ];
$line_remaining -= $textwidth;
}
else {
# Split this chunk at most $line_remaining chars
my $eol_ch = cols2chars $text, $line_remaining;
if( $eol_ch < length $text && substr( $text, $eol_ch, 1 ) =~ m/\S/ ) {
# TODO: This surely must be possible without substr()ing a temporary
if( substr( $text, 0, $eol_ch ) =~ m/\S+$/ and
( $-[0] > 0 or @$thisline ) ) {
$eol_ch = $-[0];
}
}
my $partial_text = substr( $text, 0, $eol_ch );
my $partial_chunk = [ $partial_text, textwidth( $partial_text ), $opts{pen} ];
push @$thisline, $partial_chunk;
my $bol_ch = pos $text = $eol_ch;
$text =~ m/\G\s+/g and $bol_ch = $+[0];
my $remaining_text = substr( $text, $bol_ch );
my $remaining_chunk = [ $remaining_text, textwidth( $remaining_text ), %opts ];
unshift @chunks, $remaining_chunk;
$line_remaining = 0;
}
if( ( $line_remaining == 0 or $opts{linebreak} ) and @chunks ) {
push @lineruns, $thisline = [];
$line_remaining = $width - ( $self->{indent} || 0 );
}
}
return scalar @lineruns;
}
sub render
{
my $self = shift;
my ( $rb, %args ) = @_;
my $cols = $args{width};
# Rechunk if width changed
$self->height_for_width( $cols ) if $cols != $self->{width};
my $lineruns = $self->{lineruns};
foreach my $lineidx ( $args{firstline} .. $args{lastline} ) {
my $indent = ( $lineidx && $self->{indent} ) ? $self->{indent} : 0;
$rb->goto( $lineidx, 0 );
$rb->erase( $indent ) if $indent;
foreach my $chunk ( @{ $lineruns->[$lineidx] } ) {
my ( $text, $width, $chunkpen ) = @$chunk;
$rb->text( $text, $chunkpen );
}
$rb->erase_to( $cols );
}
}
=head1 AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
=cut
0x55AA;
|