blib/lib/Term/Fabulous/Widget/TextInput.pm untested

filter: Statementstmt 0.0% 0 / 214 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 75 MC/DCmcdc 0.0% 0 / 44 Subroutinesub 0.0% 0 / 13 Podpod n/a totaltotal 0.0% 0 / 430 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Term::Fabulous::Widget::TextInput;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 use Object::Pad 0.825;
9  
10 use Term::Fabulous::Editor;
11 use Term::Fabulous::TextView;
12 use Term::Fabulous::Widget::Input;
13  
14 our $VERSION = '0.05';
15  
16 class Term::Fabulous::Widget::TextInput
17 :isa(Term::Fabulous::Widget::Input)
18 :abstract
19 {
20 use Feature::Compat::Try;
21 use Scalar::Util qw(weaken);
22 use Term::Fabulous::Check qw(boolean string);
23 use Term::Fabulous::Termbox qw(TB_KEY_MOUSE_LEFT TB_KEY_MOUSE_RELEASE TB_MOD_MOTION TB_MOD_SHIFT);
24 use Term::Fabulous::Unicode qw(sanitize_text grapheme_clusters);
25  
26 # Cursor movement: key name => [ editor method, extend the selection ].
27 my %MOVEMENT_BY_KEY = (
28 'Left' => [ move_left => 0 ],
29 'Shift+Left' => [ move_left => 1 ],
30 'Right' => [ move_right => 0 ],
31 'Shift+Right' => [ move_right => 1 ],
32 'Ctrl+Left' => [ move_word_left => 0 ],
33 'Ctrl+Shift+Left' => [ move_word_left => 1 ],
34 'Ctrl+Right' => [ move_word_right => 0 ],
35 'Ctrl+Shift+Right' => [ move_word_right => 1 ],
36 'Home' => [ move_line_start => 0 ],
37 'Shift+Home' => [ move_line_start => 1 ],
38 'End' => [ move_line_end => 0 ],
39 'Shift+End' => [ move_line_end => 1 ],
40 'Ctrl+Home' => [ move_document_start => 0 ],
41 'Ctrl+Shift+Home' => [ move_document_start => 1 ],
42 'Ctrl+End' => [ move_document_end => 0 ],
43 'Ctrl+Shift+End' => [ move_document_end => 1 ],
44 );
45  
46 # Edits: key name => editor method.
47 my %EDIT_BY_KEY = (
48 'Backspace' => 'delete_backward',
49 'Delete' => 'delete_forward',
50 'Ctrl+W' => 'delete_word_backward',
51 'Ctrl+Delete' => 'delete_word_forward',
52 'Ctrl+U' => 'delete_to_line_start',
53 'Ctrl+K' => 'delete_to_line_end',
54 'Ctrl+X' => 'cut',
55 'Shift+Delete' => 'cut',
56 'Ctrl+V' => 'paste',
57 'Shift+Insert' => 'paste',
58 'Ctrl+Z' => 'undo',
59 'Ctrl+Y' => 'redo',
60 );
61  
62 # A text that must not show (a password) has no words to move or delete
63 # by, which would tell where its spaces are: these keys act on the
64 # whole text instead. It cannot be copied or cut either.
65 my %WHOLE_TEXT_KEY_FOR = (
66 'Ctrl+Left' => 'Home',
67 'Ctrl+Shift+Left' => 'Shift+Home',
68 'Ctrl+Right' => 'End',
69 'Ctrl+Shift+Right' => 'Shift+End',
70 'Ctrl+W' => 'Ctrl+U',
71 'Ctrl+Delete' => 'Ctrl+K',
72 );
73 my %COPIES = map { $_ => 1 } 'Ctrl+X', 'Shift+Delete', 'Ctrl+Insert';
74  
75 field $editor :reader = Term::Fabulous::Editor->new( multi_line => __CLASS__->is_multi_line );
76  
77 field $placeholder :param = '';
78 field $read_only :param = 0;
79  
80 # The explicit accept spec; undef takes the validator's suggestion.
81 field $accept :param = undef;
82  
83 # How the text is laid out in the buffer: one row without wrapping
84 # unless a subclass says otherwise.
85 field $view :reader;
86  
87 # Mouse: whether a press in the widget started a drag, and the text
88 # position of the last single press, which a double click must repeat.
89 field $dragging = 0;
90 field @last_press;
91  
92 ADJUST :params ( :$value = undef, :$max_length = undef ) {
93 weaken( my $weak_self = $self );
94 $view = Term::Fabulous::TextView->new( editor => $editor, display => sub ($cluster) { $weak_self->display_cluster($cluster) } );
95  
96 $read_only = boolean( $self, read_only => $read_only );
97 $placeholder = string( $self, placeholder => $placeholder );
98 $self->max_length($max_length) if defined $max_length;
99 $self->_apply_accept;
100 $self->value($value) if defined $value;
101 }
102  
103 # Runs an editor call for a public method; the editor's errors are
104 # reworded to name this widget, which is the class the caller used.
105 method _in_editor ($code) {
106 try {
107 return $code->();
108 }
109 catch ($error) {
110 $error =~ s/\ATerm::Fabulous::Editor:/ref($self) . ':'/e;
111 die $error;
112 }
113 }
114  
115 method is_multi_line :common () {
116 return 0;
117 }
118  
119 # True when the text is not shown as it is (a masked password field).
120 method hides_text () {
121 return 0;
122 }
123  
124 # ---------------------------------------------------------------------
125 # Properties
126 # ---------------------------------------------------------------------
127  
128 method value (@new) {
129 return $editor->text unless @new;
130 $self->_in_editor( sub { $editor->set_text( $new[0] ) } );
131 $self->mark_changed;
132 return $editor->text;
133 }
134  
135 method max_length (@new) {
136 return $editor->max_length unless @new;
137 $self->_in_editor( sub { $editor->set_max_length( $new[0] ) } );
138 return $editor->max_length;
139 }
140  
141 method accept (@new) {
142 return $accept unless @new;
143 my $previous = $accept;
144 $accept = $new[0];
145 try {
146 $self->_apply_accept;
147 }
148 catch ($error) {
149 $accept = $previous;
150 die $error;
151 }
152 return $accept;
153 }
154  
155 # The editor takes the explicit accept spec, or the validator's
156 # suggestion.
157 method _apply_accept () {
158 my $validator = $self->validator;
159 my $spec = $accept // ( defined $validator ? $validator->accept : undef );
160 $self->_in_editor( sub { $editor->set_accept($spec) } );
161 return;
162 }
163  
164 method validator_changed :override () {
165 $self->_apply_accept;
166 return;
167 }
168  
169 method placeholder (@new) {
170 return $placeholder unless @new;
171 $placeholder = string( $self, placeholder => $new[0] );
172 $self->mark_changed;
173 return $placeholder;
174 }
175  
176 method read_only (@new) {
177 return $read_only unless @new;
178 $read_only = boolean( $self, read_only => $new[0] );
179 return $read_only;
180 }
181  
182 method theme_family :common () {
183 return 'text_input';
184 }
185  
186 method themed_params :common () {
187 return ( $class->SUPER::themed_params, placeholder_color => [ 'placeholder', 'normal', 'cell_color' ], selection_color => [ 'selection', 'normal', 'cell_color' ] );
188 }
189  
190 method placeholder_color (@new) {
191 return @new ? $self->set_look( placeholder_color => $new[0] ) : $self->look_value('placeholder_color');
192 }
193  
194 method selection_color (@new) {
195 return @new ? $self->set_look( selection_color => $new[0] ) : $self->look_value('selection_color');
196 }
197  
198 method layout_properties :common () {
199 return (
200 $class->SUPER::layout_properties,
201 value => 'scalar',
202 placeholder => 'scalar',
203 max_length => 'scalar',
204 accept => 'scalar',
205 read_only => 'boolean',
206 );
207 }
208  
209 # The max_length, accept and validator of a layout come first, so they
210 # limit the value wherever it stands.
211 my %LIMITS_VALUE = map { $_ => 1 } qw(max_length accept validator);
212  
213 method apply_layout_settings :override (@settings) {
214 return $self->SUPER::apply_layout_settings( ( grep { $LIMITS_VALUE{ $_->[0] } } @settings ), ( grep { !$LIMITS_VALUE{ $_->[0] } } @settings ) );
215 }
216  
217 # ---------------------------------------------------------------------
218 # Keys
219 # ---------------------------------------------------------------------
220  
221 method handle_key ($event) {
222 my $text = $event->text;
223 return $self->apply_edit( $editor->type($text) ) if defined $text && !$read_only;
224  
225 my $name = $event->main_key_name // return 0;
226 if ( $self->hides_text ) {
227 return 0 if $COPIES{$name};
228 $name = $WHOLE_TEXT_KEY_FOR{$name} // $name;
229 }
230 if ( my $movement = $MOVEMENT_BY_KEY{$name} ) {
231 my ( $method, $extend ) = @$movement;
232 $editor->$method($extend);
233 return $self->_view_changed;
234 }
235 if ( my $edit = $EDIT_BY_KEY{$name} ) {
236 return 0 if $read_only;
237 return $self->apply_edit( $editor->$edit );
238 }
239 if ( $name eq 'Ctrl+A' ) {
240 $editor->select_all;
241 return $self->_view_changed;
242 }
243 if ( $name eq 'Ctrl+Insert' ) {
244 $editor->copy;
245 return 1;
246 }
247 return 0;
248 }
249  
250 # The editor changed the cursor, the selection or the text: the next
251 # frame shows it. Returns 1, for handlers that used their event.
252 method _view_changed () {
253 $self->mark_changed;
254 return 1;
255 }
256  
257 # After an editor edit: fires Change when the text changed. Returns 1,
258 # the key was used either way.
259 method apply_edit ($changed) {
260 $self->_view_changed;
261 $self->fire_change( $editor->text ) if $changed;
262 return 1;
263 }
264  
265 # ---------------------------------------------------------------------
266 # Mouse: a press places the cursor, a Shift+click extends the
267 # selection to it, a double click selects a word (the whole text when
268 # it is hidden), dragging selects.
269 # ---------------------------------------------------------------------
270  
271 method handle_mouse ($event) {
272 my $key = $event->key;
273 if ( $key == TB_KEY_MOUSE_RELEASE ) {
274 $dragging = 0;
275 return 1;
276 }
277 return 0 unless $key == TB_KEY_MOUSE_LEFT;
278  
279 my ( $column, $row ) = $self->cell_at($event);
280 return 1 unless defined $column;
281 my @position = $view->position_at( $column, $row );
282  
283 if ( $event->modifiers & TB_MOD_MOTION ) {
284 $editor->move_to( @position, 1 ) if $dragging;
285 return $self->_view_changed;
286 }
287  
288 if ( $event->modifiers & TB_MOD_SHIFT ) {
289 $editor->move_to( @position, 1 );
290 @last_press = ();
291 }
292 elsif ( $event->click_count >= 2 && "@last_press" eq "@position" ) {
293 $self->hides_text ? $editor->select_all : $editor->select_word_at(@position);
294 @last_press = ();
295 }
296 else {
297 $editor->move_to( @position, 0 );
298 @last_press = @position;
299 }
300 $dragging = 1;
301 return $self->_view_changed;
302 }
303  
304 # ---------------------------------------------------------------------
305 # Painting
306 # ---------------------------------------------------------------------
307  
308 # Edits made through the editor, also by the program, show when the
309 # frame is drawn: the view follows the cursor whenever the editor or
310 # the size changed.
311 method refresh :override () {
312 $view->set_size( $self->columns, $self->rows )->follow_cursor;
313 return $self->SUPER::refresh;
314 }
315  
316 method paint_key :override () {
317 return ( $self->SUPER::paint_key, $editor->revision, $view->top_row, $view->left_column );
318 }
319  
320 method focus_changed :override ($is_focused) {
321 $dragging = 0;
322 return;
323 }
324  
325 # How a cluster of the text is shown; a password field masks it.
326 method display_cluster ($cluster) {
327 return sanitize_text($cluster);
328 }
329  
330 method paint () {
331 $self->paint_focus_background;
332 return $self->_paint_placeholder if $editor->is_empty && length $placeholder;
333  
334 my $width = $view->text_columns;
335 my $y = 0;
336 foreach my $row ( $view->visible_rows ) {
337 my ( $line, $from, $to, $is_last, $shown ) = @$row;
338 my $end_x = $self->_paint_part( $y, $line, $shown, $to, $is_last, $from, $width );
339 $self->_paint_selected_line_break( $y, $line, $end_x, $width ) if $is_last;
340 $y++;
341 }
342 return;
343 }
344  
345 sub _is_selected ( $selection, $row, $offset ) {
346 my ( $row_0, $offset_0, $row_1, $offset_1 ) = @$selection;
347 return 0 unless defined $row_0;
348 return 0 if $row < $row_0 || ( $row == $row_0 && $offset < $offset_0 );
349 return 0 if $row > $row_1 || ( $row == $row_1 && $offset >= $offset_1 );
350 return 1;
351 }
352  
353 # Paints the part [from, to) of a line on buffer row $y, scrolled as
354 # the view is, with the selection and (when $cursor_at_end allows a
355 # cursor at $to) the cursor. Text in [hidden_from, from) belongs to the
356 # row unseen; a cursor on it is shown at $from. Returns the column after
357 # the text.
358 method _paint_part ( $y, $row, $from, $to, $cursor_at_end, $hidden_from, $width ) {
359 my @selection = $editor->selection;
360 my ( $cursor_row, $cursor_offset ) = $editor->cursor;
361 my $has_cursor = $self->is_focused && $cursor_row == $row;
362 $cursor_offset = $from if $cursor_offset >= $hidden_from && $cursor_offset < $from;
363 my $fg = $self->foreground_attr;
364 my $bg = $self->focus_background_attr;
365 my $selected = $self->color_attr( $self->look_value('selection_color') );
366  
367 my $x = -$view->left_column;
368 foreach my $cluster ( $view->clusters( $row, $from, $to ) ) {
369 my ( $offset, $display, $columns ) = @$cluster;
370 my $cell_bg = _is_selected( \@selection, $row, $offset ) ? $selected : $bg;
371 my $cell_fg = $has_cursor && $offset == $cursor_offset ? $self->reverse_attr($fg) : $fg;
372 $self->put_attrs( $x, $y, $display, $cell_fg, $cell_bg ) if $x >= 0 && $x + $columns <= $width;
373 $x += $columns;
374 }
375 $self->put_attrs( $x, $y, ' ', $self->reverse_attr($fg), $bg )
376 if $has_cursor && $cursor_at_end && $cursor_offset == $to && $x >= 0 && $x < $width;
377 return $x;
378 }
379  
380 # A selection reaching over the end of a line shows a selected cell
381 # after it.
382 method _paint_selected_line_break ( $y, $line, $x, $width ) {
383 return if $line == $editor->line_count - 1 || $x < 0 || $x >= $width;
384 my ( $row_0, $offset_0, $row_1 ) = $editor->selection;
385 return unless defined $row_0 && $line < $row_1;
386 return if $line < $row_0 || ( $line == $row_0 && $offset_0 > length $editor->line($line) );
387  
388 my ( $cursor_row, $cursor_offset ) = $editor->cursor;
389 return if $self->is_focused && $cursor_row == $line && $cursor_offset == length $editor->line($line);
390 $self->put_attrs( $x, $y, ' ', undef, $self->color_attr( $self->look_value('selection_color') ) );
391 return;
392 }
393  
394 method _paint_placeholder () {
395 my $bg = $self->focus_background_attr;
396 $self->paint_text( 0, 0, $placeholder, $self->color_attr( $self->look_value('placeholder_color') ), $bg );
397 return unless $self->is_focused;
398  
399 my ($first) = grapheme_clusters($placeholder);
400 $self->put_attrs( 0, 0, $first // ' ', $self->reverse_attr( $self->foreground_attr ), $bg );
401 return;
402 }
403 }
404  
405 1;
406  
407 __END__