File Coverage

blib/lib/UI/Various/PoorTerm/Listbox.pm
Criterion Covered Total %
statement 96 96 100.0
branch 46 46 100.0
condition 18 18 100.0
subroutine 10 10 100.0
pod n/a
total 170 170 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   16 use v5.14;
  2         4  
31 2     2   8 use strictures;
  2         2  
  2         8  
32 2     2   232 no indirect 'fatal';
  2         3  
  2         6  
33 2     2   84 no multidimensional;
  2         8  
  2         13  
34 2     2   48 use warnings 'once';
  2         4  
  2         99  
35              
36             our $VERSION = '0.23';
37              
38 2     2   9 use UI::Various::core;
  2         3  
  2         13  
39 2     2   10 use UI::Various::Listbox;
  2         2  
  2         48  
40 2     2   278 use UI::Various::PoorTerm::base;
  2         4  
  2         2229  
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   5032 my ($self, $prefix) = @_;
79 8         23 my $blank = ' ' x length($prefix);
80 8         20 my ($i, $h) = ($self->first, $self->height);
81 8         27 my $entries = @{$self->texts};
  8         20  
82 8 100       16 if ($entries)
83             {
84 6         7 my $last = $i + $h;
85 6 100       12 $last <= $entries or $last = $entries;
86 6         146 print $prefix, ' ', $i + 1, '-', $last, '/', $entries, "\n";
87             }
88             else
89 2         49 { print $blank, " 0/0\n"; }
90 8         22 local $_ = 0;
91 8         26 while ($_ < $h)
92             {
93 37 100 100     97 if (0 <= $i && $i < $entries)
94             {
95             print $self->_cut($blank,
96             $self->{_selected}[$i], ' ',
97 24         83 $self->{texts}[$i]), "\n";
98 24         53 $i++;
99             }
100             else
101 13         88 { print "\n"; }
102 37         110 $_++;
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   8350 my ($self) = @_;
124 9         29 my ($h, $selection) = ($self->height, $self->selection);
125 9         34 my $entries = @{$self->texts};
  9         21  
126 9 100       22 my $head = $entries > $h ? '<+/-> ' : ' ';
127 9         26 $head .= ' ' x (($selection > 0) * int($h / 10));
128 9         21 my $pre_active = '<%' . int(1 + $h / 10) . 'd> ';
129 9 100       44 my $re_selection = ($selection == 0 ? qr/(0)/ :
    100          
130             $selection == 1 ? qr/(\d+)/ :
131             qr/(\d+)(?:,\s*\d+)*/);
132 9         19 my $prompt = msg('enter_selection');
133 9 100       22 $entries > $h and $prompt .= ' (' . msg('scrolls') . ')';
134 9         15 $prompt .= ': ';
135 9         12 local $_ = '';
136 9         19 while ($_ ne '0')
137             {
138 29         51 my $i = $self->{first};
139 29         31 my $last = $i + $h;
140 29 100       47 $last <= $entries or $last = $entries;
141 29 100       425 print($head,
142             $entries ? ($i + 1 . '-' . $last . '/' . $entries) : '0/0',
143             "\n");
144 29         61 $_ = 0;
145 29         58 while ($_ < $h)
146             {
147 182         178 $_++;
148 182 100 100     465 if (0 <= $i && $i < $entries)
149             {
150 172 100       214 if ($selection)
151             {
152             print $self->_cut(sprintf($pre_active, $_),
153             $self->{_selected}[$i], ' ',
154 135         595 $self->{texts}[$i]), "\n";
155             }
156             else
157 37         95 { print $self->_cut($self->{texts}[$i]), "\n"; }
158 172         552 $i++;
159             }
160             else
161 10         75 { print "\n"; }
162             }
163 29         102 print sprintf($pre_active, 0), ' ', msg('leave_listbox'), "\n";
164 29         60 $_ = '';
165 29         52 while ($_ eq '')
166             {
167 35         229 print $prompt;
168 35         97 $_ = ;
169 35         213 print $_;
170 35         175 s/\s+$//;
171 35 100 100     376 unless (($entries > $h and m/^[-+]$/) or
      100        
      100        
172             (m/^$re_selection$/ and $1 <= $h))
173 6         19 { error('invalid_selection'); $_ = ''; next; }
  6         8  
  6         14  
174             }
175 29 100       63 if ($_ eq '+')
    100          
176             {
177 7         11 $self->{first} += $h;
178 7 100       21 $self->{first} + $h <= $entries or $self->{first} = $entries - $h;
179             }
180             elsif ($_ eq '-')
181             {
182 4         7 $self->{first} -= $h;
183 4 100       12 $self->{first} >= 0 or $self->{first} = 0;
184             }
185             else
186             {
187 18         20 my $changes = 0;
188 18 100       27 if ($selection == 1)
189             {
190 9 100       16 if ($_ > 0)
191             {
192 5         7 foreach my $i (0..$#{$self->texts})
  5         12  
193             {
194             $self->{_selected}[$i] =
195             $i != $self->{first} + $_ - 1 ? ' ' :
196 40 100       73 $self->{_selected}[$i] eq ' ' ? '*' : ' ';
    100          
197 40         40 $changes++;
198             }
199             }
200             }
201             else
202             {
203 9         26 foreach (split m/,\s*/, $_)
204             {
205 12 100       27 $_ > 0 or next;
206 7         21 $i = $self->{first} + $_ - 1;
207             $self->{_selected}[$i] =
208 7 100       17 $self->{_selected}[$i] eq ' ' ? '*' : ' ';
209 7         8 $changes++;
210             }
211             }
212             defined $self->{on_select} and $changes > 0 and
213 18 100 100     95 &{$self->{on_select}};
  4         12  
214             }
215             }
216             }
217              
218             1;
219              
220             #########################################################################
221             #########################################################################
222              
223             =head1 SEE ALSO
224              
225             L, L
226              
227             =head1 LICENSE
228              
229             Copyright (C) Thomas Dorner.
230              
231             This library is free software; you can redistribute it and/or modify it
232             under the same terms as Perl itself. See LICENSE file for more details.
233              
234             =head1 AUTHOR
235              
236             Thomas Dorner Edorner (at) cpan (dot) orgE
237              
238             =cut