App-BatchRelease

 view release on metacpan or  search on metacpan

lib/App/BatchRelease/Prompt.pm  view on Meta::CPAN

}


sub answers_from () {
    open( my $fh, '<', $TERMINAL ) or return '/dev/null';
    close $fh                      or return '/dev/null';
    return $TERMINAL;
}


sub interactive () {
    return IO::Interactive::Tiny::is_interactive() ? 1 : 0;
}


sub output_is_redirected () {
    return IO::Interactive::Tiny::is_interactive(*STDIN) && !IO::Interactive::Tiny::is_interactive();
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

App::BatchRelease::Prompt - Ask a person a question, also when the output is redirected

=head1 VERSION

version 0.001

=head1 SYNOPSIS

    use 5.040;
    use App::BatchRelease::Prompt;

    my $answer = App::BatchRelease::Prompt::ask('Go on? [y/N]');
    my $secret = App::BatchRelease::Prompt::ask( 'Password:', -echo => '*' );

=head1 DESCRIPTION

Every question that the program asks goes through L</ask>, over
L<IO::Prompter>.  The two problems that it solves are those that
C<trog-provisioner>'s C<Trog::Utils::prompt> solves, and in the same way.

=head1 FUNCTIONS

=head2 ask

C<($message, %options)>.  Asks the question C<$message>.  C<%options> go to
L<IO::Prompter> unchanged.

Returns an object, what the person typed, as L<IO::Prompter/prompt> returns
it: false when the input ended before an answer, and true for any line, an
empty one too.

    my $answer = App::BatchRelease::Prompt::ask( 'PAUSE password:', -echo => '*' );

For example, C<-echo>
masks what the person types, and C<-in> and C<-out> ask somewhere other than
standard input.

L<IO::Prompter> reads from C<*ARGV>, so it dies with C<Can't open *ARGV> in a
program that has arguments, such as the paths of checkouts.  This makes
C<*ARGV> C<local> to the call.

If standard input is a terminal and the output is not, for example under
C<&E<gt> file>, the question is asked at F</dev/tty>, and the answer is still
read from standard input.  Without that, L<IO::Prompter> shows neither the
question nor the masked echo, and the program waits with nothing on the
screen.  A C<-out> from the caller is used as given.

=head2 at_terminal

C<($what)>.

Returns a list, the options C<-in> and C<-out> for L</ask> that ask at
F</dev/tty>, for when standard input is spoken for, as when the credentials
came on it.

    my $answer = App::BatchRelease::Prompt::ask( 'Release? [y/N]', App::BatchRelease::Prompt::at_terminal('the confirmation') );

It dies, naming C<$what>, when there is no terminal.

=head2 answers_from

Returns a string, the path that a child process reads its answers from when
standard input is spoken for: F</dev/tty> when this process can open it, else
F</dev/null>,
where a question gets the end of the input and takes its default.  A process
with no controlling terminal can see F</dev/tty>, and even read its mode, and
still fails to open it, so this opens it to find out.

    my $stdin = App::BatchRelease::Prompt::answers_from();

=head2 interactive

Returns an integer: 1 when a person can answer a question on standard input,
because standard input and the selected output are both terminals, else 0.

    die "Pass --yes.\n" if !App::BatchRelease::Prompt::interactive();

A question that nobody
answers does not fail.  It stops the run until something kills it.  So a
caller asks this before it asks a question.

=head2 output_is_redirected

Returns a boolean, true when standard input is a terminal and the selected
output is not.  Then a person can type an answer but cannot see the question.

    my @out = App::BatchRelease::Prompt::output_is_redirected() ? ( -out => '/dev/tty' ) : ();

=head1 SEE ALSO

Please see those modules/websites for more information related to this module.



( run in 1.216 second using v1.01-cache-2.11-cpan-f5f21f1076c )