| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # Prompt/Timeout.pm | 
| 2 |  |  |  |  |  |  | # | 
| 3 |  |  |  |  |  |  | # $Id: Timeout.pm 24 2010-11-01 14:47:00Z stro $ | 
| 4 |  |  |  |  |  |  | # | 
| 5 |  |  |  |  |  |  | # Copyright (c) 2008, 2009 Serguei Trouchelle. All rights reserved. | 
| 6 |  |  |  |  |  |  | # | 
| 7 |  |  |  |  |  |  | # This program is free software; you can redistribute it and/or modify it | 
| 8 |  |  |  |  |  |  | # under the same terms as Perl itself. | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | # History: | 
| 11 |  |  |  |  |  |  | #  1.03  2010/11/01 Fixed problem with non-working "click", thanks to bottomsc[.]missouri.edu for reporting (RT#62535) | 
| 12 |  |  |  |  |  |  | #                   Fixed potential problem with TAP output parsing | 
| 13 |  |  |  |  |  |  | #  1.02  2009/08/31 Fixed problem with hanging in Unix environment, thanks to leocharre[.]cpan.org for reporting | 
| 14 |  |  |  |  |  |  | #  1.01  2008/05/23 Documentation typo | 
| 15 |  |  |  |  |  |  | #                   Removed Term::ReadKey::ReadMode because it doesn't work on some terminals | 
| 16 |  |  |  |  |  |  | #  1.00  2008/05/22 Initial revision | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | =head1 NAME | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | Prompt::Timeout - prompt() with auto-selecting default value in case of inactivity | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | use Prompt::Timeout; | 
| 25 |  |  |  |  |  |  | my $res = prompt ( $question, $default, $timeout ); | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | =cut | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | package Prompt::Timeout; | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 3 |  |  | 3 |  | 41403 | use strict; | 
|  | 3 |  |  |  |  | 9 |  | 
|  | 3 |  |  |  |  | 175 |  | 
| 32 | 3 |  |  | 3 |  | 20 | use warnings; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 166 |  | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 3 |  |  | 3 |  | 17 | use Carp; | 
|  | 3 |  |  |  |  | 9 |  | 
|  | 3 |  |  |  |  | 252 |  | 
| 35 | 3 |  |  | 3 |  | 3486 | use Term::ReadKey; | 
|  | 3 |  |  |  |  | 21944 |  | 
|  | 3 |  |  |  |  | 1810 |  | 
| 36 |  |  |  |  |  |  | require 5.006; | 
| 37 |  |  |  |  |  |  | require Exporter; | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | our @EXPORT = qw(prompt); | 
| 40 |  |  |  |  |  |  | our @ISA = qw(Exporter); | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | $Prompt::Timeout::VERSION = '1.03'; | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | This module provides only one function, prompt(), and it's exported by default. | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =head2 prompt | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | my $res = prompt ( $question, $default, $timeout ); | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | Prints a $question and waits for the input. | 
| 53 |  |  |  |  |  |  | If no keys are pressed during $timeout seconds, $default is assumed. | 
| 54 |  |  |  |  |  |  | When pressing just Enter key, $default is assumed. | 
| 55 |  |  |  |  |  |  | If prompt() detects that it is not running interactively, $default will be used without prompting too. | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | If you want to disable inactivity timer if any key is pressed during prompt(), | 
| 58 |  |  |  |  |  |  | you can invoke it with another optional parameter: | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | my $res = prompt ( $question, $default, $timeout, 1 ); | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | Once a key is pressed, inactivity timer is disabled. | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | When $timeout value is omitted, it assumes 60 seconds. | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | =cut | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | sub prompt ($;$$$) { | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 5 |  |  | 5 | 1 | 71 | my ($mess, $def, $timeout, $click) = @_; | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 5 | 100 |  |  |  | 15 | $timeout = 60 unless $timeout; | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 5 | 50 |  |  |  | 15 | Carp::confess("prompt function called without an argument") | 
| 75 |  |  |  |  |  |  | unless defined $mess; | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 5 | 50 |  |  |  | 32 | Carp::confess("timeout argument should be integer") | 
| 78 |  |  |  |  |  |  | unless $timeout =~ /^\d+$/; | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 5 |  | 33 |  |  | 38 | my $isa_tty = -t STDIN && (-t STDOUT || !(-f STDOUT || -c STDOUT)) ; | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 5 | 100 |  |  |  | 18 | my $dispdef = defined $def ? "[$def] " : " "; | 
| 83 | 5 | 100 |  |  |  | 12 | $def = defined $def ? $def : ""; | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 5 |  |  |  |  | 19 | local $|=1; | 
| 86 | 5 |  |  |  |  | 12 | local $\; | 
| 87 | 5 |  |  |  |  | 64 | print "$mess $dispdef:"; | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 5 |  |  |  |  | 9 | my $ans = ''; | 
| 90 | 5 | 50 | 33 |  |  | 77 | if (!$isa_tty && eof STDIN) { | 
| 91 | 5 |  |  |  |  | 37 | print "$def\n"; | 
| 92 |  |  |  |  |  |  | } else { | 
| 93 |  |  |  |  |  |  |  | 
| 94 | 0 |  |  |  |  | 0 | my $end = time + $timeout; | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | #ReadMode 4; # Turn off controls keys | 
| 97 | 0 |  |  |  |  | 0 | while (1) { | 
| 98 | 0 |  |  |  |  | 0 | my $key = ReadKey(1); # 1 sec | 
| 99 | 0 | 0 |  |  |  | 0 | if (defined $key) { | 
| 100 | 0 | 0 |  |  |  | 0 | last if $key =~ /^[\r\n]$/x; | 
| 101 | 0 |  |  |  |  | 0 | $ans .= $key; | 
| 102 | 0 |  |  |  |  | 0 | print $key; | 
| 103 | 0 |  |  |  |  | 0 | $end = time + $timeout; | 
| 104 |  |  |  |  |  |  | } else { | 
| 105 | 0 | 0 | 0 |  |  | 0 | unless ($click and $ans ne '') { | 
| 106 | 0 | 0 |  |  |  | 0 | if ($end < time) { | 
| 107 | 0 |  |  |  |  | 0 | $ans = $def; | 
| 108 | 0 |  |  |  |  | 0 | print "$def\n"; | 
| 109 | 0 |  |  |  |  | 0 | last; | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  | #ReadMode 0; # Reset tty mode before exiting | 
| 115 |  |  |  |  |  |  | } | 
| 116 | 5 | 50 | 33 |  |  | 51 | return (!defined $ans || $ans eq '') ? $def : $ans; | 
| 117 |  |  |  |  |  |  | } | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | =head1 AUTHOR | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | Serguei Trouchelle EFE | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | Prompt::Timeout uses partial code from ExtUtils::MakeMaker module. | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | =head1 COPYRIGHT | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | Copyright (c) 2008-2010 Serguei Trouchelle. All rights reserved. | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify it | 
| 130 |  |  |  |  |  |  | under the same terms as Perl itself. | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | =cut | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | 1; |