File Coverage

blib/lib/UI/Various/PoorTerm/Box.pm
Criterion Covered Total %
statement 75 75 100.0
branch 30 30 100.0
condition 6 6 100.0
subroutine 10 10 100.0
pod n/a
total 121 121 100.0


line stmt bran cond sub pod time code
1             package UI::Various::PoorTerm::Box;
2              
3             # Author, Copyright and License: see end of file
4              
5             =head1 NAME
6              
7             UI::Various::PoorTerm::Box - 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::Box;
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   19 use v5.14;
  2         5  
31 2     2   8 use strictures;
  2         5  
  2         8  
32 2     2   280 no indirect 'fatal';
  2         4  
  2         7  
33 2     2   93 no multidimensional;
  2         2  
  2         7  
34 2     2   72 use warnings 'once';
  2         3  
  2         115  
35              
36             our $VERSION = '0.24';
37              
38 2     2   11 use UI::Various::core;
  2         2  
  2         8  
39 2     2   10 use UI::Various::Box;
  2         3  
  2         69  
40 2     2   715 use UI::Various::PoorTerm::container;
  2         4  
  2         1920  
41              
42             require Exporter;
43             our @ISA = qw(UI::Various::Box UI::Various::PoorTerm::container);
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 the complete box by printing a separator (blank line unless the border
70             should be visible) and its indented content. If the box is not indented,
71             its active elements are numbered to allow later interaction with them.
72             I
73             action of UI element>> or a I::Various::PoorTerm container element!>
74              
75             =cut
76              
77             # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
78              
79             sub _show($$)
80             {
81 39     39   9414 my ($self, $prefix) = @_;
82 39 100       101 defined $prefix or $prefix = '';
83 39         82 my $blank = ' ' x length($prefix);
84 39         56 local $_;
85              
86             # The number of '_show' parents' calls is the indention level:
87 39         47 my $level = 0;
88 39         91 $level++ while (caller($level + 1))[3] =~ m/::_show$/;
89 39 100       1130 my $indent = $level <= 1 ? '' : ' ';
90 39 100       106 my $border = $self->border ? '----------' : '';
91              
92 39 100       674 $prefix ne '' and print $indent, $prefix, $border, "\n";
93              
94             # 1st gather active children and get width of prefix:
95 39         133 $self->{_active} = [];
96 39         152 while ($_ = $self->child)
97             {
98 86 100       252 $_->can('_process') and push @{$self->{_active}}, $_;
  61         149  
99             }
100 39         49 my $active = @{$self->{_active}};
  39         63  
101 39         66 $_ = length($active);
102              
103             # print children:
104 39         81 my $pre_active = '<%' . $_ . 'd> ';
105 39         76 my $pre_passive = ' ' x ($_ + 3);
106 39         47 my $i = 1;
107 39         87 foreach my $row (0..($self->rows - 1))
108             {
109 58         146 foreach my $column (0..($self->columns - 1))
110             {
111 102         275 $_ = $self->field($row, $column);
112 102 100       187 next unless defined $_;
113 86         113 my $pre_child = $indent;
114 86 100       274 if ($prefix =~ m/[^ ]/)
    100          
115             {
116 32 100 100     117 if ($active == 1 and $_->can('_process'))
117 9         18 { $pre_child .= '<*> '; }
118             else
119 23         70 { $pre_child .= $blank; }
120             }
121             elsif ($prefix eq '')
122             {
123 42 100       183 $pre_child .= $_->can('_process')
124             ? sprintf($pre_active, $i++) : $pre_passive;
125             }
126             else
127 12         17 { $pre_child .= $blank; }
128 86         234 $_->_show($pre_child);
129             }
130             }
131              
132             # finish as child or master of selection:
133 39 100       155 if ($prefix ne '')
134             {
135 22         325 print $indent, $blank, $border, "\n";
136             }
137             else
138             {
139             # print standard selection strings:
140 17         89 print $self->_wrap(sprintf($pre_active, 0), msg('leave_box')), "\n\n";
141 17         85 print $self->_wrap('----- ',
142             msg('enter_number_to_choose_next_step')), ': ';
143             }
144             }
145              
146             #########################################################################
147              
148             =head2 B<_process> - handle action of UI element
149              
150             $ui_element->_process;
151              
152             =head3 description:
153              
154             Handle the action of the UI element. For a C's box this means: If
155             the box has no active element, just return. If it has exactly one active
156             element, the active element is processed directly. Otherwise the method
157             iterates through a loop of printing the box's elements and allowing to
158             select one of the active ones for processing until the box is exited.
159              
160             =cut
161              
162             # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
163              
164             sub _process($)
165             {
166 5     5   4354 debug(3, __PACKAGE__, '::_process');
167 5         13 my ($self) = @_;
168              
169 5         9 my $active = @{$self->{_active}};
  5         12  
170 5 100       23 $active == 1 and $self->{_active}->[0]->_process;
171 5 100       70 $active <= 1 and return 0;
172              
173 3         6 while (1)
174             {
175 11         97 $self->_show;
176 11         49 local $_ = ;
177 11         91 print $_;
178 11         86 s/\r?\n$//;
179 11 100       46 return $_ if m/^0$/;
180 8 100 100     37 unless ($_ =~ m/^\d+$/ and $_ <= @{$self->{_active}})
  7         31  
181 2         11 { error('invalid_selection'); next; }
  2         5  
182 6         26 $self->{_active}->[$_-1]->_process;
183             }
184             }
185              
186             1;
187              
188             #########################################################################
189             #########################################################################
190              
191             =head1 SEE ALSO
192              
193             L, L
194              
195             =head1 LICENSE
196              
197             Copyright (C) Thomas Dorner.
198              
199             This library is free software; you can redistribute it and/or modify it
200             under the same terms as Perl itself. See LICENSE file for more details.
201              
202             =head1 AUTHOR
203              
204             Thomas Dorner Edorner (at) cpan (dot) orgE
205              
206             =cut