#!perl

our $DATE = '2016-07-30'; # DATE
our $VERSION = '0.03'; # VERSION

use 5.010001;
use strict;
use warnings;

use Getopt::Long qw(:config auto_help auto_version);
use Term::ReadKey;
use Time::HiRes qw(time sleep);

my %opts = (
    update_delay => 0.02,
    banner_size  => 0,
);
GetOptions(
    'update-delay|d=f' => \$opts{update_delay},
    'banner-size|b=i'  => \$opts{banner_size},
);

# borrowed from Games::2048
sub read_key {
    my $self = shift;
    state @keys;

    if (@keys) {
        return shift @keys;
    }

    my $char;
    my $packet = '';
    while (defined($char = ReadKey -1)) {
        $packet .= $char;
    }

    while ($packet =~ m(
                           \G(
                               \e \[          # CSI
                               [\x30-\x3f]*   # Parameter Bytes
                               [\x20-\x2f]*   # Intermediate Bytes
                               [\x40-\x7e]    # Final Byte
                           |
                               .              # Otherwise just any character
                           )
                   )gsx) {
        push @keys, $1;
    }

    return shift @keys;
}

my $time0 = time();
my $time;
my $stopped;
my $stoptime;
$|++;
ReadMode "cbreak";

if ($opts{banner_size} > 0) {
    require Text::Banner;
    require Text::NonWideChar::Util;
    $opts{update_delay} = 1;
}

TIMER:
while (1) {
    my $str;
    my $size;

    while (defined(my $key = read_key())) {
        if ($key eq 'q' || $key eq 'Q') {
            if ($opts{banner_size} > 0) {
                if ($str) {
                    for (1..$size->[1]) { print "\n" }
                }
            } else {
                print "\n";
            }
            last TIMER;
        } elsif ($key eq ' ' || $key eq 'r' || $key eq 'R') {
            print "\n" unless $opts{banner_size} > 0;
        } elsif ($key eq 'z' || $key eq 'Z') {
            $time0 = time();
            $time      = $time0 if $stopped;
            $stoptime  = $time0 if $stopped;
        } elsif ($key eq "\n" || $key eq 's' || $key eq 'S') {
            $stopped = !$stopped;
            $stoptime = time() if $stopped;
            $time0 += (time()-$stoptime) if !$stopped;
        }
    }

    {
        $time = time() unless $stopped;
        my $elapsed = $time - $time0;
        my $hours   = int( $elapsed/3600);
        my $minutes = int(($elapsed-$hours*3600)/60);
        my $seconds = int( $elapsed-$hours*3600-$minutes*60);
        my $millis  = int(($elapsed-$hours*3600-$minutes*60-$seconds)*1000);

        if ($opts{banner_size} > 0) {
            my $str2 = sprintf("%02d:%02d", $minutes, $seconds);
            my $ban = Text::Banner->new;
            $ban->set($str2);
            $ban->size($opts{banner_size});
            $ban->fill('*');
            $str = $ban->get;
            print $str;
            sleep $opts{update_delay};
            $size = Text::NonWideChar::Util::length_height($str);
            print "\x08" x $size->[0]; # move left
            printf "\e[%dA", ($size->[1]-1); # move up
        } else {
            $str = sprintf("%02d:%02d:%02d.%03d ",
                           $hours, $minutes, $seconds, $millis);
            print $str;
            sleep $opts{update_delay};
            print "\x08" x length($str);
        }
    }
}
ReadMode "normal";

# ABSTRACT: A console-based virtual stopwatch and timer
# PODNAME: stopw

__END__

=pod

=encoding UTF-8

=head1 NAME

stopw - A console-based virtual stopwatch and timer

=head1 VERSION

This document describes version 0.03 of stopw (from Perl distribution App-stopw), released on 2016-07-30.

=head1 SYNOPSIS

 % stopw [options]

See manpage for list of options.

=head1 DESCRIPTION

B<stopw> is a console-based virtual stopwatch and timer. After you run the
program, you can press:

=over

=item * C<Z> to reset the timer back to zero

=item * C<R> (or spacebar) to record time

This will print the current value of timer and move to the next line.

=item * C<S> (or Enter) to start/stop the timer

=item * C<Q> (or Ctrl-C) to quit the program

=back

=head1 OPTIONS

=head2 --update-delay, -d (float, default: 0.02)

=head2 --banner-size, -b (int, 0-5, default: 0)

If set to a number between 1-5, will print a large banner (ASCII-art) of
C<mm:ss> digits instead of regular text (using L<Text::Banner>). This will also
automatically set --update-delay to 1.

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-stopw>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-stopw>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-stopw>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 SEE ALSO

L<App::Stopwatch> which includes the L<stopwatch> console utility. This utility
is geared more towards executing a command after a specified time period.

The B<stopwatch> Debian package, a graphical (Tk-based) virtual stopwatch and
timer.

The L<time> Unix command.

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
