blib/lib/Curses/UI/Searchable.pm

filter: Statementstmt 22.4% 35 / 156 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 40.9% 9 / 22 Podpod 7.6% 1 / 13 totaltotal 18.8% 45 / 239 CC 22 SCAR 34.6
CC
22
Cov
18%
search
90.0
change_canvasheight
42.0
search_get
42.0
CRAP 31.9
« blib/lib/Curses/UI/Container.pm ↑ Summary blib/lib/Curses/UI/Color.pm »
1 # ----------------------------------------------------------------------
2 # Curses::UI::Searchable
3 # Curses::UI::SearchEntry
4 #
5 # (c) 2001-2002 by Maurice Makaay. All rights reserved.
6 # This file is part of Curses::UI. Curses::UI is free software.
7 # You can redistribute it and/or modify it under the same terms
8 # as perl itself.
9 #
10 # Currently maintained by Marcus Thiesen
11 # e-mail: marcus@cpan.thiesenweb.de
12 # ----------------------------------------------------------------------
13  
14 # TODO: fix dox
15  
16 # ----------------------------------------------------------------------
17 # SearchEntry package
18 # ----------------------------------------------------------------------
19  
20 package Curses::UI::SearchEntry;
21  
22 2 ▶use Curses;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Curses::UI::Widget; # For height_by_windowscrheight()
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Curses::UI::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Curses::UI::Container;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
28 $VERSION
29 @ISA
30 2 );
31  
32 2 $VERSION = "1.10";
33  
34 2 @ISA = qw(
35 Curses::UI::ContainerWidget
36 );
37  
38 sub new()
39 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
40 0 my $class = shift;
41  
42 0 my %userargs = @_;
43 0 keys_to_lowercase(\%userargs);
44  
45 0 my %args = (
46 -prompt => '/', # The initial search prompt
47  
48 %userargs,
49  
50 -x => 0,
51 -y => -1,
52 -width => undef,
53 -border => 0,
54 -sbborder => 0,
55 -showlines => 0,
56 -focus => 0,
57 );
58
59 # The windowscr height should be 1.
60 0 $args{-height} = height_by_windowscrheight(1,%args);
61  
62 0 my $this = $class->SUPER::new(%args);
63  
64 0 my $entry = $this->add(
65 'entry', 'TextEntry',
66 -x => 1,
67 -y => 0,
68 -height => 1,
69 -border => 0,
70 -sbborder => 0,
71 -showlines => 0,
72 -width => undef,
73 -intellidraw => 0,
74 );
75
76 $this->add(
77 'prompt', 'Label',
78 -x => 0,
79 -y => 0,
80 -height => 1,
81 -width => 2,
82 -border => 0,
83 -text => $this->{-prompt},
84 0 -intellidraw => 0,
85 );
86  
87 0 $entry->set_routine('loose-focus', \&entry_loose_focus);
88  
89 0 $this->layout;
90  
91 0 return $this;
92 }
93  
94 sub entry_loose_focus()
95 0 ▶{
Subroutine entry_loose_focus: not called CC=1 SCAR=6.9
Pod undocumented
96 0 my $this = shift;
97 0 $this->parent->loose_focus;
98 }
99  
100 sub event_keypress($;)
101 0 ▶{
Subroutine event_keypress: not called CC=2 SCAR=17.9
Pod undocumented
102 0 my $this = shift;
103 0 my $key = shift;
104  
105 0 my $entry = $this->getobj('entry');
106 0 ▶ if ($entry->{-focus}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{'-focus'}) { } 0 0 0
107 0 $this->getobj('entry')->event_keypress($key);
108 } else {
109 0 $this->{-key} = $key;
110 }
111  
112 0 return $this;
113 }
114  
115 sub get()
116 0 ▶{
Subroutine get: not called CC=1 SCAR=6.9
Pod undocumented
117 0 my $this = shift;
118 0 $this->getobj('entry')->get;
119 }
120  
121 sub pos(;$)
122 0 ▶{
Subroutine pos: not called CC=1 SCAR=6.9
Pod undocumented
123 0 my $this = shift;
124 0 my $pos = shift;
125 0 $this->getobj('entry')->pos($pos);
126 }
127  
128 sub text(;$)
129 0 ▶{
Subroutine text: not called CC=1 SCAR=6.9
Pod undocumented
130 0 my $this = shift;
131 0 my $text = shift;
132 0 $this->getobj('entry')->text($text);
133 }
134  
135 sub prompt(;$)
136 0 ▶{
Subroutine prompt: not called CC=2 SCAR=17.9
Pod undocumented
137 0 my $this = shift;
138 0 my $prompt = shift;
139 0 ▶ if (defined $prompt)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prompt) { } 0 0 0
140 {
141 0 $prompt = substr($prompt, 0, 1);
142 0 $this->{-prompt} = $prompt;
143 0 $this->getobj('prompt')->text($prompt);
144 0 $this->intellidraw;
145 0 return $this;
146 } else {
147 0 return $this->{-prompt};
148 }
149 }
150  
151 # Let Curses::UI->usemodule() believe that this module
152 # was already loaded (usemodule() would else try to
153 # require the non-existing file).
154 #
155 2 $INC{'Curses/UI/SearchEntry.pm'} = $INC{'Curses/UI/Searchable.pm'};
156  
157  
158 # ----------------------------------------------------------------------
159 # Searchable package
160 # ----------------------------------------------------------------------
161  
162 package Curses::UI::Searchable;
163  
164 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
165 2 ▶use Curses;
Subroutine BEGIN: called CC=1 SCAR=0.0
166 2 ▶use Curses::UI::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
167 2 require Exporter;
168  
169 2 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
170 $VERSION
171 @ISA
172 @EXPORT
173 2 );
174  
175 2 $VERSION = '1.10';
176  
177 2 @ISA = qw(
178 Exporter
179 );
180  
181 2 @EXPORT = qw(
182 search_forward
183 search_backward
184 search
185 search_next
186 );
187  
188 sub search_forward()
189 0 ▶{
Subroutine search_forward: not called CC=1 SCAR=6.9
Pod undocumented
190 0 my $this = shift;
191 0 $this->search("/", +1);
192 }
193  
194 sub search_backward()
195 0 ▶{
Subroutine search_backward: not called CC=1 SCAR=6.9
Pod undocumented
196 0 my $this = shift;
197 0 $this->search("?", -1);
198 }
199  
200 sub search()
201 0 ▶{
Subroutine search: not called CC=9 SCAR=45.0
Pod undocumented
202 0 my $this = shift;
203 0 ▶ my $prompt = shift || ':';
Condition0%0/2
shift() || ':'
l!l
00
MC/DC0%0/1
shift() || ':'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
204 0 ▶ my $direction = shift || +1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
205  
206 0 $this->change_canvasheight(-1);
207 0 $this->draw;
208  
209 0 my $querybox = new Curses::UI::SearchEntry(
210 -parent => $this,
211 -prompt => $prompt,
212 );
213  
214 0 my $old_cursor_mode = $this->root->cursor_mode;
215 0 $this->root->cursor_mode(1);
216 0 $querybox->getobj('entry')->{-focus} = 1;
217 0 $querybox->draw;
218 0 $querybox->modalfocus();
219 0 $querybox->getobj('entry')->{-focus} = 0;
220  
221 0 my $query = $querybox->get;
222 0 $querybox->prompt(':');
223 0 $querybox->draw;
224
225 0 my $key;
226 0 ▶ if ($query ne '')
Branch: 1 branch
BranchTrueFalseTotal
if ($query ne "") 0 0 0
227 {
228 0 my ($newidx, $wrapped) =
229 $this->search_next($query, $direction);
230  
231 0 KEY: for (;;)
232 {
233 0 ▶ unless (defined $newidx) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newidx) { } 0 0 0
234 0 $querybox->text('Not found');
235 } else {
236 0 ▶ $querybox->text($wrapped ? 'Wrapped' : '');
Branch: 1 branch
BranchTrueFalseTotal
$wrapped ? : 0 0 0
237 }
238 0 $querybox->pos(0);
239 0 $querybox->draw;
240  
241 0 $querybox->{-key} = '-1';
242 0 ▶ while ($querybox->{-key} eq '-1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($querybox->{"-key"} eq -1) 0 0 0
243 0 $this->root->do_one_event($querybox);
244 }
245  
246 0 ▶ if ($querybox->{-key} eq 'n') {
Branch: 2 branches
BranchTrueFalseTotal
if ($querybox->{'-key'} eq 'n') { } 0 0 0
elsif ($querybox->{'-key'} eq 'N') { } 0 0 0
247 0 ($newidx, $wrapped) =
248 $this->search_next($query, $direction);
249 } elsif ($querybox->{-key} eq 'N') {
250 0 ($newidx, $wrapped) =
251 $this->search_next($query, -$direction);
252 } else {
253 0 last KEY;
254 }
255 }
256 }
257  
258 # Restore the screen.
259 0 $this->root->cursor_mode($old_cursor_mode);
260 0 $this->change_canvasheight(+1);
261 0 $this->draw;
262  
263 0 $this->root->feedkey($querybox->{-key});
264 0 return $this;
265 }
266  
267 sub search_next($$;)
268 0 ▶{
Subroutine search_next: not called CC=2 SCAR=17.9
Pod undocumented
269 0 my $this = shift;
270 0 my $query = shift;
271 0 my $direction = shift;
272 0 ▶ $direction = ($direction > 0 ? +1 : -1);
Branch: 1 branch
BranchTrueFalseTotal
$direction > 0 ? : 0 0 0
273 0 $this->search_get($query, $direction);
274 }
275  
276 sub change_canvasheight($;)
277 0 ▶{
Subroutine change_canvasheight: not called CC=6 SCAR=37.4
Pod undocumented
278 0 my $this = shift;
279 0 my $change = shift;
280  
281 0 ▶ if ($change < 0)
Branch: 2 branches
BranchTrueFalseTotal
if ($change < 0) { } 0 0 0
elsif ($change > 0) { } 0 0 0
282 {
283 # Change the canvasheight, so we can fit in the searchline.
284 0 $this->{-sh}--;
285 $this->{-yscrpos}++
286 0 ▶ if ($this->{-ypos}-$this->{-yscrpos} == $this->canvasheight);
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"-ypos"} - $this->{"-yscrpos"} == $this->canvasheight 0 0 0
287 }
288 elsif ($change > 0)
289 {
290 # Restore the canvasheight.
291 0 $this->{-sh}++;
292 my $inscreen = ($this->canvasheight
293 - ($this->number_of_lines
294 0 - $this->{-yscrpos}));
295 0 ▶ while ($this->{-yscrpos} > 0 and
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"-yscrpos"} > 0 and $inscreen < $this->canvasheight) 0 0 0
Condition0%0/3
$this->{"-yscrpos"} > 0 and $inscreen < $this->canvasheight
!ll&&!rl&&r
000
MC/DC0%0/2
$this->{"-yscrpos"} > 0 and $inscreen < $this->canvasheight
$this->{"-yscrpos"} > 0 $inscreen < $this->canvasheight
Truth table0%0/3
A and B
A= $this->{"-yscrpos"} > 0B= $inscreen < $this->canvasheight
ABresult
0X0
100
111
296 $inscreen < $this->canvasheight)
297 {
298 0 $this->{-yscrpos}--;
299 $inscreen = ($this->canvasheight
300 - ($this->number_of_lines
301 0 - $this->{-yscrpos}));
302 }
303 }
304  
305 0 $this->{-search_highlight} = undef;
306 0 $this->layout_content();
307 }
308  
309 sub search_get($$;)
310 0 ▶{
Subroutine search_get: not called CC=6 SCAR=37.4
Pod undocumented
311 0 my $this = shift;
312 0 my $query = shift;
313 0 ▶ my $direction = shift || +1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
314  
315 0 my $startpos = $this->{-ypos};
316 0 my $offset = 0;
317 0 my $wrapped = 0;
318 0 for (;;)
319 {
320 # Find the line position to match.
321 0 $offset += $direction;
322 0 my $newpos = $this->{-ypos} + $offset;
323  
324 0 my $last_idx = $this->number_of_lines - 1;
325  
326 # Beyond limits?
327 0 ▶ if ($newpos < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($newpos < 0) 0 0 0
328 {
329 0 $newpos = $last_idx;
330 0 $offset = $newpos - $this->{-ypos};
331 0 $wrapped = 1;
332 }
333
334 0 ▶ if ($newpos > $last_idx)
Branch: 1 branch
BranchTrueFalseTotal
if ($newpos > $last_idx) 0 0 0
335 {
336 0 $newpos = 0;
337 0 $offset = $newpos - $this->{-ypos};
338 0 $wrapped = 1;
339 }
340  
341 # Nothing found?
342 0 ▶ return (undef,undef) if $newpos == $startpos;
Branch: 1 branch
BranchTrueFalseTotal
if $newpos == $startpos 0 0 0
343  
344 0 ▶ if ($this->getline_at_ypos($newpos) =~ /\Q$query/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($this->getline_at_ypos($newpos) =~ /\Q$query\E/i) 0 0 0
345 {
346 0 $this->{-ypos} = $newpos;
347 0 $this->{-search_highlight} = $newpos;
348 0 $startpos = $newpos;
349 0 $this->layout_content;
350 0 $this->draw(1);
351 0 return $newpos, $wrapped;
352 0 $wrapped = 0;
353 }
354 }
355 }
356  
357  
358  
359 2 1;
360  
361  
362 =pod
363  
364 =head1 NAME
365  
366 Curses::UI::Searchable - Add 'less'-like search abilities to a widget
367  
368 =head1 CLASS HIERARCHY
369  
370 Curses::UI::Searchable - base class
371  
372  
373 =head1 SYNOPSIS
374  
375 package MyWidget;
376  
377 use Curses::UI::Searchable;
378 use vars qw(@ISA);
379 @ISA = qw(Curses::UI::Searchable);
380  
381 ....
382  
383 sub new () {
384 # Create class instance $this.
385 ....
386  
387 $this->set_routine('search-forward', \&search_forward);
388 $this->set_binding('search-forward', '/');
389 $this->set_routine('search-backward', \&search_backward);
390 $this->set_binding('search-backward', '?');
391 }
392  
393 sub layout_content() {
394 my $this = shift;
395  
396 # Layout your widget's content.
397 ....
398  
399 return $this;
400 }
401  
402 sub number_of_lines() {
403 my $this = shift;
404  
405 # Return the number of lines in
406 # the widget's content.
407 return ....
408 }
409  
410 sub getline_at_ypos($;) {
411 my $this = shift;
412 my $ypos = shift;
413  
414 # Return the content on the line
415 # where ypos = $ypos
416 return ....
417 }
418  
419  
420 =head1 DESCRIPTION
421  
422 Using Curses::UI::Searchable, you can add 'less'-like
423 search capabilities to your widget.
424  
425 To make your widget searchable using this class,
426 your widget should meet the following requirements:
427  
428 =over 4
429  
430 =item * B<make it a descendant of Curses::UI::Searchable>
431  
432 All methods for searching are in Curses::UI::Searchable.
433 By making your class a descendant of this class, these
434 methods are automatically inherited.
435  
436 =item * B<-ypos data member>
437  
438 The current vertical position in the widget should be
439 identified by $this->{-ypos}. This y-position is the
440 index of the line of content. Here's an example for
441 a Listbox widget.
442  
443 -ypos
444 |
445 v
446 +------+
447 0 |One |
448 1 |Two |
449 2 |Three |
450 +------+
451  
452 =item * B<method: number_of_lines ( )>
453  
454 Your widget class should have a method B<number_of_lines>,
455 which returns the total number of lines in the widget's
456 content. So in the example above, this method would
457 return the value 3.
458  
459 =item * B<method: getline_at_ypos ( YPOS )>
460  
461 Your widget class should have a method B<getline_at_ypos>,
462 which returns the line of content at -ypos YPOS.
463 So in the example above, this method would return
464 the value "Two" for YPOS = 1.
465  
466 =item * B<method: layout_content ( )>
467  
468 The search routines will set the -ypos of your widget if a
469 match is found for the given search string. Your B<layout_content>
470 routine should make sure that the line of content at -ypos
471 will be made visible if the B<draw> method is called.
472  
473 =item * B<method: draw ( )>
474  
475 If the search routines find a match, $this->{-search_highlight}
476 will be set to the -ypos for the line on which the match
477 was found. If no match was found $this->{-search_highlight}
478 will be undefined. If you want a matching line to be highlighted,
479 in your widget, you can use this data member to do so
480 (an example of a widget that uses this option is the
481 L<Curses::UI::TextViewer|Curses::UI::TextViewer> widget).
482  
483 =item * B<bindings for searchroutines>
484  
485 There are two search routines. These are B<search_forward> and
486 B<search_backward>. These have to be called in order to
487 display the search prompt. The best way to do this is by
488 creating bindings for them. Here's an example which will
489 make '/' a forward search and '?' a backward search:
490  
491 $this->set_routine('search-forward' , \&search_forward);
492 $this->set_binding('search-forward' , '/');
493 $this->set_routine('search-backward' , \&search_backward);
494 $this->set_binding('search-backward' , '?');
495  
496 =back
497  
498  
499  
500 =head1 SEE ALSO
501  
502 L<Curses::UI|Curses::UI>,
503  
504  
505  
506  
507 =head1 AUTHOR
508  
509 Copyright (c) 2001-2002 Maurice Makaay. All rights reserved.
510  
511 Maintained by Marcus Thiesen (marcus@cpan.thiesenweb.de)
512  
513  
514 This package is free software and is provided "as is" without express
515 or implied warranty. It may be used, redistributed and/or modified
516 under the same terms as perl itself.
517  
« blib/lib/Curses/UI/Container.pm ↑ Summary blib/lib/Curses/UI/Color.pm »