File Coverage

blib/lib/UI/Various/RichTerm/Radio.pm
Criterion Covered Total %
statement 23 63 36.5
branch 0 14 0.0
condition n/a
subroutine 8 11 72.7
pod n/a
total 31 88 35.2


line stmt bran cond sub pod time code
1             package UI::Various::RichTerm::Radio;
2              
3             # Author, Copyright and License: see end of file
4              
5             =head1 NAME
6              
7             UI::Various::RichTerm::Radio - 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::Radio;
14              
15             =head1 ABSTRACT
16              
17             This module is the specific implementation of L using
18             the rich terminal UI.
19              
20             =head1 DESCRIPTION
21              
22             The documentation of this module is only intended for developers of the
23             package itself.
24              
25             =cut
26              
27             #########################################################################
28              
29 6     6   76 use v5.14;
  6         19  
30 6     6   26 use strictures;
  6         13  
  6         28  
31 6     6   925 no indirect 'fatal';
  6         14  
  6         27  
32 6     6   331 no multidimensional;
  6         9  
  6         25  
33 6     6   246 use warnings 'once';
  6         13  
  6         325  
34              
35             our $VERSION = '0.22';
36              
37 6     6   32 use UI::Various::core;
  6         11  
  6         33  
38 6     6   31 use UI::Various::Radio;
  6         14  
  6         239  
39 6     6   34 use UI::Various::RichTerm::base qw(%D);
  6         16  
  6         5844  
40              
41             require Exporter;
42             our @ISA = qw(UI::Various::Radio UI::Various::RichTerm::base);
43             our @EXPORT_OK = qw();
44              
45             #########################################################################
46             #########################################################################
47              
48             =head1 METHODS
49              
50             =cut
51              
52             #########################################################################
53              
54             =head2 B<_prepare> - prepare UI element
55              
56             ($width, $height) = $ui_element->_prepare($content_width);
57              
58             =head3 example:
59              
60             my ($w, $h) = $_->_prepare($content_width);
61             $width < $w and $width = $w;
62             $height += $h;
63              
64             =head3 parameters:
65              
66             $content_width preferred width of content
67              
68             =head3 description:
69              
70             Prepare output of the UI element by determining and returning the space it
71             wants or needs. I
72             C container elements!>
73              
74             =head3 returns:
75              
76             width and height the UI element will require or need when printed
77              
78             =cut
79              
80             # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
81              
82             sub _prepare($$)
83             {
84 0     0     my ($self, $content_width) = @_;
85 0           my ($w, $h) = (0, 0);
86 0           local $_;
87              
88 0           foreach (0..$#{$self->{_button_values}})
  0            
89             {
90             my ($_w, $_h) =
91 0           $self->_size($self->{_button_values}[$_], $content_width);
92 0 0         $w > $_w or $w = $_w;
93 0           $h += $_h;
94             }
95 0           return ($w, $h);
96             }
97              
98             #########################################################################
99              
100             =head2 B<_show> - return formatted UI element
101              
102             $string = $ui_element->_show($prefix, $width, $height);
103              
104             =head3 example:
105              
106             my ($w, $h) = $_->_prepare($content_width);
107             ...
108             $_->_show('(1) ', $w, $h);
109              
110             =head3 parameters:
111              
112             $prefix text in front of first line
113             $width the width returned by _prepare above
114             $height the height returned by _prepare above
115              
116             =head3 description:
117              
118             Return the formatted (rectangular) text box of the UI element. Its height
119             will be exactly as specified, unless there hasn't been enough space. The
120             weight is similarly as specified plus the width needed for the prefix.
121             I
122             elements!>
123              
124             =head3 returns:
125              
126             the rectangular text box for UI element
127              
128             =cut
129              
130             # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
131              
132             sub _show($$$$)
133             {
134 0     0     my ($self, $prefix, $width, $height) = @_;
135 0           my $blank = ' ' x length($prefix);
136             # Note that the accessor automatically dereferences the SCALAR here:
137 0           my $var = $self->var;
138 0 0         defined $var or $var = '$ ^ }!\\"{]}[%]'; # magic invalid string
139 0           my @text = ();
140 0           foreach my $i (0..$#{$self->{_button_keys}})
  0            
141             {
142 0 0         local $_ = ($i == 0 ? $prefix : $blank) . $D{RL};
143 0 0         $_ .= ($var eq $self->{_button_keys}[$i] ? 'o' : ' ') . $D{RR} . ' ';
144 0           push @text, $self->_format($_, '', '', $self->{_button_values}[$i],
145             '', '', $width, 0);
146             }
147 0           return $self->_format('', '', '', \@text, '', '', $width, $height);
148             }
149              
150             #########################################################################
151              
152             =head2 B<_process> - handle action of UI element
153              
154             $ui_element->_process;
155              
156             =head3 description:
157              
158             Handle the action of the UI element (aka select one of the radio buttons).
159              
160             =cut
161              
162             # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
163              
164             sub _process($)
165             {
166 0     0     my ($self) = @_;
167              
168 0           my $max = @{$self->{_button_keys}};
  0            
169 0           my $prefix = '<%' . length($max) . 'd> ';
170 0           my $blank = ' ' x (length($max) + 3);
171 0           my $prompt = '';
172 0           my $selected = '';
173             # Note that the accessor automatically dereferences the SCALAR here:
174 0           my $var = $self->var;
175 0 0         defined $var or $var = '$ ^ }!\\"{]}[%]'; # magic invalid string
176 0           $Text::Wrap::columns = $self->width;
177 0           foreach my $i (0..$#{$self->{_button_keys}})
  0            
178             {
179             $prompt .= Text::Wrap::wrap(sprintf($prefix, $i + 1), $blank,
180 0           $self->{_button_values}[$i]);
181 0           $prompt .= "\n";
182 0 0         $var eq $self->{_button_keys}[$i] and $selected = $i + 1;
183             }
184 0           $prompt .= Text::Wrap::wrap('', '', msg('enter_selection') . ' (' .
185             sprintf(msg('_1_to_cancel'), 0) . '): ');
186 0           my $re_valid = '^(?:' . join('|', 0..$max) . ')$';
187 0           local $_ = $self->top->readline($prompt, $re_valid, $selected);
188 0 0         0 < $_ and ${$self->{var}} = $self->{_button_keys}[$_ - 1];
  0            
189             }
190              
191             1;
192              
193             #########################################################################
194             #########################################################################
195              
196             =head1 SEE ALSO
197              
198             L, L
199              
200             =head1 LICENSE
201              
202             Copyright (C) Thomas Dorner.
203              
204             This library is free software; you can redistribute it and/or modify it
205             under the same terms as Perl itself. See LICENSE file for more details.
206              
207             =head1 AUTHOR
208              
209             Thomas Dorner Edorner (at) cpan (dot) orgE
210              
211             =cut