blib/lib/App/Asciio/Text/Asciio.pm untested

filter: Statementstmt 0.0% 0 / 301 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 113 MC/DCmcdc 0.0% 0 / 61 Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 601 CC 42 SCAR 68.8
CC
42
Cov
0%
update_display
1722.0
render_text
6.0
new
2.0
CRAP 968.1
1  
2 package App::Asciio::Text ;
3 use base App::Asciio ;
4  
5 use strict;
6 use warnings;
7  
8 use List::MoreUtils qw(minmax) ;
9 use Term::Size::Any qw(chars) ;
10 use Term::ANSIColor ;
11 use Time::HiRes qw(gettimeofday) ;
12 use Sereal qw(get_sereal_decoder get_sereal_encoder) ;
13 use Sereal::Encoder qw(SRL_SNAPPY SRL_ZLIB SRL_ZSTD) ;
14  
15 use App::Asciio::Text::Asciio::stripes::editable_arrow2;
16 use App::Asciio::Text::Asciio::stripes::wirl_arrow ;
17 use App::Asciio::Text::Asciio::stripes::section_wirl_arrow ;
18 use App::Asciio::Text::Asciio::stripes::editable_box2;
19 use App::Asciio::Text::Asciio::stripes::editable_exec_box;
20 use App::Asciio::stripes::rhombus;
21 use App::Asciio::stripes::ellipse;
22  
23 use App::Asciio::Text::Asciio::Dialogs ;
24 use App::Asciio::Text::Asciio::Menues ;
25  
26 use App::Asciio::String ;
27  
28 #-----------------------------------------------------------------------------
29  
30 our $VERSION = '0.01' ;
31  
32 #-----------------------------------------------------------------------------
33  
34 =head1 NAME
35  
36 =cut
37  
38 sub new
39 {
40 my ($class, $width, $height) = @_ ;
41  
42 my $self = App::Asciio->new($class) ;
43 $self->{UI} = 'TUI' ;
44  
45 bless $self, $class ;
46  
47 return($self) ;
48 }
49  
50 #-----------------------------------------------------------------------------
51  
52 sub render_text
53 {
54 my ($text_array, $COLS, $ROWS) = @_ ;
55  
56 my $rendering = '' ;
57 my ($text, $previous_color , $color) = ('', '', '') ;
58  
59 for my $line (1 .. $ROWS)
60 {
61 my $line_ref = $text_array->[$line] ;
62
63 for my $character (1 .. $COLS)
64 {
65 ($text, $color) = @{$line_ref->[$character]} ;
66
67 $rendering .= "\e[m$color" if $color ne $previous_color ;
68 $previous_color = $color ;
69
70 $rendering .= $text ;
71 }
72 }
73  
74 print $rendering ;
75 print "\e[m" ;
76 }
77  
78 #-----------------------------------------------------------------------------
79  
80 my ($grid_setup_text, $background_rendered, $grid_rendered) ;
81 my $encoder = get_sereal_encoder({compress => SRL_ZLIB}) ;
82 my $decoder = get_sereal_decoder() ;
83  
84 sub update_display
85 {
86 my ($self) = @_;
87  
88 $self->SUPER::update_display() ;
89  
90 my ($COLS, $ROWS) = chars ;
91 my $rows_cols = "$ROWS-$COLS" ; # cache tag
92  
93 print "\e[?25l\e[H" ; # hide cursor, reset position to 1,1
94  
95 my $text_array = [] ;
96  
97 # draw background
98 if(defined $background_rendered && $background_rendered eq $rows_cols)
99 {
100 $text_array = $decoder->decode($grid_setup_text) ;
101 }
102 else
103 {
104 for my $line (1 .. $ROWS)
105 {
106 for my $column (1 .. $COLS)
107 {
108 $text_array->[$line][$column] = [' ', ''] ;
109 }
110 }
111
112 $grid_setup_text = $encoder->encode($text_array) ;
113 $background_rendered = $rows_cols ;
114 }
115  
116 if($self->{DISPLAY_GRID})
117 {
118 unless(defined $grid_rendered && $grid_rendered eq $rows_cols)
119 {
120 if($self->{DISPLAY_GRID})
121 {
122 my $color = color($self->get_color('grid')) ;
123
124 for my $line (1 .. $ROWS /10)
125 {
126 for (1 .. $COLS)
127 {
128 $text_array->[$line * 10][$_] = ['-', $color] ;
129 }
130 }
131
132 for my $line (1 .. $COLS / 10)
133 {
134 for (1 .. $ROWS)
135 {
136 $text_array->[$_][$line * 10] = ['|', $color] ;
137 }
138 }
139 }
140
141 $grid_setup_text = $encoder->encode($text_array) ;
142 $grid_rendered = $rows_cols ;
143 }
144 }
145  
146 # draw ruler lines
147 for my $ruler (@{$self->{RULER_LINES}})
148 {
149 my $color = color($self->get_color('ruler_line')) ;
150
151 if($ruler->{TYPE} eq 'VERTICAL')
152 {
153 $text_array->[$_][$ruler->{POSITION}] = ['|', $color] for (1 .. $ROWS) ;
154 }
155 else
156 {
157 $text_array->[$ruler->{POSITION}][$_] = ['-', $color] for (1 .. $COLS) ;
158 }
159 }
160  
161 # draw elements
162 my $element_index = 0 ;
163  
164 for my $element (@{$self->{ELEMENTS}})
165 {
166 $element_index++ ;
167 my $is_selected = $element->{SELECTED} // 0 ;
168 $is_selected = 1 if $is_selected > 0 ;
169
170 my ($background_color, $foreground_color) = $element->get_colors() ;
171
172 if($is_selected)
173 {
174 if(exists $element->{GROUP} and defined $element->{GROUP}[-1])
175 {
176 $background_color = $element->{GROUP}[-1]{GROUP_COLOR} ;
177 }
178 else
179 {
180 $background_color = $self->get_color('selected_element_background');
181 }
182 }
183 else
184 {
185 unless (defined $background_color)
186 {
187 if(exists $element->{GROUP} and defined $element->{GROUP}[-1])
188 {
189 $background_color = $element->{GROUP}[-1]{GROUP_COLOR} ;
190 }
191 else
192 {
193 $background_color = $self->get_color('element_background') ;
194 }
195 }
196 }
197
198 $foreground_color //= $self->get_color('element_foreground') ;
199
200 my $color_set = $is_selected . '-'
201 . ($background_color // 'undef') . '-' . ($foreground_color // 'undef') . '-'
202 . ($self->{OPAQUE_ELEMENTS} // 1) . '-' . ($self->{NUMBERED_OBJECTS} // 0) ;
203
204 my $stripes = $element->get_stripes() ;
205 # $self->update_quadrants($element) ;
206
207 for my $strip (@{$stripes})
208 {
209 my $line_index = 0 ;
210
211 for my $line (split /\n/, $strip->{TEXT})
212 {
213 my $strip_color = color($foreground_color) . color($background_color) ;
214 $strip_color .= color($self->get_color('selected_element_background')) if $is_selected ;
215
216 my $line_length = unicode_length $line ;
217 $line = sprintf "%0${line_length}d", $element_index if $self->{NUMBERED_OBJECTS} ;
218
219 my $character_index = 0 ;
220
221 for (split //, $line)
222 {
223 $character_index++ ;
224
225 my $Y = $element->{Y} + $strip->{Y_OFFSET} + $line_index + 1 ;
226 next if $Y < 1 || $Y > $ROWS ;
227
228 my $X = $element->{X} + $strip->{X_OFFSET} + $character_index ;
229 next if $X < 1 || $X > $COLS ;
230
231 $text_array->[$Y][$X] = [$_, $strip_color] ;
232 }
233
234 $line_index++ ;
235 }
236 }
237 }
238  
239 # draw connections
240 my (%connected_connections, %connected_connectors) ;
241  
242 my $connector_point_color = color($self->get_color('connector_point'));
243 my $connector_point_rendering = "O" ;
244  
245 my $connection_point_color = color($self->get_color('connection_point'));
246 my $connection_point_rendering = "o" ;
247  
248 my $extra_point_color = color($self->get_color('extra_point'));
249 my $extra_point_rendering = "#" ;
250  
251 for my $element (grep {$self->is_over_element($_, $self->{MOUSE_X}, $self->{MOUSE_Y}, 1)} @{$self->{ELEMENTS}})
252 {
253 for my $connector ($element->get_connector_points())
254 {
255 next if exists $connected_connectors{$element}{$connector->{X}}{$connector->{Y}} ;
256
257 my $column = $element->{X} + $connector->{X} + 1 ;
258 my $line = $connector->{Y} + $element->{Y} + 1 ;
259
260 unless($column < 1 || $column > $COLS || $line < 1 || $line > $ROWS)
261 {
262 $text_array->[$line][$column] = [$connector_point_rendering, $connector_point_color] ;
263 }
264 }
265
266 for my $extra_point ($element->get_extra_points())
267 {
268 my $column = $extra_point->{X} + $element->{X} + 1 ;
269 my $line = $extra_point->{Y} + $element->{Y} + 1 ;
270
271 unless($column < 1 || $column > $COLS || $line < 1 || $line > $ROWS)
272 {
273 $text_array->[$line][$column] = [$extra_point_rendering, $extra_point_color] ;
274 }
275 }
276
277 if($self->{DRAW_CONNECTION_POINTS})
278 {
279 for my $connection_point ($element->get_connection_points())
280 {
281 next if exists $connected_connections{$element}{$connection_point->{X}}{$connection_point->{Y}} ;
282
283 my $column = $connection_point->{X} + $element->{X} + 1 ;
284 my $line = $connection_point->{Y} + $element->{Y} + 1 ;
285
286 unless($column < 1 || $column > $COLS || $line < 1 || $line > $ROWS)
287 {
288 $text_array->[$line][$column] = [$connection_point_rendering, $connection_point_color] ;
289 }
290 }
291 }
292 }
293  
294 for my $connection (@{$self->{CONNECTIONS}})
295 {
296 my $draw_connection ;
297 my $connector ;
298
299 if($self->is_over_element($connection->{CONNECTED}, $self->{MOUSE_X}, $self->{MOUSE_Y}, 1))
300 {
301 $draw_connection++ ;
302
303 $connector = $connection->{CONNECTED}->get_named_connection($connection->{CONNECTOR}{NAME}) ;
304 $connected_connectors{$connection->{CONNECTED}}{$connector->{X}}{$connector->{Y}}++ ;
305 }
306
307 if($self->is_over_element($connection->{CONNECTEE}, $self->{MOUSE_X}, $self->{MOUSE_Y}, 1))
308 {
309 $draw_connection++ ;
310
311 my $connectee_connection = $connection->{CONNECTEE}->get_named_connection($connection->{CONNECTION}{NAME}) ;
312
313 if($connectee_connection)
314 {
315 $connected_connectors{$connection->{CONNECTEE}}{$connectee_connection->{X}}{$connectee_connection->{Y}}++ ;
316 }
317 }
318
319 if($draw_connection)
320 {
321 $connector ||= $connection->{CONNECTED}->get_named_connection($connection->{CONNECTOR}{NAME}) ;
322
323 my $connection_color = color($self->get_color('connection'));
324 my $connection_char = $connector->{CHAR} // 'c' ;
325 my $connection_rendering = "$connection_char" ;
326
327 my $column = $connector->{X} + $connection->{CONNECTED}{X} + 1 ;
328 my $line = $connector->{Y} + $connection->{CONNECTED}{Y} + 1 ;
329
330 unless($column < 1 || $column > $COLS || $line < 1 || $line > $ROWS)
331 {
332 $text_array->[$line][$column] = [$connection_rendering, $connection_color] ;
333 }
334 }
335 }
336  
337 # draw new connections
338 my $new_connection_color = color($self->get_color('new_connection'));
339 my $connection_rendering = "c" ;
340 for my $new_connection (@{$self->{NEW_CONNECTIONS}})
341 {
342 my $end_connection = $new_connection->{CONNECTED}->get_named_connection($new_connection->{CONNECTOR}{NAME}) ;
343
344 my $column = $end_connection->{X} + $new_connection->{CONNECTED}{X} + 1 ;
345 my $line = $end_connection->{Y} + $new_connection->{CONNECTED}{Y} + 1 ;
346
347 unless($column < 1 || $column > $COLS || $line < 1 || $line > $ROWS)
348 {
349 $text_array->[$line][$column] = [$connection_rendering, $new_connection_color] ;
350 }
351 }
352  
353 delete $self->{NEW_CONNECTIONS} ;
354  
355 my $selection_rectangle = '' ;
356 if(defined $self->{SELECTION_RECTANGLE}{END_X})
357 {
358 my $start_x = $self->{SELECTION_RECTANGLE}{START_X} + 1 ;
359 my $start_y = $self->{SELECTION_RECTANGLE}{START_Y} + 1 ;
360 my $end_x = $self->{SELECTION_RECTANGLE}{END_X} + 1 ;
361 my $end_y = $self->{SELECTION_RECTANGLE}{END_Y} + 1 ;
362
363 ($start_x, $end_x) = ($end_x, $start_x) if $end_x < $start_x ;
364 ($start_y, $end_y) = ($end_y, $start_y) if $end_y < $start_y ;
365
366 my $width = $end_x - $start_x + 1 ;
367 my $height = $end_y - $start_y + 1 ;
368
369 $selection_rectangle = "$start_x - $start_y * $end_x - $end_y" ;
370
371 my $color = color($self->get_color('selection_rectangle')) ;
372
373 for ($start_y .. $start_y + $height - 1)
374 {
375 next if $_ < 1 || $_ > $ROWS ;
376
377 $text_array->[$_][$start_x] = ['|', $color] if $start_x > 0 && $start_x <= $COLS ;
378 $text_array->[$_][$end_x] = ['|', $color] if $end_x > 1 && $end_x <= $COLS ;
379 }
380
381 if($start_x <= $COLS && $end_x > 0)
382 {
383 my $top_bottom = '-' x $width ;
384 $top_bottom = substr($top_bottom, -$start_x + 1) and $start_x = 1 if $start_x < 1 ;
385 $top_bottom = substr($top_bottom, 0, ($COLS - $start_x) + 1) if $end_x > $COLS ;
386
387 if($start_y > 0 && $start_y <= $ROWS)
388 {
389 my $character_index = 0 ;
390 for (1 .. unicode_length($top_bottom))
391 {
392 $text_array->[$start_y][$start_x + $character_index] = ['-', $color] ;
393 $character_index++ ;
394 }
395 }
396
397 if($end_y > 0 && $end_y <= $ROWS)
398 {
399 my $character_index = 0 ;
400
401 for (1 .. unicode_length($top_bottom))
402 {
403 $text_array->[$end_y][$start_x + $character_index] = ['-', $color] ;
404 $character_index++ ;
405 }
406 }
407 }
408
409 delete $self->{SELECTION_RECTANGLE}{END_X} ;
410 }
411  
412 if ($self->{MOUSE_TOGGLE})
413 {
414 $text_array->[$self->{MOUSE_Y} + 1][$self->{MOUSE_X} + 1] = ['X', color($self->get_color('mouse_rectangle'))]
415 unless($self->{MOUSE_X} < 0 || $self->{MOUSE_X} >= $COLS || $self->{MOUSE_Y} < 0 || $self->{MOUSE_Y} >= $ROWS) ;
416 }
417  
418 render_text($text_array, $COLS, $ROWS) ;
419  
420 if(defined $self->{ACTION_VERBOSE})
421 {
422 print "\e[2;81H\e[32m$self->{LAST_ACTION} " ;
423 print "\e[3;81H$selection_rectangle" ;
424 }
425 }
426  
427 #-----------------------------------------------------------------------------
428  
429 sub key_press_event
430 {
431 my ($self, $key_name, $modifiers)= @_;
432  
433 $self->SUPER::key_press_event
434 ({
435 TYPE => "key-press",
436 STATE => '',
437 MODIFIERS => $modifiers,
438 BUTTON => -1,
439 KEY_NAME => $key_name,
440 COORDINATES => [0, 0],
441 }) ;
442 }
443  
444 #-----------------------------------------------------------------------------
445  
446 sub button_press_event
447 {
448 my ($self, $modifiers, $button, $x, $y)= @_;
449  
450 $self->SUPER::button_press_event
451 ({
452 TIME => 0,
453 TYPE => "button-press",
454 STATE => '',
455 MODIFIERS => "$modifiers-",
456 BUTTON => $button,
457 KEY_NAME => -1,
458 COORDINATES => [$x, $y],
459 }) ;
460 }
461  
462 #-----------------------------------------------------------------------------
463  
464 sub button_release_event
465 {
466 my ($self, $modifiers, $button, $x, $y)= @_;
467  
468 $self->SUPER::button_release_event
469 ({
470 TIME => 0,
471 TYPE => "button-release",
472 STATE => '',
473 MODIFIERS => "$modifiers-",
474 BUTTON => $button,
475 KEY_NAME => -1,
476 COORDINATES => [$x, $y],
477 }) ;
478 }
479  
480 #-----------------------------------------------------------------------------
481  
482 sub motion_notify_event
483 {
484 my ($self, $modifiers, $button, $x, $y)= @_;
485  
486 $self->SUPER::motion_notify_event
487 ({
488 TIME => 0,
489 TYPE => "motion-notify",
490 STATE => '',
491 MODIFIERS => "$modifiers-",
492 BUTTON => $button,
493 KEY_NAME => -1,
494 COORDINATES => [$x, $y],
495 }) ;
496 }
497  
498 #-----------------------------------------------------------------------------
499  
500 sub motion_dragging_event
501 {
502 my ($self, $modifiers, $button, $x, $y)= @_;
503  
504 $self->SUPER::motion_notify_event
505 ({
506 TIME => 0,
507 TYPE => "motion-notify",
508 STATE => "dragging-button$button",
509 MODIFIERS => "$modifiers-",
510 BUTTON => $button,
511 KEY_NAME => -1,
512 COORDINATES => [$x, $y],
513 }) ;
514 }
515  
516 #-----------------------------------------------------------------------------
517  
518 sub get_key_modifiers
519 {
520 my ($key) = @_ ;
521  
522 my $modifiers = '???' ;
523 return($modifiers) ;
524 }
525  
526 #-----------------------------------------------------------------------------
527  
528 use Term::ReadKey;
529  
530 sub exit
531 {
532 my ($self, $code) = @_ ;
533  
534 ReadMode('normal') ;
535 print "\e[2J\e[H" ;
536 print "\e[?25h" ;
537 print "\e[?1000h" ;
538  
539 exit ($code // 0) ;
540 }
541  
542 #-----------------------------------------------------------------------------
543  
544 sub get_character_size { my ($self) = @_ ; return 1, 1 ; }
545  
546 #-----------------------------------------------------------------------------
547  
548 sub invalidate_rendering_cache
549 {
550 my ($self) = @_ ;
551  
552 for my $element (@{$self->{ELEMENTS}})
553 {
554 delete $element->{CACHE} ;
555 }
556  
557 delete $self->{CACHE} ;
558 }
559  
560 #----------------------------------------------------------------------------------------------
561  
562  
563 =head1 DEPENDENCIES
564  
565  
566 =head1 AUTHOR
567  
568 Khemir Nadim ibn Hamouda
569 CPAN ID: NKH
570 mailto:nadim@khemir.net
571  
572 =head1 LICENSE AND COPYRIGHT
573  
574 This program is free software; you can redistribute
575 it and/or modify it under the same terms as Perl itself.
576  
577 =cut
578  
579 #------------------------------------------------------------------------------------------------------
580  
581 "Terinal world domination!" ;
582