line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package UI::Various::RichTerm::Optionmenu; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Author, Copyright and License: see end of file |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
UI::Various::RichTerm::Optionmenu - 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::Optionmenu; |
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
|
|
58
|
use v5.14; |
|
6
|
|
|
|
|
17
|
|
30
|
6
|
|
|
6
|
|
26
|
use strictures; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
25
|
|
31
|
6
|
|
|
6
|
|
806
|
no indirect 'fatal'; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
35
|
|
32
|
6
|
|
|
6
|
|
301
|
no multidimensional; |
|
6
|
|
|
|
|
20
|
|
|
6
|
|
|
|
|
23
|
|
33
|
6
|
|
|
6
|
|
240
|
use warnings 'once'; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
301
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
our $VERSION = '0.24'; |
36
|
|
|
|
|
|
|
|
37
|
6
|
|
|
6
|
|
48
|
use UI::Various::core; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
28
|
|
38
|
6
|
|
|
6
|
|
35
|
use UI::Various::Optionmenu; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
187
|
|
39
|
6
|
|
|
6
|
|
45
|
use UI::Various::RichTerm::base qw(%D); |
|
6
|
|
|
|
|
19
|
|
|
6
|
|
|
|
|
4593
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
require Exporter; |
42
|
|
|
|
|
|
|
our @ISA = qw(UI::Various::Optionmenu 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
|
0
|
|
|
|
|
local $_ = defined $self->{_selected_menu} ? $self->{_selected_menu} : '---'; |
86
|
|
|
|
|
|
|
# Note that Optionmenu has 2 chars "special" decoration used in each line: |
87
|
0
|
|
|
|
|
|
my ($w, $h) = $self->_size($_, $content_width - 2); |
88
|
0
|
|
|
|
|
|
return ($w + 2, $h); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
######################################################################### |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 B<_show> - return formatted UI element |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
$string = $ui_element->_show($prefix, $width, $height); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head3 example: |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
my ($w, $h) = $_->_prepare($content_width); |
100
|
|
|
|
|
|
|
... |
101
|
|
|
|
|
|
|
$_->_show('(1) ', $w, $h); |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head3 parameters: |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
$prefix text in front of first line |
106
|
|
|
|
|
|
|
$width the width returned by _prepare above |
107
|
|
|
|
|
|
|
$height the height returned by _prepare above |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head3 description: |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Return the formatted (rectangular) text box of the UI element. Its height |
112
|
|
|
|
|
|
|
will be exactly as specified, unless there hasn't been enough space. The |
113
|
|
|
|
|
|
|
weight is similarly as specified plus the width needed for the prefix. |
114
|
|
|
|
|
|
|
I
|
115
|
|
|
|
|
|
|
elements!> |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head3 returns: |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
the rectangular text box for UI element |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=cut |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub _show($$$$) |
126
|
|
|
|
|
|
|
{ |
127
|
0
|
|
|
0
|
|
|
my ($self, $prefix, $width, $height) = @_; |
128
|
0
|
0
|
|
|
|
|
local $_ = defined $self->{_selected_menu} ? $self->{_selected_menu} : '---'; |
129
|
|
|
|
|
|
|
return $self->_format($prefix, $D{BL}, '', $_, '', $D{BR}, |
130
|
0
|
|
|
|
|
|
$width - 2, $height); # - 2 chars decoration |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
######################################################################### |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head2 B<_process> - handle action of UI element |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
$ui_element->_process; |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head3 description: |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Handle the action of the UI element aka I |
142
|
|
|
|
|
|
|
options>. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=cut |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub _process($) |
149
|
|
|
|
|
|
|
{ |
150
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
151
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
|
my $max = @{$self->{options}}; |
|
0
|
|
|
|
|
|
|
153
|
0
|
|
|
|
|
|
my $prefix = '<%' . length($max) . 'd> '; |
154
|
0
|
|
|
|
|
|
my $blank = ' ' x (length($max) + 3); |
155
|
0
|
|
|
|
|
|
my $prompt = ''; |
156
|
|
|
|
|
|
|
|
157
|
0
|
|
|
|
|
|
local $_ = $self->{_selected}; |
158
|
0
|
0
|
|
|
|
|
my $selected = defined $_ ? $_ : ''; |
159
|
0
|
|
|
|
|
|
$Text::Wrap::columns = $self->width; |
160
|
0
|
|
|
|
|
|
foreach (0..$#{$self->{options}}) |
|
0
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
{ |
162
|
|
|
|
|
|
|
$prompt .= Text::Wrap::wrap(sprintf($prefix, $_ + 1), $blank, |
163
|
0
|
|
|
|
|
|
$self->{options}[$_][0]); |
164
|
0
|
|
|
|
|
|
$prompt .= "\n"; |
165
|
|
|
|
|
|
|
} |
166
|
0
|
|
|
|
|
|
$prompt .= Text::Wrap::wrap('', '', msg('enter_selection') . ' (' . |
167
|
|
|
|
|
|
|
sprintf(msg('_1_to_cancel'), 0) . '): '); |
168
|
0
|
|
|
|
|
|
my $re_valid = '^(?:' . join('|', 0..$max) . ')$'; |
169
|
0
|
|
|
|
|
|
$_ = $self->top->readline($prompt, $re_valid, $selected); |
170
|
0
|
0
|
|
|
|
|
if (0 < $_) |
171
|
|
|
|
|
|
|
{ |
172
|
0
|
|
|
|
|
|
$self->{_selected_menu} = $self->{options}[$_ - 1][0]; |
173
|
0
|
|
|
|
|
|
$self->{_selected} = $self->{options}[$_ - 1][1]; |
174
|
0
|
|
|
|
|
|
$_ = $self->{on_select}; |
175
|
0
|
0
|
|
|
|
|
defined $_ and &$_($self->{_selected}); |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
1; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
######################################################################### |
182
|
|
|
|
|
|
|
######################################################################### |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 SEE ALSO |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
L, L |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head1 LICENSE |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
Copyright (C) Thomas Dorner. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify it |
193
|
|
|
|
|
|
|
under the same terms as Perl itself. See LICENSE file for more details. |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head1 AUTHOR |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Thomas Dorner Edorner (at) cpan (dot) orgE |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=cut |