| 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 |  | 18 | use v5.14; | 
|  | 2 |  |  |  |  | 6 |  | 
| 31 | 2 |  |  | 2 |  | 8 | use strictures; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 8 |  | 
| 32 | 2 |  |  | 2 |  | 269 | no indirect 'fatal'; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 7 |  | 
| 33 | 2 |  |  | 2 |  | 101 | no multidimensional; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 8 |  | 
| 34 | 2 |  |  | 2 |  | 70 | use warnings 'once'; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 103 |  | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | our $VERSION = '0.24'; | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 2 |  |  | 2 |  | 17 | use UI::Various::core; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 9 |  | 
| 39 | 2 |  |  | 2 |  | 11 | use UI::Various::Listbox; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 58 |  | 
| 40 | 2 |  |  | 2 |  | 339 | use UI::Various::PoorTerm::base; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 2546 |  | 
| 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 |  | 5860 | my ($self, $prefix) = @_; | 
| 79 | 8 |  |  |  |  | 27 | my $blank = ' ' x length($prefix); | 
| 80 | 8 |  |  |  |  | 22 | my ($i, $h) = ($self->first, $self->height); | 
| 81 | 8 |  |  |  |  | 26 | my $entries = @{$self->texts}; | 
|  | 8 |  |  |  |  | 25 |  | 
| 82 | 8 | 100 |  |  |  | 16 | if ($entries) | 
| 83 |  |  |  |  |  |  | { | 
| 84 | 6 |  |  |  |  | 9 | my $last = $i + $h; | 
| 85 | 6 | 100 |  |  |  | 14 | $last <= $entries  or  $last = $entries; | 
| 86 | 6 |  |  |  |  | 170 | print $prefix, '  ', $i + 1, '-', $last, '/', $entries, "\n"; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  | else | 
| 89 | 2 |  |  |  |  | 73 | {   print $blank, "  0/0\n";   } | 
| 90 | 8 |  |  |  |  | 24 | local $_ = 0; | 
| 91 | 8 |  |  |  |  | 27 | while ($_ < $h) | 
| 92 |  |  |  |  |  |  | { | 
| 93 | 37 | 100 | 100 |  |  | 100 | if (0 <= $i  &&  $i < $entries) | 
| 94 |  |  |  |  |  |  | { | 
| 95 |  |  |  |  |  |  | print $self->_cut($blank, | 
| 96 |  |  |  |  |  |  | $self->{_selected}[$i], ' ', | 
| 97 | 24 |  |  |  |  | 98 | $self->{texts}[$i]), "\n"; | 
| 98 | 24 |  |  |  |  | 62 | $i++; | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  | else | 
| 101 | 13 |  |  |  |  | 104 | {   print "\n";   } | 
| 102 | 37 |  |  |  |  | 130 | $_++; | 
| 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 |  | 9689 | my ($self) = @_; | 
| 124 | 9 |  |  |  |  | 24 | my ($h, $selection) = ($self->height, $self->selection); | 
| 125 | 9 |  |  |  |  | 42 | my $entries = @{$self->texts}; | 
|  | 9 |  |  |  |  | 22 |  | 
| 126 | 9 | 100 |  |  |  | 25 | my $head = $entries > $h ? '<+/-> ' : '      '; | 
| 127 | 9 |  |  |  |  | 31 | $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 |  |  |  |  | 37 | my $prompt = msg('enter_selection'); | 
| 133 | 9 | 100 |  |  |  | 24 | $entries > $h  and  $prompt .= ' (' . msg('scrolls') . ')'; | 
| 134 | 9 |  |  |  |  | 15 | $prompt .= ': '; | 
| 135 | 9 |  |  |  |  | 17 | local $_ = ''; | 
| 136 | 9 |  |  |  |  | 19 | while ($_ ne '0') | 
| 137 |  |  |  |  |  |  | { | 
| 138 | 29 |  |  |  |  | 51 | my $i = $self->{first}; | 
| 139 | 29 |  |  |  |  | 36 | my $last = $i + $h; | 
| 140 | 29 | 100 |  |  |  | 50 | $last <= $entries  or  $last = $entries; | 
| 141 | 29 | 100 |  |  |  | 473 | print($head, | 
| 142 |  |  |  |  |  |  | $entries ? ($i + 1 . '-' . $last . '/' . $entries) : '0/0', | 
| 143 |  |  |  |  |  |  | "\n"); | 
| 144 | 29 |  |  |  |  | 70 | $_ = 0; | 
| 145 | 29 |  |  |  |  | 60 | while ($_ < $h) | 
| 146 |  |  |  |  |  |  | { | 
| 147 | 182 |  |  |  |  | 251 | $_++; | 
| 148 | 182 | 100 | 100 |  |  | 526 | if (0 <= $i  &&  $i < $entries) | 
| 149 |  |  |  |  |  |  | { | 
| 150 | 172 | 100 |  |  |  | 253 | if ($selection) | 
| 151 |  |  |  |  |  |  | { | 
| 152 |  |  |  |  |  |  | print $self->_cut(sprintf($pre_active, $_), | 
| 153 |  |  |  |  |  |  | $self->{_selected}[$i], ' ', | 
| 154 | 135 |  |  |  |  | 650 | $self->{texts}[$i]), "\n"; | 
| 155 |  |  |  |  |  |  | } | 
| 156 |  |  |  |  |  |  | else | 
| 157 | 37 |  |  |  |  | 102 | {   print $self->_cut($self->{texts}[$i]), "\n";   } | 
| 158 | 172 |  |  |  |  | 607 | $i++; | 
| 159 |  |  |  |  |  |  | } | 
| 160 |  |  |  |  |  |  | else | 
| 161 | 10 |  |  |  |  | 88 | {   print "\n";   } | 
| 162 |  |  |  |  |  |  | } | 
| 163 | 29 |  |  |  |  | 115 | print sprintf($pre_active, 0), '  ', msg('leave_listbox'), "\n"; | 
| 164 | 29 |  |  |  |  | 72 | $_ = ''; | 
| 165 | 29 |  |  |  |  | 62 | while ($_ eq '') | 
| 166 |  |  |  |  |  |  | { | 
| 167 | 35 |  |  |  |  | 270 | print $prompt; | 
| 168 | 35 |  |  |  |  | 116 | $_ = ; | 
| 169 | 35 |  |  |  |  | 247 | print $_; | 
| 170 | 35 |  |  |  |  | 211 | s/\s+$//; | 
| 171 | 35 | 100 | 100 |  |  | 448 | unless (($entries > $h  and  m/^[-+]$/)  or | 
|  |  |  | 100 |  |  |  |  | 
|  |  |  | 100 |  |  |  |  | 
| 172 |  |  |  |  |  |  | (m/^$re_selection$/  and  $1 <= $h)) | 
| 173 | 6 |  |  |  |  | 21 | {   error('invalid_selection');   $_ = '';   next;   } | 
|  | 6 |  |  |  |  | 8 |  | 
|  | 6 |  |  |  |  | 16 |  | 
| 174 |  |  |  |  |  |  | } | 
| 175 | 29 | 100 |  |  |  | 76 | if ($_ eq '+') | 
|  |  | 100 |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | { | 
| 177 | 7 |  |  |  |  | 12 | $self->{first} += $h; | 
| 178 | 7 | 100 |  |  |  | 24 | $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 |  |  |  |  |  |  | else | 
| 186 |  |  |  |  |  |  | { | 
| 187 | 18 |  |  |  |  | 22 | my $changes = 0; | 
| 188 | 18 | 100 |  |  |  | 36 | if ($selection == 1) | 
| 189 |  |  |  |  |  |  | { | 
| 190 | 9 | 100 |  |  |  | 16 | if ($_ > 0) | 
| 191 |  |  |  |  |  |  | { | 
| 192 | 5 |  |  |  |  | 9 | foreach my $i (0..$#{$self->texts}) | 
|  | 5 |  |  |  |  | 14 |  | 
| 193 |  |  |  |  |  |  | { | 
| 194 |  |  |  |  |  |  | $self->{_selected}[$i] = | 
| 195 |  |  |  |  |  |  | $i != $self->{first} + $_ - 1 ? ' ' : | 
| 196 | 40 | 100 |  |  |  | 76 | $self->{_selected}[$i] eq ' ' ? '*' : ' '; | 
|  |  | 100 |  |  |  |  |  | 
| 197 | 40 |  |  |  |  | 55 | $changes++; | 
| 198 |  |  |  |  |  |  | } | 
| 199 |  |  |  |  |  |  | } | 
| 200 |  |  |  |  |  |  | } | 
| 201 |  |  |  |  |  |  | else | 
| 202 |  |  |  |  |  |  | { | 
| 203 | 9 |  |  |  |  | 26 | foreach (split m/,\s*/, $_) | 
| 204 |  |  |  |  |  |  | { | 
| 205 | 12 | 100 |  |  |  | 35 | $_ > 0  or  next; | 
| 206 | 7 |  |  |  |  | 13 | $i = $self->{first} + $_ - 1; | 
| 207 |  |  |  |  |  |  | $self->{_selected}[$i] = | 
| 208 | 7 | 100 |  |  |  | 20 | $self->{_selected}[$i] eq ' ' ? '*' : ' '; | 
| 209 | 7 |  |  |  |  | 10 | $changes++; | 
| 210 |  |  |  |  |  |  | } | 
| 211 |  |  |  |  |  |  | } | 
| 212 |  |  |  |  |  |  | defined $self->{on_select}  and  $changes > 0  and | 
| 213 | 18 | 100 | 100 |  |  | 88 | &{$self->{on_select}}; | 
|  | 4 |  |  |  |  | 15 |  | 
| 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 |