Widget/Entry.pm untested

filter: Statementstmt 0.0% 0 / 211 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 8 Podpod n/a totaltotal 0.0% 0 / 355 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2011-2023 -- leonerd@leonerd.org.uk
5  
6 use v5.20;
7 use warnings;
8 use Object::Pad 0.807;
9  
10 package Tickit::Widget::Entry 0.42;
11 class Tickit::Widget::Entry :strict(params);
12  
13 inherit Tickit::Widget;
14  
15 use Carp;
16  
17 use Tickit::Style;
18 Tickit::Window->VERSION( '0.39' ); # expose_after_scroll default on
19  
20 use Tickit::Utils qw( textwidth chars2cols cols2chars substrwidth );
21  
22 use constant CAN_FOCUS => 1;
23  
24 # Positions in this code can get complicated. The following conventions apply:
25 # $pos_ch = a position in CHaracters within a Unicode string (length, substr,..)
26 # $pos_co = a position in screen COlumns counted from the start of the string
27 # $pos_x = a position in screen columns from the start of the window ($win positions)
28  
29 =head1 NAME
30  
31 C<Tickit::Widget::Entry> - a widget for entering text
32  
33 =head1 SYNOPSIS
34  
35 use Tickit;
36 use Tickit::Widget::Entry;
37  
38 my $entry = Tickit::Widget::Entry->new(
39 on_enter => sub {
40 my ( $self, $line ) = @_;
41  
42 # process $line somehow
43  
44 $self->set_text( "" );
45 },
46 );
47  
48 Tickit->new( root => $entry )->run;
49  
50 =head1 DESCRIPTION
51  
52 This class provides a widget which allows the user to enter a line of text.
53  
54 =head1 STYLE
55  
56 The default style pen is used as the widget pen. The following style pen
57 prefixes are also used:
58  
59 =over 4
60  
61 =item more => PEN
62  
63 The pen used for the "more" scroll markers
64  
65 =back
66  
67 The following style keys are used:
68  
69 =over 4
70  
71 =item more_left => STRING
72  
73 =item more_right => STRING
74  
75 The text used to indicate that there is more content scrolled to the left or
76 right, respectively
77  
78 =back
79  
80 =cut
81  
82 style_definition base =>
83 more_fg => "cyan",
84 more_left => "<..",
85 more_right => "..>";
86  
87 use constant WIDGET_PEN_FROM_STYLE => 1;
88  
89 =head1 KEYBINDINGS
90  
91 The following keys are bound by default
92  
93 =over 2
94  
95 =item * Ctrl-K
96  
97 Delete the entire line
98  
99 =item * Ctrl-U
100  
101 Delete to the start of the line
102  
103 =item * Ctrl-W or Ctrl-Backspace
104  
105 Delete one word backwards
106  
107 =item * Backspace
108  
109 Delete one character backwards
110  
111 =item * Delete
112  
113 Delete one character forwards
114  
115 =item * Ctrl-Delete
116  
117 Delete one word forwards
118  
119 =item * End or Ctrl-E
120  
121 Move the cursor to the end of the input line
122  
123 =item * Enter
124  
125 Accept a line of input by running the C<on_enter> action
126  
127 =item * Home or Ctrl-A
128  
129 Move the cursor to the beginning of the input line
130  
131 =item * Insert
132  
133 Toggle between overwrite and insert mode
134  
135 =item * Left
136  
137 Move the cursor one character left
138  
139 =item * Ctrl-Left or Alt-B
140  
141 Move the cursor one word left
142  
143 =item * Right
144  
145 Move the cursor one character right
146  
147 =item * Ctrl-Right or Alt-F
148  
149 Move the cursor one word right
150  
151 =back
152  
153 =cut
154  
155 =head1 CONSTRUCTOR
156  
157 =cut
158  
159 =head2 new
160  
161 $entry = Tickit::Widget::Entry->new( %args );
162  
163 Constructs a new C<Tickit::Widget::Entry> object.
164  
165 Takes the following named arguments:
166  
167 =over 8
168  
169 =item text => STR
170  
171 Optional. Initial text to display in the box
172  
173 =item position => INT
174  
175 Optional. Initial position of the cursor within the text.
176  
177 =item on_enter => CODE
178  
179 Optional. Callback function to invoke when the C<< <Enter> >> key is pressed.
180  
181 =back
182  
183 =cut
184  
185 field $_text :reader :param //= "";
186 field $_pos_ch :reader(position) :param(position) //= 0;
187 field $_scrolloffs_co = 0;
188 field $_overwrite = 0;
189 field %_keybindings = (
190 'C-a' => "key_beginning_of_line",
191 'C-e' => "key_end_of_line",
192 'C-k' => "key_delete_line",
193 'C-u' => "key_backward_delete_line",
194 'C-w' => "key_backward_delete_word",
195  
196 'M-b' => "key_backward_word",
197 'M-f' => "key_forward_word",
198  
199 'Backspace' => "key_backward_delete_char",
200 'C-Backspace' => "key_backward_delete_word",
201 'Delete' => "key_forward_delete_char",
202 'C-Delete' => "key_forward_delete_word",
203 'End' => "key_end_of_line",
204 'Enter' => "key_enter_line",
205 'Home' => "key_beginning_of_line",
206 'Insert' => "key_overwrite_mode",
207 'Left' => "key_backward_char",
208 'C-Left' => "key_backward_word",
209 'Right' => "key_forward_char",
210 'C-Right' => "key_forward_word",
211 );
212  
213 field $_on_enter :reader :param = undef;
214  
215 ADJUST
216 {
217 my $textlen = length $_text;
218 $_pos_ch = $textlen if $_pos_ch > $textlen;
219  
220 # Since we take keyboard input we almost certainly want to take focus here
221 $self->take_focus;
222 }
223  
224 method lines { 1 }
225 method cols { 5 }
226  
227 method char2col
228 {
229 my ( $ch ) = @_;
230  
231 return scalar chars2cols $_text, $ch;
232 }
233  
234 method pretext_width
235 {
236 return 0 if $_scrolloffs_co == 0;
237 return textwidth( $self->get_style_values( "more_left" ) );
238 }
239  
240 method pretext_render
241 {
242 my ( $rb ) = @_;
243  
244 $rb->text_at( 0, 0, $self->get_style_values( "more_left" ), $self->get_style_pen( "more" ) );
245 }
246  
247 method posttext_width
248 {
249 return 0 if textwidth( $self->text ) <= $_scrolloffs_co + $self->window->cols;
250 return textwidth( $self->get_style_values( "more_right" ) );
251 }
252  
253 method posttext_render
254 {
255 my ( $rb ) = @_;
256  
257 $rb->text_at( 0, 0, $self->get_style_values( "more_right" ), $self->get_style_pen( "more" ) );
258 }
259  
260 method render_to_rb
261 {
262 my ( $rb, $rect ) = @_;
263  
264 my $cols = $self->window->cols;
265  
266 if( $rect->top == 0 ) {
267 my $text = substrwidth( $self->text, $_scrolloffs_co, $cols );
268  
269 $rb->goto( 0, 0 );
270 $rb->text( $text ) if length $text;
271 $rb->erase_to( $cols );
272  
273 if( my $pretext_width = $self->pretext_width ) {
274 $rb->save;
275 $rb->clip( Tickit::Rect->new( top => 0, left => 0, lines => 1, cols => $pretext_width ) );
276  
277 $self->pretext_render( $rb );
278  
279 $rb->restore;
280 }
281  
282 if( my $posttext_width = $self->posttext_width ) {
283 $rb->save;
284 $rb->translate( 0, $cols - $posttext_width );
285 $rb->clip( Tickit::Rect->new( top => 0, left => 0, lines => 1, cols => $posttext_width ) );
286  
287 $self->posttext_render( $rb );
288  
289 $rb->restore;
290 }
291 }
292  
293 foreach my $line ( $rect->linerange( 1, undef ) ) {
294 $rb->erase_at( $line, 0, $cols );
295 }
296  
297 $self->reposition_cursor;
298 }
299  
300 method _recalculate_scroll
301 {
302 my ( $pos_ch ) = @_;
303  
304 my $pos_co = $self->char2col( $pos_ch );
305 my $off_co = $_scrolloffs_co;
306  
307 my $pos_x = $pos_co - $off_co;
308  
309 my $width = $self->window->cols;
310 my $halfwidth = int( $width / 2 );
311  
312 # Don't even try unless we have at least 2 columns
313 return unless $halfwidth;
314  
315 # Try to keep the cursor within 5 columns of the window edge
316 while( $pos_x < 5 and $off_co >= 5 ) {
317 $off_co -= $halfwidth;
318 $off_co = 0 if $off_co < 0;
319 $pos_x = $pos_co - $off_co;
320 }
321 while( $pos_x > ( $width - 5 ) ) {
322 $off_co += $halfwidth;
323 $pos_x = $pos_co - $off_co;
324 }
325  
326 return $off_co if $off_co != $_scrolloffs_co;
327 return undef;
328 }
329  
330 method reposition_cursor
331 {
332 my ( $pos_ch ) = @_;
333  
334 $_pos_ch = $pos_ch if defined $pos_ch;
335  
336 my $win = $self->window or return;
337  
338 my $new_scrolloffs = $self->_recalculate_scroll( $_pos_ch );
339 if( defined $new_scrolloffs ) {
340 $_scrolloffs_co = $new_scrolloffs;
341 $self->redraw;
342 }
343  
344 my $pos_x = $self->char2col( $_pos_ch ) - $_scrolloffs_co;
345  
346 $win->cursor_at( 0, $pos_x );
347 }
348  
349 method _text_spliced
350 {
351 my ( $pos_ch, $deleted, $inserted, $at_end ) = @_;
352  
353 my $win = $self->window;
354 my $width = $win->cols;
355  
356 my $insertedlen_co = textwidth $inserted;
357 my $deletedlen_co = textwidth $deleted;
358  
359 my $delta_co = $insertedlen_co - $deletedlen_co;
360  
361 my $pos_co = $self->char2col( $pos_ch );
362 my $pos_x = $pos_co - $_scrolloffs_co;
363  
364 # Don't bother at all if the affected range is scrolled off the right
365 return if $pos_x >= $width;
366  
367 if( $pos_x < 0 ) {
368 die "TODO: text_splice before window - what to do??\n";
369 }
370  
371 my $need_reprint = 0;
372  
373 # No point doing a scrollrect if there's nothing after here
374 if( $delta_co != 0 and !$at_end ) {
375 $win->scrollrect( 0, $pos_x, 1, $win->cols - $pos_x, 0, -$delta_co ) or
376 $need_reprint = 1;
377 }
378  
379 if( $need_reprint ) {
380 # ICH/DCH failed; we'll have to reprint the entire rest of the line from
381 # here
382 $win->expose(
383 Tickit::Rect->new( top => 0, left => $pos_x, lines => 1, right => $width )
384 );
385 return;
386 }
387  
388 if( $insertedlen_co > 0 ) {
389 $win->expose(
390 Tickit::Rect->new( top => 0, left => $pos_x, lines => 1, cols => $insertedlen_co )
391 );
392  
393 if( my $posttext_width = $self->posttext_width ) {
394 $win->expose(
395 Tickit::Rect->new( top => 0, left => $width - $posttext_width, lines => 1, right => $width )
396 );
397 }
398 }
399  
400 if( $delta_co < 0 and $_scrolloffs_co + $width < textwidth $self->text ) {
401 # Add extra damage to redraw the trashed posttext marker
402 my $rhs_x = -$delta_co + $self->posttext_width;
403  
404 $win->expose(
405 Tickit::Rect->new( top => 0, left => $width - $rhs_x, lines => 1, right => $width )
406 );
407 }
408 }
409  
410 method on_key
411 {
412 my ( $args ) = @_;
413  
414 return 0 unless $self->window->is_focused;
415  
416 my $type = $args->type;
417 my $str = $args->str;
418  
419 if( $type eq "key" and my $code = $_keybindings{$str} ) {
420 $self->$code( $str );
421 return 1;
422 }
423 if( $type eq "text" ) {
424 $self->on_text( $str );
425 return 1;
426 }
427  
428 return 0;
429 }
430  
431 method on_text
432 {
433 my ( $text ) = @_;
434  
435 $self->text_splice( $_pos_ch, $_overwrite ? 1 : 0, $text );
436 }
437  
438 method on_mouse
439 {
440 my ( $args ) = @_;
441  
442 return unless $args->type eq "press" and $args->button == 1;
443  
444 my $pos_ch = scalar cols2chars $_text, $args->col + $_scrolloffs_co;
445 $self->set_position( $pos_ch );
446 }
447  
448 =head1 ACCESSORS
449  
450 =cut
451  
452 =head2 on_enter
453  
454 $on_enter = $entry->on_enter;
455  
456 =head2 set_on_enter
457  
458 $entry->set_on_enter( $on_enter );
459  
460 Return or set the CODE reference to be called when the C<key_enter_line>
461 action is invoked; usually bound to the C<Enter> key.
462  
463 $on_enter->( $entry, $line );
464  
465 =cut
466  
467 # generated accessor
468  
469 method set_on_enter
470 {
471 ( $_on_enter ) = @_;
472 }
473  
474 =head2 position
475  
476 $offset = $entry->position;
477  
478 Returns the current entry position, in terms of characters within the text.
479  
480 =cut
481  
482 # generated accessor
483  
484 =head2 set_position
485  
486 $entry->set_position( $position );
487  
488 Set the text entry position, moving the cursor
489  
490 =cut
491  
492 method set_position
493 {
494 my ( $pos_ch ) = @_;
495  
496 $pos_ch = 0 if $pos_ch < 0;
497 $pos_ch = length $_text if $pos_ch > length $_text;
498  
499 $self->reposition_cursor( $pos_ch );
500 }
501  
502 =head1 METHODS
503  
504 =cut
505  
506 =head2 bind_keys
507  
508 $entry->bind_keys( $keystr => $value, ... );
509  
510 Associate methods or CODE references with keypresses. On receipt of a the key
511 the method or CODE reference will be invoked, being passed the stringified key
512 representation and the underlying C<Term::TermKey::Key> structure.
513  
514 $ret = $entry->method( $keystr, $key );
515 $ret = $coderef->( $entry, $keystr, $key );
516  
517 This method takes a hash of keystring/value pairs. Binding a value of C<undef>
518 will remove it.
519  
520 =cut
521  
522 method bind_keys
523 {
524 while( @_ ) {
525 my $str = shift;
526 my $value = shift;
527  
528 if( defined $value ) {
529 $_keybindings{$str} = $value;
530 }
531 else {
532 delete $_keybindings{$str};
533 }
534 }
535 }
536  
537 =head2 make_popup_at_cursor
538  
539 $win = $entry->make_popup_at_cursor( $top_offset, $left_offset, $lines, $cols );
540  
541 I<Since version 0.33.>
542  
543 Creates a new popup window, as if calling L<Tickit::Window/make_popup> on the
544 widget's main window, but with an offset relative to the current cursor
545 position.
546  
547 An offet of (0, 0) will position the popup window's top left corner exactly over
548 the cursor; this is likely not what you want.
549  
550 To position the popup just below the widget, use a top offset of +1:
551  
552 $win = $entry->make_popup_at_cursor( +1, 0, $lines, $cols );
553  
554 To position the popup just above the widget, use a top offset of negative the
555 number of lines:
556  
557 $win = $entry->make_popup_at_cursor( -$lines, 0, $lines, $cols );
558  
559 =cut
560  
561 method make_popup_at_cursor
562 {
563 my ( $topoff, $leftoff, $lines, $cols ) = @_;
564  
565 $self->window or
566 croak "Cannot ->make_popup_at_cursor on an Entry widget with no window";
567  
568 my $pos_x = $self->char2col( $_pos_ch ) - $_scrolloffs_co;
569  
570 return $self->window->make_popup( $topoff, $pos_x + $leftoff, $lines, $cols );
571 }
572  
573 =head1 TEXT MODEL METHODS
574  
575 These methods operate on the text input buffer directly, updating the stored
576 text and changing the rendered display to reflect the changes. They can be
577 used by a program to directly manipulate the text.
578  
579 =cut
580  
581 =head2 text
582  
583 $text = $entry->text;
584  
585 Returns the currently entered text.
586  
587 =cut
588  
589 # generated accessor
590  
591 =head2 set_text
592  
593 $entry->set_text( $text );
594  
595 Replace the text in the entry box. This completely redraws the widget's
596 window. It is largely provided for initialisation; for normal edits (such as
597 from keybindings), it is preferable to use C<text_insert>, C<text_delete> or
598 C<text_splice>.
599  
600 =cut
601  
602 method set_text
603 {
604 my ( $text ) = @_;
605  
606 $_text = $text;
607 $_pos_ch = length $text if $_pos_ch > length $text;
608  
609 $self->redraw;
610 }
611  
612 =head2 text_insert
613  
614 $entry->text_insert( $text, $pos_ch );
615  
616 Insert the given text at the given character position.
617  
618 =cut
619  
620 method text_insert
621 {
622 my ( $text, $pos_ch ) = @_;
623  
624 $self->text_splice( $pos_ch, 0, $text );
625 }
626  
627 =head2 text_delete
628  
629 $deleted = $entry->text_delete( $pos_ch, $len_ch );
630  
631 Delete the given section of text. Returns the deleted text.
632  
633 =cut
634  
635 method text_delete
636 {
637 my ( $pos_ch, $len_ch ) = @_;
638  
639 return $self->text_splice( $pos_ch, $len_ch, "" );
640 }
641  
642 =head2 text_splice
643  
644 $deleted = $entry->text_splice( $pos_ch, $len_ch, $text );
645  
646 Replace the given section of text with the given replacement. Returns the
647 text deleted from the section.
648  
649 =cut
650  
651 method text_splice
652 {
653 my ( $pos_ch, $len_ch, $text ) = @_;
654  
655 my $textlen_ch = length($text);
656  
657 my $delta_ch = $textlen_ch - $len_ch;
658  
659 my $at_end = ( $pos_ch == length $_text );
660  
661 my $deleted = substr( $_text, $pos_ch, $len_ch, $text );
662  
663 my $new_pos_ch;
664  
665 if( $_pos_ch >= $pos_ch + $len_ch ) {
666 # Cursor after splice; move to suit
667 $new_pos_ch = $self->position + $delta_ch;
668 }
669 elsif( $_pos_ch >= $pos_ch ) {
670 # Cursor within splice; move to end
671 $new_pos_ch = $pos_ch + $textlen_ch;
672 }
673 # else { ignore }
674  
675 # No point incrementally updating as we'll have to scroll anyway
676 unless( defined $new_pos_ch and defined $self->_recalculate_scroll( $new_pos_ch ) ) {
677 $self->_text_spliced( $pos_ch, $deleted, $text, $at_end );
678 }
679  
680 $self->reposition_cursor( $new_pos_ch ) if defined $new_pos_ch and $new_pos_ch != $_pos_ch;
681  
682 return $deleted;
683 }
684  
685 =head2 find_bow_forward
686  
687 $pos = $entry->find_bow_forward( $initial, $else );
688  
689 Search forward in the string, returning the character position of the next
690 beginning of word from the initial position. If none is found, returns
691 C<$else>.
692  
693 =cut
694  
695 method find_bow_forward
696 {
697 my ( $pos, $else ) = @_;
698  
699 my $posttext = substr( $self->text, $pos );
700  
701 return $posttext =~ m/(?<=\s)\S/ ? $pos + $-[0] : $else;
702 }
703  
704 =head2 find_eow_forward
705  
706 $pos = $entry->find_eow_forward( $initial )
707  
708 Search forward in the string, returning the character position of the next
709 end of word from the initial position. If none is found, returns the length of
710 the string.
711  
712 =cut
713  
714 method find_eow_forward
715 {
716 my ( $pos ) = @_;
717  
718 my $posttext = substr( $self->text, $pos );
719  
720 $posttext =~ m/(?<=\S)\s|$/;
721 return $pos + $-[0];
722 }
723  
724 =head2 find_bow_backward
725  
726 $pos = $entry->find_bow_backward( $initial );
727  
728 Search backward in the string, returning the character position of the
729 previous beginning of word from the initial position. If none is found,
730 returns 0.
731  
732 =cut
733  
734 method find_bow_backward
735 {
736 my ( $pos ) = @_;
737  
738 my $pretext = substr( $self->text, 0, $pos );
739  
740 return $pretext =~ m/.*\s(?=\S)/ ? $+[0] : 0;
741 }
742  
743 =head2 find_eow_backward
744  
745 $pos = $entry->find_eow_backward( $initial );
746  
747 Search backward in the string, returning the character position of the
748 previous end of word from the initial position. If none is found, returns
749 C<undef>.
750  
751 =cut
752  
753 method find_eow_backward
754 {
755 my ( $pos ) = @_;
756  
757 my $pretext = substr( $self->text, 0, $pos + 1 ); # +1 to allow if cursor is on the space
758  
759 return $pretext =~ m/.*\S(?=\s)/ ? $+[0] : undef;
760 }
761  
762 ## Key binding methods
763  
764 method key_backward_char
765 {
766 if( $_pos_ch > 0 ) {
767 $self->set_position( $_pos_ch - 1 );
768 }
769 }
770  
771 method key_backward_delete_char
772 {
773 if( $_pos_ch > 0 ) {
774 $self->text_delete( $_pos_ch - 1, 1 );
775 }
776 }
777  
778 method key_backward_delete_line
779 {
780 $self->text_delete( 0, $_pos_ch );
781 }
782  
783 method key_backward_delete_word
784 {
785 my $bow = $self->find_bow_backward( $_pos_ch );
786 $self->text_delete( $bow, $_pos_ch - $bow );
787 }
788  
789 method key_backward_word
790 {
791 if( $_pos_ch > 0 ) {
792 $self->set_position( $self->find_bow_backward( $_pos_ch ) );
793 }
794 }
795  
796 method key_beginning_of_line
797 {
798 $self->set_position( 0 );
799 }
800  
801 method key_delete_line
802 {
803 $self->text_delete( 0, length $self->text );
804 }
805  
806 method key_end_of_line
807 {
808 $self->set_position( length $_text );
809 }
810  
811 method key_enter_line
812 {
813 my $text = $self->text;
814 return unless length $text;
815  
816 $_on_enter->( $self, $text ) if $_on_enter;
817 }
818  
819 method key_forward_char
820 {
821 if( $_pos_ch < length $_text ) {
822 $self->set_position( $_pos_ch + 1 );
823 }
824 }
825  
826 # Renamed from readline's "delete-char" because this one doesn't have the EOF
827 # behaviour if input line is empty
828 method key_forward_delete_char
829 {
830 if( $_pos_ch < length $_text ) {
831 $self->text_delete( $_pos_ch, 1 );
832 }
833 }
834  
835 method key_forward_delete_word
836 {
837 my $bow = $self->find_bow_forward( $_pos_ch, length $self->text );
838 $self->text_delete( $_pos_ch, $bow - $_pos_ch );
839 }
840  
841 method key_forward_word
842 {
843 my $bow = $self->find_bow_forward( $_pos_ch, length $self->text );
844 $self->set_position( $bow );
845 }
846  
847 method key_overwrite_mode
848 {
849 $_overwrite = !$_overwrite;
850 }
851  
852 =head1 TODO
853  
854 =over 4
855  
856 =item * Plugin ability
857  
858 Try to find a nice way to allow loaded plugins, possibly per-instance if not
859 just globally or per-class. See how many of these TODO items can be done using
860 plugins.
861  
862 =item * More readline behaviours
863  
864 History. Isearch. History replay. Transpose. Transcase. Yank ring. Numeric
865 prefixes.
866  
867 =item * Visual selection behaviour
868  
869 Shift-movement, or vim-style. Mouse.
870  
871 =back
872  
873 =head1 AUTHOR
874  
875 Paul Evans <leonerd@leonerd.org.uk>
876  
877 =cut
878  
879 0x55AA;