App-JobLog
view release on metacpan or search on metacpan
lib/App/JobLog/Command/when.pm view on Meta::CPAN
package App::JobLog::Command::when;
$App::JobLog::Command::when::VERSION = '1.042';
# ABSTRACT: when you'll be done for the day
use App::JobLog -command;
use Modern::Perl;
use Class::Autouse qw(
App::JobLog::Log
App::JobLog::Log::Day
);
use autouse 'App::JobLog::TimeGrammar' => qw(parse daytime);
use autouse 'Carp' => qw(carp);
use autouse 'Getopt::Long::Descriptive' => qw(prog_name);
use autouse 'App::JobLog::Config' => qw(
columns
is_hidden
merge
);
use autouse 'App::JobLog::Log::Format' => qw(
display
single_interval
summary
);
use autouse 'App::JobLog::Time' => qw(today now);
use constant FULL_FORMAT => '%l:%M:%S %p on %A, %B %d, %Y';
use constant SAME_YEAR_FORMAT => '%l:%M:%S %p on %A, %B %d';
use constant SAME_WEEK_FORMAT => '%l:%M:%S %p on %A';
use constant SAME_DAY_FORMAT => '%l:%M:%S %p';
sub execute {
my ( $self, $opt, $args ) = @_;
my $tags = $opt->tag || [];
my $excluded_tags = $opt->exclude_tag || [];
my $match = $opt->match || [];
my $no_match = $opt->no_match || [];
# validate regexes, if any, while generating test
my $test =
App::JobLog::Command::summary::_make_test( $tags, $excluded_tags, $match,
$no_match, undef );
# parse time expression
my $days;
my $start = ( join ' ', @$args ) || 'today';
eval { ( $days, undef ) = summary "$start through today", $test, {} };
$self->usage_error($@) if $@;
# check for long task
my ($last_e) = App::JobLog::Log->new->last_event;
if ( $last_e && $last_e->is_open ) {
my ( $then, $today ) = ( $last_e->start, today );
if (
!(
$then->year == $today->year
&& $then->month == $today->month
&& $then->day == $today->day
)
)
{
print <<END;
WARNING! The last event in the log has been open since before 12:00 am today!
END
}
}
my $remaining = 0;
$remaining += $_->time_remaining for @$days;
if ( $remaining == 0 ) {
say "\nyou are just now done";
}
else {
my $then = now->add( seconds => $remaining );
my $format;
if ( $then->year == now->year ) {
my $delta = abs $then->delta_days(now)->in_units('days');
if ( $delta > 7 ) {
$format = SAME_YEAR_FORMAT;
}
elsif ( $then->month == now->month && $then->day == now->day ) {
$format = SAME_DAY_FORMAT;
}
else {
$format = SAME_WEEK_FORMAT;
}
}
else {
$format = FULL_FORMAT;
}
if ( $then < now ) {
$then = ( $days->[-1]->last_event->end // now )->add( seconds => $remaining );
say "\nyou were finished at " . $then->strftime($format);
}
else {
say "\nyou will be finished at " . $then->strftime($format);
}
( run in 0.784 second using v1.01-cache-2.11-cpan-39bf76dae61 )