| 1 | package Lab::Measurement::KeyboardHandling; | ||||||||||||||||||||||||||||
| 2 | #ABSTRACT: Minimal keyboard input handling | ||||||||||||||||||||||||||||
| 3 | 7 | $Lab::Measurement::KeyboardHandling::VERSION = '3.881'; | |||||||||||||||||||||||||||
| 4 | 7 | ▶ | use v5.20; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Term::ReadKey; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 7 | my $labkey_initialized = 0; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | sub labkey_safe_exit { | ||||||||||||||||||||||||||||
| 11 | 7 | ▶ | ReadMode('normal'); | ||||||||||||||||||||||||||
|
Subroutine
labkey_safe_exit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 7 | exit(@_); | |||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | sub labkey_safe_int { | ||||||||||||||||||||||||||||
| 16 | 0 | ▶ | ReadMode('normal'); | ||||||||||||||||||||||||||
|
Subroutine
labkey_safe_int: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | 0 | exit(1); | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | sub labkey_safe_die { | ||||||||||||||||||||||||||||
| 21 | 0 | ▶ | ReadMode('normal'); | ||||||||||||||||||||||||||
|
Subroutine
labkey_safe_die: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # In order to print stack trace do not call exit(@_) here. | ||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub labkey_init { | ||||||||||||||||||||||||||||
| 27 | 0 | ▶ | $SIG{'INT'} = \&labkey_safe_int; | ||||||||||||||||||||||||||
|
Subroutine
labkey_init: not called CC=1 SCAR=4.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | 0 | $SIG{'QUIT'} = \&labkey_safe_exit; | |||||||||||||||||||||||||||
| 29 | 0 | $SIG{__DIE__} = \&labkey_safe_die; | |||||||||||||||||||||||||||
| 30 | 7 | ▶ | END { labkey_safe_exit(); } | ||||||||||||||||||||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | ▶ | my $soft = shift; | ||||||||||||||||||||||||||
|
Subroutine
labkey_check: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | if ( ( $labkey_initialized == 1 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$labkey_initialized == 1 and defined(my $key = ReadKey(-1))
MC/DC0%0/2
$labkey_initialized == 1 and defined(my $key = ReadKey(-1))
$labkey_initialized == 1
defined(my $key = ReadKey(-1))
Truth table0%0/3
A and B
A= $labkey_initialized == 1B= defined(my $key = ReadKey(-1))
| |||||||||||||||||||||||||||||
| 44 | && ( defined( my $key = ReadKey(-1) ) ) ) { | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # input waiting; it's in $key | ||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ( $key eq 'q' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | print "Terminating on keyboard request\n"; | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ( $soft == 1 ) { return "DIE"; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | else { exit; } | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | if ( $key eq 'p' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | print "Measurement paused, press any key to continue\n"; | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | while ( !defined( my $key = ReadKey(-1) ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | sleep 1; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 0 | print "Measurement continuing\n"; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if ( $key eq 't' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | print "Timing info following (maybe, sometime)\n"; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 0 | return ""; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub labkey_soft_check { | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | return labkey_check(1); | ||||||||||||||||||||||||||
|
Subroutine
labkey_soft_check: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 7 | 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.881 | ||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||