File Coverage

blib/lib/Lab/Measurement/KeyboardHandling.pm
Criterion Covered Total %
statement 8 32 25.0
branch 0 10 0.0
condition 0 3 0.0
subroutine 4 9 44.4
pod 0 6 0.0
total 12 60 20.0


line stmt bran cond sub pod time code
1             package Lab::Measurement::KeyboardHandling;
2             #ABSTRACT: Minimal keyboard input handling
3             $Lab::Measurement::KeyboardHandling::VERSION = '3.880';
4 7     7   1814 use v5.20;
  7         27  
5              
6 7     7   44 use Term::ReadKey;
  7         18  
  7         3536  
7              
8             my $labkey_initialized = 0;
9              
10             sub labkey_safe_exit {
11 7     7 0 44 ReadMode('normal');
12 7         452 exit(@_);
13             }
14              
15             sub labkey_safe_int {
16 0     0 0   ReadMode('normal');
17 0           exit(1);
18             }
19              
20             sub labkey_safe_die {
21 0     0 0   ReadMode('normal');
22              
23             # In order to print stack trace do not call exit(@_) here.
24             }
25              
26             sub labkey_init {
27 0     0 0   $SIG{'INT'} = \&labkey_safe_int;
28 0           $SIG{'QUIT'} = \&labkey_safe_exit;
29 0           $SIG{__DIE__} = \&labkey_safe_die;
30 7     7   6493 END { labkey_safe_exit(); }
31 0           ReadMode('raw');
32 0           $labkey_initialized = 1;
33             }
34              
35             sub labkey_check {
36              
37             # handle as much as we can here directly
38             # q - exit: if parameter soft=1 is passed, just return "DIE", otherwise exit
39             # p - pause, i.e. output "Measurement paused, press any key to continue"
40             # t - output script timing info
41 0     0 0   my $soft = shift;
42              
43 0 0 0       if ( ( $labkey_initialized == 1 )
44             && ( defined( my $key = ReadKey(-1) ) ) ) {
45              
46             # input waiting; it's in $key
47 0 0         if ( $key eq 'q' ) {
48 0           print "Terminating on keyboard request\n";
49 0 0         if ( $soft == 1 ) { return "DIE"; }
  0            
50 0           else { exit; }
51             }
52 0 0         if ( $key eq 'p' ) {
53 0           print "Measurement paused, press any key to continue\n";
54 0           while ( !defined( my $key = ReadKey(-1) ) ) {
55 0           sleep 1;
56             }
57 0           print "Measurement continuing\n";
58             }
59 0 0         if ( $key eq 't' ) {
60 0           print "Timing info following (maybe, sometime)\n";
61             }
62             }
63 0           return "";
64             }
65              
66             sub labkey_soft_check {
67 0     0 0   return labkey_check(1);
68             }
69              
70             1;
71              
72             __END__
73              
74             =pod
75              
76             =encoding UTF-8
77              
78             =head1 NAME
79              
80             Lab::Measurement::KeyboardHandling - Minimal keyboard input handling
81              
82             =head1 VERSION
83              
84             version 3.880
85              
86             =head1 COPYRIGHT AND LICENSE
87              
88             This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
89              
90             Copyright 2012 Andreas K. Huettel, Hermann Kraus
91             2016 Simon Reinhardt
92             2017 Andreas K. Huettel
93             2020 Andreas K. Huettel
94              
95              
96             This is free software; you can redistribute it and/or modify it under
97             the same terms as the Perl 5 programming language system itself.
98              
99             =cut