Quiq/Terminal.pm

filter: Statementstmt 72.5% 87 / 120 Branchbran 48.0% 25 / 52 Conditioncond 50.0% 6 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 92.8% 13 / 14 Podpod 100% 4 / 4 totaltotal 65.7% 138 / 210 CC 32 SCAR 45.1
CC
32
Cov
64%
askUser
131.7
__ANON__
2.0
ansiEsc
2.0
CRAP 91.1
« Quiq/Tree.pm ↑ Summary Quiq/Sql.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Terminal - Ein- und Ausgabe aufs Terminal
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =cut
14  
15 # -----------------------------------------------------------------------------
16  
17 package Quiq::Terminal;
18 270 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 270 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 270 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 270 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 270 our $VERSION = '1.240';
25  
26 270 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 270 ▶use Quiq::FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 270 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 270 ▶use Term::ANSIColor ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 270 ▶use Quiq::Exit;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # -----------------------------------------------------------------------------
33  
34 =head1 METHODS
35  
36 =head2 Klassenmethoden
37  
38 =head3 askUser() - Erfrage vom Benutzer einen Wert
39  
40 =head4 Synopsis
41  
42 $val = $class->askUser($text,@opt);
43  
44 =head4 Options
45  
46 =over 4
47  
48 =item -automatic => $bool (Default: 0)
49  
50 Stelle keine Frage an den Benutzer, sondern liefere den Defaultwert.
51 Ist kein Defaultwert angegeben, wirf eine Exception. Diese Option ist
52 für Programme nützlich, die auch ohne Benutzerinteraktion ablaufen
53 können.
54  
55 =item -default => $default (Default: keiner)
56  
57 Liefere $default, wenn der Benutzer keinen Wert eingibt. An den
58 Prompt wird die Zeichenkette " ($default) " angehängt.
59  
60 =item -inHandle => $fh (Default: *STDIN)
61  
62 Filehandle, von der die Benutzereingabe gelesen wird.
63  
64 =item -outHandle => $fh (Default: *STDOUT)
65  
66 Filehandle, auf die der Prompt geschrieben wird.
67  
68 =item -sloppy => $bool (Default: 0)
69  
70 Beschränke die möglichen Antworten nicht auf die Liste $valSpec.
71  
72 =item -timer => \$t (Default: undef)
73  
74 Addiere Antwortzeit des Benutzer zu Zeitvariable $t hinzu. Dieses
75 Feature kann genutzt werden, um aus einer Zeitmessung des rufenden
76 Code die (langsame) Antwortzeit des Benutzers herauszunehmen.
77  
78 my $t0 = Time::HiRes::gettimeofday;
79 ...
80 Quiq::Terminal->askUser($prompt,
81 -timer=>\$t0,
82 ...
83 );
84 ...
85 printf "Elapsed: %.2f\n",Time::HiRes::gettimeofday-$t0;
86  
87 Achtung: Der Wert der Zeitvariable wird in die Zukunft verschoben
88 und sollte daher nur zur Zeitdauermessung verwendet werden.
89  
90 =item -timeout => $n
91  
92 Liefere den Defaultwert nach $n Sekunden. Ist kein Defaultwert
93 angegeben, wirf eine Exception. Diese Option ist für Programme
94 nützlich, die einen automatischen Default-Ablauf haben, in den
95 der Benutzer aber eingreifen kann, wenn er das Programm bedient.
96  
97 =item -ttyIn => $bool (Default: 0)
98  
99 Lies Eingabe vom Terminal. Der Terminal-Eingabekanal (/dev/tty)
100 wird mit jedem Aufruf geöffnet und geschlossen.
101  
102 =item -ttyOut => $bool (Default: 0)
103  
104 Schreibe Ausgabe auf Terminal. Der Terminal-Ausgabekanal (/dev/tty)
105 wird mit jedem Aufruf geöffnet und geschlossen.
106  
107 =item -values => $valSpec (Default: keiner)
108  
109 Liste der zulässigen Antworten. Ist die Antwort nicht in der Liste
110 enthalten, wird die Frage erneut gestellt.
111  
112 =back
113  
114 =head4 Description
115  
116 Fordere den Benutzer mit Prompt $text zur Eingabe eines
117 Werts auf. Der vom Benutzer eingegebene Wert wird zurückgeliefert.
118 Whitespace am Anfang und am Ende des Werts werden entfernt.
119  
120 =head4 Example
121  
122 Eingabe vom Terminal statt von STDIN per Filehandle:
123  
124 my $tty = Quiq::FileHandle->new('<','/dev/tty');
125 my $val = Quiq::Terminal->askUser($prompt,-inHandle=>$tty);
126 $tty->close;
127  
128 Dasselbe per Option:
129  
130 my $val = Quiq::Terminal->askUser($prompt,-ttyIn=>1);
131  
132 =cut
133  
134 # -----------------------------------------------------------------------------
135  
136 7 ▶sub askUser {
Subroutine askUser: called CC=31 SCAR=48.8
Pod documented
137 7 my $class = shift;
138 7 my $prompt = shift;
139 # @_: @opt
140  
141 # Optionen
142  
143 7 my $automatic = 0;
144 7 my $color = '';
145 7 my $default = undef;
146 7 my $in = *STDIN;
147 7 my $out = *STDOUT;
148 7 my $sloppy = 0;
149 7 my $timer = undef;
150 7 my $timeout = undef;
151 7 my $ttyIn = 0;
152 7 my $ttyOut = 0;
153 7 my $values = undef;
154  
155 7 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 3 7
156 4 Quiq::Option->extract(\@_,
157 -automatic => \$automatic,
158 -color => \$color,
159 -default => \$default,
160 -inHandle => \$in,
161 -outHandle => \$out,
162 -sloppy => \$sloppy,
163 -timer => \$timer,
164 -timeout => \$timeout,
165 -ttyIn => \$ttyIn,
166 -ttyOut => \$ttyOut,
167 -values => \$values,
168 );
169 }
170  
171 7 ▶ if ($automatic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($automatic) 2 5 7
172 2 ▶ if (defined $default) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) { } 2 0 2
173 2 return $default;
174 }
175 else {
176 0 $class->throw(
177 'ASK-00001: Option -automatic without option -default',
178 );
179 }
180 }
181  
182 5 ▶ if ($timeout && !defined $default) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout and not defined $default) 0 5 5
Condition33%1/3
$timeout and not defined $default
!ll&&!rl&&r
500
MC/DC0%0/2
$timeout and not defined $default
$timeout not defined $default
Truth table33%1/3
A and not(B)
A= $timeoutB= not defined $default
ABresult
0X0
100
111
183 0 $class->throw(
184 'ASK-00002: Option -timeout without option -default',
185 );
186 }
187  
188 5 my $reset = '';
189 5 ▶ if ($color) {
Branch: 1 branch
BranchTrueFalseTotal
if ($color) 0 5 5
190 0 $color = $class->ansiEsc($color);
191 0 $reset = $class->ansiEsc('reset');
192 }
193  
194 # Prompt generieren: "$prompt [$val1,$val2,...] ($def)"
195  
196 5 $prompt = sprintf '%s%s%s',$color,$prompt,$reset;
197  
198 5 my (@values,$valuesText);
199 5 ▶ if ($values) {
Branch: 1 branch
BranchTrueFalseTotal
if ($values) 0 5 5
200 0 for my $val (split m|[/,]|,$values) {
201 0 my $text = $val;
202 0 ▶ if ($val =~ /\((.+)\)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($val =~ /\((.+)\)/) { } 0 0 0
elsif ($val =~ /^(.*?)=(.*)/) { } 0 0 0
203 # (y)es,(a)bort
204 0 $val = $1;
205 0 $text = sprintf '%s(%s%s%s)%s',$`,$color,$val,$reset,$';
206 }
207 elsif ($val =~ /^(.*?)=(.*)/) {
208 # y=yes,a=abort
209 0 $val = $1;
210 0 $text = sprintf '%s%s%s=%s',$color,$val,$reset,$2;
211 }
212 0 push @values,$val;
213  
214 0 ▶ $valuesText .= ',' if $valuesText;
Branch: 1 branch
BranchTrueFalseTotal
if $valuesText 0 0 0
215 0 $valuesText .= $text;
216 }
217 }
218  
219 5 ▶ $prompt .= " [$valuesText]" if defined $valuesText;
Branch: 1 branch
BranchTrueFalseTotal
if defined $valuesText 0 5 5
220 5 ▶ $prompt .= " ($default)" if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 2 3 5
221 5 ▶ $prompt .= " " if $valuesText || defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if $valuesText or defined $default 2 3 5
Condition66%2/3
$valuesText or defined $default
l!l&&r!l&&!r
023
MC/DC50%1/2
$valuesText or defined $default
$valuesText defined $default
Truth table66%2/3
A or B
A= $valuesTextB= defined $default
ABresult
1X1
011
000
222  
223 # Eingabe lesen und prüfen (falls -values)
224  
225 5 my $t0 = Time::HiRes::gettimeofday;
226  
227 5 my $answ;
228 5 while (1) {
229 5 ▶ if ($ttyOut) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ttyOut) 0 5 5
230 0 $out = Quiq::FileHandle->new('>','/dev/tty');
231 }
232 5 print $out $prompt;
233  
234 5 ▶ if ($ttyIn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ttyIn) 0 5 5
235 0 $in = Quiq::FileHandle->new('<','/dev/tty');
236 }
237  
238 5 ▶ if ($timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout) { } 0 5 5
239 0 eval {
240 0 ▶ local $SIG{ALRM} = sub {die "alarm\n"}; # \n erforderlich
Subroutine __ANON__: not called CC=1 SCAR=6.9
241 0 alarm $timeout;
242 0 $answ = <$in>;
243 0 alarm 0;
244 };
245 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
246 # Timeout abgelaufen
247  
248 0 ▶ if ($@ ne "alarm\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ ne "alarm\n") 0 0 0
249 # Unerwarteten Fehler weiterleiten
250 0 die;
251 }
252 0 say $answ = $default;
253 }
254 }
255 else {
256 5 $answ = <$in>;
257 }
258  
259 5 ▶ if ($ttyIn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ttyIn) 0 5 5
260 0 $in->close;
261 }
262  
263 5 ▶ if (defined $answ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $answ) { } 5 0 5
264 # Wert bereinigen
265  
266 5 $answ =~ s/^\s+//;
267 5 $answ =~ s/\s+$//;
268 5 ▶ $answ = $default if $answ eq '' && defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if $answ eq "" and defined $default 1 4 5
Condition100%3/3
$answ eq "" and defined $default
!ll&&!rl&&r
311
MC/DC100%2/2
$answ eq "" and defined $default
$answ eq "" defined $default
Truth table100%3/3
A and B
A= $answ eq ""B= defined $default
ABresult
0X0
100
111
269 }
270 else { # eof
271 0 print $out "\n";
272 }
273 5 ▶ if ($ttyOut) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ttyOut) 0 5 5
274 0 $out->close;
275 }
276  
277 5 ▶ if (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) 0 5 5
278 0 ▶ next if !defined $answ; # kein Ausstieg mit ^D
Branch: 1 branch
BranchTrueFalseTotal
unless defined $answ 0 0 0
279 0 ▶ next if !$sloppy && !grep { $_ eq $answ } @values;
Branch: 1 branch
BranchTrueFalseTotal
unless $sloppy or grep {$_ eq $answ;} @values 0 0 0
Condition0%0/3
$sloppy or grep {$_ eq $answ;} @values
l!l&&r!l&&!r
000
MC/DC0%0/2
$sloppy or grep {$_ eq $answ;} @values
$sloppy grep {$_ eq $answ;} @values
Truth table0%0/3
A or B
A= $sloppyB= grep {$_ eq $answ;} @values
ABresult
1X1
011
000
280 }
281 5 last; # Ausstieg
282 }
283  
284 5 ▶ if ($timer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timer) 0 5 5
285 0 $$timer += Time::HiRes::gettimeofday-$t0;
286 }
287  
288 5 return $answ;
289 }
290  
291 # -----------------------------------------------------------------------------
292  
293 =head3 ansiEsc() - Liefere ANSI Terminal Escape-Sequenz
294  
295 =head4 Synopsis
296  
297 $esc = $class->ansiEsc($str);
298  
299 =head4 Description
300  
301 Liefere die Terminal Escape-Sequenz $esc für die in $str angegebenen
302 Terminal-Eigenschaften. Es kann eine Kombination aus Eigenschaften
303 angegeben werden. Die Eigenschaften werden durch Leerzeichen getrennt.
304  
305 Beginnt $str mit ESC, d.h. ist $str bereits eine Escape-Sequenz,
306 wird $str unverändert zurückgeliefert.
307  
308 B<Terminal-Eigenschaften>
309  
310 Allgemein Vordergrund Hintergrund
311 ----------- ----------- -----------
312 dark black on_black
313 bold red on_red
314 underline green on_green
315 blink yellow on_yellow
316 reverse blue on_blue
317 concealed magenta on_magenta
318 reset cyan on_cyan
319 white on_white
320  
321 =head4 Example
322  
323 Rote Schrift:
324  
325 $esc = Quiq::Terminal->ansiEsc('red');
326  
327 Fette weiße Schrift auf rotem Grund:
328  
329 $esc = Quiq::Terminal->ansiEsc('bold white on_red');
330  
331 Terminal in den Anfangszustand zurückversetzen:
332  
333 $esc = Quiq::Terminal->ansiEsc('reset');
334  
335 =cut
336  
337 # -----------------------------------------------------------------------------
338  
339 2 ▶sub ansiEsc {
Subroutine ansiEsc: called CC=2 SCAR=6.9
Pod documented
340 2 my ($class,$str) = @_;
341 2 ▶ return substr($str,0,1) eq "\e"? $str: Term::ANSIColor::color($str);
Branch: 1 branch
BranchTrueFalseTotal
substr($str, 0, 1) eq "\e" ? : 1 1 2
342 }
343  
344 # -----------------------------------------------------------------------------
345  
346 =head3 width() - Liefere die Breite des Terminals
347  
348 =head4 Synopsis
349  
350 $width = $this->width;
351  
352 =head4 Returns
353  
354 Integer
355  
356 =head4 Description
357  
358 Ermittele die Anzahl der Spalten des Terminals und liefere diese zurück.
359  
360 =cut
361  
362 # -----------------------------------------------------------------------------
363  
364 1 ▶sub width {
Subroutine width: called CC=1 SCAR=0.0
Pod documented
365 1 my $this = shift;
366  
367 1 my $cmd = 'tput cols';
368 1 my $width = `$cmd`;
369 1 Quiq::Exit->check($?,$cmd);
370 1 chomp $width;
371  
372 1 return $width;
373 }
374  
375 # -----------------------------------------------------------------------------
376  
377 =head3 height() - Liefere die Höhe des Terminals
378  
379 =head4 Synopsis
380  
381 $height = $this->height;
382  
383 =head4 Returns
384  
385 Integer
386  
387 =head4 Description
388  
389 Ermittele die Anzahl der Zeilen des Terminals und liefere diese zurück.
390  
391 =cut
392  
393 # -----------------------------------------------------------------------------
394  
395 1 ▶sub height {
Subroutine height: called CC=1 SCAR=0.0
Pod documented
396 1 my $this = shift;
397  
398 1 my $cmd = 'tput lines';
399 1 my $height = `$cmd`;
400 1 Quiq::Exit->check($?,$cmd);
401 1 chomp $height;
402  
403 1 return $height;
404 }
405  
406 # -----------------------------------------------------------------------------
407  
408 =head1 VERSION
409  
410 1.240
411  
412 =head1 AUTHOR
413  
414 Frank Seitz, L<http://fseitz.de/>
415  
416 =head1 COPYRIGHT
417  
418 Copyright (C) 2026 Frank Seitz
419  
420 =head1 LICENSE
421  
422 This code is free software; you can redistribute it and/or modify
423 it under the same terms as Perl itself.
424  
425 =cut
426  
427 # -----------------------------------------------------------------------------
428  
429 270 1;
430  
431 # eof
« Quiq/Tree.pm ↑ Summary Quiq/Sql.pm »