File: Menu.pm

package info (click to toggle)
ddtc 0.17.2
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 252 kB
  • sloc: perl: 2,260; makefile: 90
file content (327 lines) | stat: -rw-r--r-- 5,606 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package Ddtc::Menu;


use strict;
use Exporter;
use Term::ReadLine;
use Term::ANSIColor;
use Ddtc::Common;

use vars  qw(@ISA);

@ISA	= qw(Exporter);


# new
#
# create new menu with title and redraw
#
# input:
#   title
#   title color
#   default letter color
#   default text color
# output:
#   Menu object
sub new($$;$$$) {
	shift;						# class

	debug 3;
	debug 4, "title: "	 .($_[0] || "")."\n"
		."letter_color: ".($_[0] || "")."\n"
		."text_color:   ".($_[0] || "")."\n"
		."title_color:  ".($_[0] || "")."\n";

	my $self = bless [];

#	$self->[0] = [undef, undef];
	$self -> title	     (	 shift) if @_;
	$self -> letter_color(0, shift);
	$self -> text_color  (0, shift);
	$self -> title_color (	 shift);
	$self -> add("__", sub { 1 });

	return $self;
}


# title
#
# get / set menu title
#
# input:
#   Menu object
#   menu title		[ set ]
# output
#   menu title	
sub title($;$) {
	my $self = shift;

	debug 3;
	debug 4, "title: ".($_[0] || "")."\n";

	$self->[0][2] = shift	if @_;			# set if value provided

	return undef unless defined $self->[0][2];
	debug 4, "title: ".$self->[0][2]."\n";
	$self->[0][2];
}


# letter
#
# get / set letter
#
# input:
#   Menu object
#   item number
#   string			[ set ]
# output
#   letter	
sub letter($$;$){
	my $self = shift;
	my $item = shift;

	debug 3;
	debug 4, "item:   ".$item."\n"
		."string: ".($_[0] || "")."\n";

	if (@_) {					# set if value provided
		$_ = shift;
		/_(.)/;
		$self->[$item][0] = $1 || undef;
	}

	return undef unless exists $self->[$item][0];
	debug 4, "letter: ".$self->[$item][0]."\n";
	$self->[$item][0];
}


# text
#
# get / set text
#
# input:
#   Menu object
#   item number
#   string			[ set ]
# output
#   text
sub text($$;$){
	my $self = shift;
	my $item = shift;

	debug 3;
	debug 4, "item:   ".$item."\n"
		."string: ".($_[0] || "")."\n";

	if (@_) {					# set if value provided
		$_ = shift;
		s/_._//;
		s/_//;
		$self->[$item][1] = $_ || undef;
	}

	return undef unless exists $self->[$item][1];
	debug 4, "text: ".($self->[$item][1] || "")."\n";
	$self->[$item][1];
}


# exec
#
# get / set command
#
# input:
#   Menu object
#   item number
#   code reference			[ set ]
# output
#   code reference
sub exec($$;$){
	my $self = shift;
	my $item = shift;

	debug 3;
	debug 4, "item: ".$item."\n"
		."code: ".($_[0] || "")."\n";

	$self->[$item][2] = shift	if @_;			# set if value provided

	return undef unless exists $self->[$item][2];
	debug 4, "code: ".$self->[$item][2]."\n";
	$self->[$item][2];
}


# letter_color
#
# get / set letter title
#
# input:
#   Menu object
#   item number
#   menu title_color		[ set ]
# output
#   menu title_color	
sub letter_color($$;$){
	my $self = shift;
	my $item = shift || 0;

	debug 3;
	debug 4, "item:  ".$item."\n"
		."color: ".($_[0] || "")."\n";

	$self->[$item][3] = shift	if @_;		# set if value provided

	return 'clear' unless defined $self->[$item][3] || defined $self->[0][3];
	debug 4, "color: ".($self->[$item][3] || $self->[0][3])."\n";
	$self->[$item][3] || $self->[0][3];
}


# text_color
#
# get / set text title
#
# input:
#   Menu object
#   item number
#   menu text_color		[ set ]
# output
#   menu text_color	
sub text_color($$;$){
	my $self = shift;
	my $item = shift || 0;

	debug 3;
	debug 4, "item:  ".$item."\n"
		."color: ".($_[0] || "")."\n";

	$self->[$item][4] = shift	if @_;		# set if value provided

	return 'clear' unless defined $self->[$item][4] || defined $self->[0][4];
	debug 4, "color: ".($self->[$item][4] || $self->[0][4])."\n";
	$self->[$item][4] || $self->[0][4];
}


# title_color
#
# get / set menu title
#
# input:
#   Menu object
#   menu title_color		[ set ]
# output
#   menu title_color	
sub title_color($;$){
	my $self = shift;

	debug 3;
	debug 4, "color: ".($_[0] || "")."\n";

	$self->[0][5] = shift	if @_;			# set if value provided

	return 'clear' unless defined $self->[0][5];
	debug 4, "color: ".$self->[0][5]."\n";
	$self->[0][5];
}


# print
#
# print menu
#
# input:
#   Menu object
sub print($) {
	my $self = shift;

	debug 3;

	debug 0, "\n  "
		.color($self -> title_color)
		.     ($self -> title      )
		.color('clear'             )."\n";
	for (my $i = 0; $i < @{ $self }; $i++) {
		next unless defined $self -> text($i);
		debug 0, color($self -> letter_color($i))
			.     ($self -> letter      ($i))
			.color('clear'			)
			.color($self -> text_color  ($i))." "
			.     ($self -> text        ($i))
			.color('clear'			)."\n";
	}
}


# add
#
# add item to menu
#
# input:
#   Menu object
#   string
#   sub reference
sub add($$$;$$) {
	my $self   = shift;
	my $string = shift;
	my $sub    = shift;

	my $i = @{ $self };

	debug 3;
	debug 4, "item:  ".$i."\n"
		."string: ".$string."\n"
		."code:   ".$sub."\n";

	$self -> letter	     ($i, $string);
	$self -> text	     ($i, $string);
	$self -> exec	     ($i, $sub);
	$self -> letter_color($i, shift) if @_;
	$self -> text_color  ($i, shift) if @_;

	return $self;
}


# execute
#
# execute menu
#
# input:
#   reference of menu hash
#   terminal object
sub execute($$) {
	my $self = shift;
	my $term = shift;

	debug 3;

	$self -> print;

	my $letters = "";
	for (my $i = 0; $i < @{ $self }; $i++) {
		next unless defined $self -> letter($i);
		$letters .= $self -> letter($i);
	}
	my $prompt  =  $letters;
	   $prompt  =~ s/\W//g;

	while(1) {
		my $l;
		until ($l && ($l =~ /^[$letters]?$/)) {
			$l   = $term -> readline("$prompt\: ");
			defined $l ? chomp $l : print "\n";
		}
		for (my $i = 0; $i < @{ $self }; $i++) {
			next unless $self -> letter($i);
			next unless $l eq $self -> letter($i);
			return &{ $self -> exec($i) };
		}
	}
}

1;