File Coverage

blib/lib/UI/Various/PoorTerm/Listbox.pm
Criterion Covered Total %
statement 90 90 100.0
branch 44 44 100.0
condition 15 15 100.0
subroutine 10 10 100.0
pod n/a
total 159 159 100.0


line stmt bran cond sub pod time code
1             package UI::Various::PoorTerm::Listbox;
2              
3             # Author, Copyright and License: see end of file
4              
5             =head1 NAME
6              
7             UI::Various::PoorTerm::Listbox - concrete implementation of L
8              
9             =head1 SYNOPSIS
10              
11             # This module should never be used directly!
12             # It is used indirectly via the following:
13             use UI::Various::Listbox;
14              
15             =head1 ABSTRACT
16              
17             This module is the specific minimal fallback implementation of
18             L. It manages and hides everything specific to the last
19             resort UI.
20              
21             =head1 DESCRIPTION
22              
23             The documentation of this module is only intended for developers of the
24             package itself.
25              
26             =cut
27              
28             #########################################################################
29              
30 2     2   25 use v5.14;
  2         5  
31 2     2   10 use strictures;
  2         6  
  2         10  
32 2     2   309 no indirect 'fatal';
  2         4  
  2         10  
33 2     2   101 no multidimensional;
  2         5  
  2         34  
34 2     2   65 use warnings 'once';
  2         5  
  2         120  
35              
36             our $VERSION = '0.22';
37              
38 2     2   15 use UI::Various::core;
  2         4  
  2         19  
39 2     2   13 use UI::Various::Listbox;
  2         5  
  2         72  
40 2     2   397 use UI::Various::PoorTerm::base;
  2         7  
  2         2888  
41              
42             require Exporter;
43             our @ISA = qw(UI::Various::Listbox UI::Various::PoorTerm::base);
44             our @EXPORT_OK = qw();
45              
46             #########################################################################
47             #########################################################################
48              
49             =head1 METHODS
50              
51             =cut
52              
53             #########################################################################
54              
55             =head2 B<_show> - print UI element
56              
57             $ui_element->_show($prefix);
58              
59             =head3 example:
60              
61             $_->_show('(1) ');
62              
63             =head3 parameters:
64              
65             $prefix text in front of first line
66              
67             =head3 description:
68              
69             Show (print) the UI element. I
70             UI::Various::PoorTerm container elements!>
71              
72             =cut
73              
74             # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
75              
76             sub _show($$)
77             {
78 8     8   6058 my ($self, $prefix) = @_;
79 8         28 my $blank = ' ' x length($prefix);
80 8         24 my ($i, $h) = ($self->first, $self->height);
81 8         25 my $entries = @{$self->texts};
  8         26  
82 8 100       18 if ($entries)
83             {
84 6         11 my $last = $i + $h;
85 6 100       13 $last <= $entries or $last = $entries;
86 6         175 print $prefix, ' ', $i + 1, '-', $last, '/', $entries, "\n";
87             }
88             else
89 2         78 { print $blank, " 0/0\n"; }
90 8         26 local $_ = 0;
91 8         33 while ($_ < $h)
92             {
93 37 100 100     112 if (0 <= $i && $i < $entries)
94             {
95             print $self->_cut($blank,
96             $self->{_selected}[$i], ' ',
97 24         98 $self->{texts}[$i]), "\n";
98 24         69 $i++;
99             }
100             else
101 13         110 { print "\n"; }
102 37         148 $_++;
103             }
104             }
105              
106             #########################################################################
107              
108             =head2 B<_process> - handle action of UI element
109              
110             $ui_element->_process;
111              
112             =head3 description:
113              
114             Handle the action of the UI element (aka scrolling and selection of
115             elements, if applicable).
116              
117             =cut
118              
119             # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
120              
121             sub _process($)
122             {
123 9     9   9602 my ($self) = @_;
124 9         25 my ($h, $selection) = ($self->height, $self->selection);
125 9         35 my $entries = @{$self->texts};
  9         22  
126 9 100       25 my $head = $entries > $h ? '<+/-> ' : ' ';
127 9         65 $head .= ' ' x (($selection > 0) * int($h / 10));
128 9         26 my $pre_active = '<%' . int(1 + $h / 10) . 'd> ';
129 9 100       55 my $re_selection = ($selection == 0 ? qr/(0)/ :
    100          
130             $selection == 1 ? qr/(\d+)/ :
131             qr/(\d+)(?:,\s*\d+)*/);
132 9         23 my $prompt = msg('enter_selection');
133 9 100       25 $entries > $h and $prompt .= ' (' . msg('scrolls') . ')';
134 9         16 $prompt .= ': ';
135 9         14 local $_ = '';
136 9         18 while ($_ ne '0')
137             {
138 29         42 my $i = $self->{first};
139 29         48 my $last = $i + $h;
140 29 100       48 $last <= $entries or $last = $entries;
141 29 100       498 print($head,
142             $entries ? ($i + 1 . '-' . $last . '/' . $entries) : '0/0',
143             "\n");
144 29         79 $_ = 0;
145 29         62 while ($_ < $h)
146             {
147 182         250 $_++;
148 182 100 100     514 if (0 <= $i && $i < $entries)
149             {
150 172 100       257 if ($selection)
151             {
152             print $self->_cut(sprintf($pre_active, $_),
153             $self->{_selected}[$i], ' ',
154 135         664 $self->{texts}[$i]), "\n";
155             }
156             else
157 37         106 { print $self->_cut($self->{texts}[$i]), "\n"; }
158 172         676 $i++;
159             }
160             else
161 10         91 { print "\n"; }
162             }
163 29         121 print sprintf($pre_active, 0), ' ', msg('leave_listbox'), "\n";
164 29         72 $_ = '';
165 29         58 while ($_ eq '')
166             {
167 35         280 print $prompt;
168 35         125 $_ = ;
169 35         268 print $_;
170 35         203 s/\s+$//;
171 35 100 100     475 unless (($entries > $h and m/^[-+]$/) or
      100        
      100        
172             (m/^$re_selection$/ and $1 <= $h))
173 6         21 { error('invalid_selection'); $_ = ''; next; }
  6         12  
  6         17  
174             }
175 29 100       77 if ($_ eq '+')
    100          
    100          
176             {
177 7         16 $self->{first} += $h;
178 7 100       30 $self->{first} + $h <= $entries or $self->{first} = $entries - $h;
179             }
180             elsif ($_ eq '-')
181             {
182 4         9 $self->{first} -= $h;
183 4 100       15 $self->{first} >= 0 or $self->{first} = 0;
184             }
185             elsif ($selection == 1)
186             {
187 9 100       34 if ($_ > 0)
188             {
189 5         8 foreach my $i (0..$#{$self->texts})
  5         24  
190             {
191             $self->{_selected}[$i] =
192             $i != $self->{first} + $_ - 1 ? ' ' :
193 40 100       86 $self->{_selected}[$i] eq ' ' ? '*' : ' ';
    100          
194             }
195             }
196             }
197             else
198             {
199 9         30 foreach (split m/,\s*/, $_)
200             {
201 12 100       48 $_ > 0 or last;
202 7         15 $i = $self->{first} + $_ - 1;
203             $self->{_selected}[$i] =
204 7 100       30 $self->{_selected}[$i] eq ' ' ? '*' : ' ';
205             }
206             }
207             }
208             }
209              
210             1;
211              
212             #########################################################################
213             #########################################################################
214              
215             =head1 SEE ALSO
216              
217             L, L
218              
219             =head1 LICENSE
220              
221             Copyright (C) Thomas Dorner.
222              
223             This library is free software; you can redistribute it and/or modify it
224             under the same terms as Perl itself. See LICENSE file for more details.
225              
226             =head1 AUTHOR
227              
228             Thomas Dorner Edorner (at) cpan (dot) orgE
229              
230             =cut