Tickit/Widget.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.5
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/App/Lorem/Tickit/TextWidget.pm ↑ Summary
1 package App::Lorem::Tickit::Widget;
2  
3 17 ▶use parent 'Tickit::Widget';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use App::Lorem::Tickit::TextWidget;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use Text::Lorem;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use Tickit::Pen;
10 use Tickit::Widget::Choice;
11 use Tickit::Widget::ScrollBox;
12  
13 our $VERSION = 0.01;
14  
15 sub new {
16 my ($class, %args) = @_;
17  
18 my $self = $class->SUPER::new(%args);
19 $self->{'_counts'} = {
20 'paragraphs' => 3,
21 'sentences' => 8,
22 'words' => 50,
23 };
24 $self->{'_lorem'} = Text::Lorem->new;
25 $self->{'_version'} = $args{'version'} || $VERSION;
26 $self->{'_choice'} = Tickit::Widget::Choice->new(
27 'choices' => [
28 ['paragraphs', 'paragraphs'],
29 ['sentences', 'sentences'],
30 ['words', 'words'],
31 ],
32 'on_changed' => sub {
33 $self->_regenerate;
34 $self->redraw;
35 return;
36 },
37 );
38 $self->{'_text_widget'} = App::Lorem::Tickit::TextWidget->new;
39 $self->{'_scrollbox'} = Tickit::Widget::ScrollBox->new(
40 'vertical' => 'on_demand',
41 'horizontal' => 0,
42 )->set_child($self->{'_text_widget'});
43 $self->_regenerate;
44  
45 return $self;
46 }
47  
48 sub CAN_FOCUS {
49 return 1;
50 }
51  
52 sub cols {
53 return 40;
54 }
55  
56 sub lines {
57 return 10;
58 }
59  
60 sub window_gained {
61 my $self = shift;
62  
63 $self->SUPER::window_gained(@_);
64 $self->reshape;
65  
66 return;
67 }
68  
69 sub window_lost {
70 my $self = shift;
71  
72 if ($self->{'_choice'}->window) {
73 $self->{'_choice'}->window->close;
74 $self->{'_choice'}->set_window(undef);
75 }
76 if ($self->{'_scrollbox'}->window) {
77 $self->{'_scrollbox'}->window->close;
78 $self->{'_scrollbox'}->set_window(undef);
79 }
80  
81 $self->SUPER::window_lost(@_);
82  
83 return;
84 }
85  
86 sub on_key {
87 my $self = shift;
88 my ($ev) = @_;
89 my $key = $ev->str;
90  
91 if ($key eq '+') {
92 $self->_change_count(1);
93 return 1;
94 } elsif ($key eq '-') {
95 $self->_change_count(-1);
96 return 1;
97 } elsif ($key eq 'Tab' || $key eq 'Down' || $key eq 'Right') {
98 $self->_next_choice;
99 return 1;
100 } elsif ($key eq 'Up' || $key eq 'Left') {
101 $self->_prev_choice;
102 return 1;
103 } elsif ($key eq 'Enter' || $key eq 'Space') {
104 $self->_regenerate;
105 $self->redraw;
106 return 1;
107 } elsif ($key eq 'PageUp') {
108 $self->_scroll_page(-1);
109 return 1;
110 } elsif ($key eq 'PageDown') {
111 $self->_scroll_page(1);
112 return 1;
113 } elsif ($key eq 'q' || $key eq 'C-c') {
114 $self->window->tickit->stop if $self->window;
115 return 1;
116 }
117  
118 return 0;
119 }
120  
121 sub reshape {
122 my $self = shift;
123 my $win = $self->window;
124  
125 return if ! $win;
126  
127 my $cols = $win->cols;
128 my $choice_win = $self->{'_choice'}->window;
129 my $text_top = 3;
130 my $text_lines = _max(0, $win->lines - $text_top - 1);
131 my $scroll_win = $self->{'_scrollbox'}->window;
132  
133 if ($choice_win) {
134 $choice_win->change_geometry(0, 0, 1, $cols);
135 } else {
136 $choice_win = $win->make_sub(0, 0, 1, $cols);
137 $self->{'_choice'}->set_window($choice_win);
138 }
139 $self->{'_choice'}->take_focus;
140 $self->_sync_text_widget;
141  
142 if ($scroll_win) {
143 $scroll_win->change_geometry($text_top, 0, $text_lines, $cols);
144 } elsif ($text_lines > 0) {
145 $scroll_win = $win->make_sub($text_top, 0, $text_lines, $cols);
146 $self->{'_scrollbox'}->set_window($scroll_win);
147 }
148 if ($scroll_win && delete $self->{'_reset_scroll'}) {
149 $self->{'_scrollbox'}->scroll_to(0, 0);
150 }
151  
152 return;
153 }
154  
155 sub render_to_rb {
156 my $self = shift;
157 my ($rb, $rect) = @_;
158 my $win = $self->window;
159  
160 $rb->eraserect($rect, Tickit::Pen->new('bg' => 'black'));
161 return if ! $win;
162  
163 my $lines = $win->lines;
164 my $cols = $win->cols;
165  
166 $self->_render_count($rb, 1, $cols);
167 $self->_render_status($rb, $lines - 1, $cols);
168  
169 return;
170 }
171  
172 sub _render_count {
173 my ($self, $rb, $line, $cols) = @_;
174  
175 return if $cols <= 0;
176  
177 my $mode = $self->_mode;
178 my $count = $self->{'_counts'}->{$mode};
179 my $text = 'count: '.$count;
180  
181 $rb->text_at($line, 0, $text, Tickit::Pen->new('fg' => 'white', 'bg' => 'black'));
182  
183 return;
184 }
185  
186 sub _render_status {
187 my ($self, $rb, $line, $cols) = @_;
188  
189 return if $line < 0 || $cols <= 0;
190  
191 my $left = '+/- count, Tab choice, Space menu, PgUp/PgDn scroll, q quit';
192 my $right = 'v'.$self->{'_version'};
193 my $pen = Tickit::Pen->new('fg' => 'black', 'bg' => 'white');
194  
195 $rb->erase_at($line, 0, $cols, $pen);
196 if (length($right) < $cols) {
197 my $left_width = $cols - length($right) - 1;
198 $left = substr($left, 0, $left_width);
199 $rb->text_at($line, 0, $left, $pen);
200 $rb->text_at($line, $cols - length($right), $right, $pen);
201 } else {
202 $rb->text_at($line, 0, substr($right, 0, $cols), $pen);
203 }
204  
205 return;
206 }
207  
208 sub _change_count {
209 my ($self, $delta) = @_;
210  
211 my $mode = $self->_mode;
212 my $value = $self->{'_counts'}->{$mode} + $delta;
213 $value = 1 if $value < 1;
214 $self->{'_counts'}->{$mode} = $value;
215 $self->_regenerate;
216 $self->redraw;
217  
218 return;
219 }
220  
221 sub _next_choice {
222 my $self = shift;
223  
224 $self->_choose_relative(1);
225  
226 return;
227 }
228  
229 sub _prev_choice {
230 my $self = shift;
231  
232 $self->_choose_relative(-1);
233  
234 return;
235 }
236  
237 sub _regenerate {
238 my $self = shift;
239 my $mode = $self->_mode;
240  
241 if ($mode eq 'paragraphs') {
242 my @paragraphs = $self->{'_lorem'}->paragraphs($self->{'_counts'}->{'paragraphs'});
243 $self->{'_text'} = join "\n\n", @paragraphs;
244 } elsif ($mode eq 'sentences') {
245 $self->{'_text'} = $self->{'_lorem'}->sentences($self->{'_counts'}->{'sentences'});
246 } else {
247 $self->{'_text'} = $self->{'_lorem'}->words($self->{'_counts'}->{'words'});
248 }
249 $self->_sync_text_widget;
250 $self->{'_reset_scroll'} = 1;
251  
252 return;
253 }
254  
255 sub _choose_relative {
256 my ($self, $delta) = @_;
257  
258 my @modes = qw(paragraphs sentences words);
259 my $mode = $self->_mode;
260 my ($index) = grep { $modes[$_] eq $mode } 0 .. $#modes;
261 $index += $delta;
262 $index = $#modes if $index < 0;
263 $index = 0 if $index > $#modes;
264 $self->{'_choice'}->choose_by_value($modes[$index]);
265  
266 return;
267 }
268  
269 sub _mode {
270 my $self = shift;
271  
272 return $self->{'_choice'}->chosen_value;
273 }
274  
275 sub _scroll_page {
276 my ($self, $direction) = @_;
277  
278 my $extent = $self->{'_scrollbox'}->vextent;
279 return if ! defined $extent->viewport;
280  
281 my $amount = int($extent->viewport / 2);
282 $amount = 1 if $amount < 1;
283 $self->{'_scrollbox'}->scroll($direction * $amount, 0);
284  
285 return;
286 }
287  
288 sub _sync_text_widget {
289 my $self = shift;
290  
291 return if ! $self->{'_text_widget'};
292  
293 my $win = $self->window;
294 my $width = $win ? _max(10, $win->cols - 5) : 76;
295 $self->{'_text_widget'}->set_text($self->{'_text'}, $width);
296  
297 return;
298 }
299  
300 sub _min {
301 my ($x, $y) = @_;
302  
303 return $x < $y ? $x : $y;
304 }
305  
306 sub _max {
307 my ($x, $y) = @_;
308  
309 return $x > $y ? $x : $y;
310 }
311  
312 1;
313  
314 __END__
« blib/lib/App/Lorem/Tickit/TextWidget.pm ↑ Summary