| 1 | package App::JobLog::Log::Day; | ||
| 2 | $App::JobLog::Log::Day::VERSION = '1.042'; | ||
| 3 | # ABSTRACT: collects events and vacation in a complete day | ||
| 4 | |||
| 5 | |||
| 6 | use Modern::Perl; | ||
| 7 | use App::JobLog::Config qw( | ||
| 8 | day_length | ||
| 9 | is_workday | ||
| 10 | precision | ||
| 11 | ); | ||
| 12 | use Carp qw(carp); | ||
| 13 | use Text::Wrap; | ||
| 14 | use App::JobLog::Log::Format qw( | ||
| 15 | duration | ||
| 16 | wrap | ||
| 17 | ); | ||
| 18 | |||
| 19 | use constant WORK_SECONDS => 60 * 60 * day_length; | ||
| 20 | |||
| 21 | |||
| 22 | sub new { | ||
| 23 | my ( $class, %opts ) = @_; | ||
| 24 | $class = ref $class || $class; | ||
| 25 | bless { events => [], vacation => [], synopses => [], %opts }, $class; | ||
| 26 | } | ||
| 27 | |||
| 28 | |||
| 29 | sub concerns_notes { $_[0]->{notes} } | ||
| 30 | |||
| 31 | sub start { $_[0]->{start} } | ||
| 32 | |||
| 33 | sub end { $_[0]->{end} } | ||
| 34 | |||
| 35 | |||
| 36 | sub skip_flex { $_[0]->{skip_flex} } | ||
| 37 | |||
| 38 | |||
| 39 | sub time_remaining { | ||
| 40 | my ($self) = @_; | ||
| 41 | my $t = 0; | ||
| 42 | $t -= $_->duration for @{ $self->events }, @{ $self->vacation }; | ||
| 43 | $t += WORK_SECONDS if is_workday $self->start; | ||
| 44 | return $t; | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | sub events { $_[0]->{events} } | ||
| 49 | |||
| 50 | |||
| 51 | sub last_event { ( $_[0]->events || [] )->[-1] } | ||
| 52 | |||
| 53 | |||
| 54 | sub vacation { $_[0]->{vacation} } | ||
| 55 | |||
| 56 | |||
| 57 | sub synopses { $_[0]->{synopses} } | ||
| 58 | |||
| 59 | |||
| 60 | sub is_empty { | ||
| 61 | my ($self) = @_; | ||
| 62 | return !( @{ $self->events } || @{ $self->vacation } ); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub show_date { !$_[0]->no_date } | ||
| 66 | |||
| 67 | sub no_date { $_[0]->{no_date} } | ||
| 68 | |||
| 69 | |||
| 70 | sub times { | ||
| 71 | my ( $self, $times ) = @_; | ||
| 72 | return if $self->concerns_notes; | ||
| 73 | |||
| 74 | for my $e ( @{ $self->events }, @{ $self->vacation } ) { | ||
| 75 | my @tags = @{ $e->tags }; | ||
| 76 | my $d = $e->duration; | ||
| 77 | $times->{tags}{$_} += $d for @tags; | ||
| 78 | $times->{untagged} += $d unless @tags; | ||
| 79 | $times->{total} += $d; | ||
| 80 | $times->{vacation} += $d if $e->isa('App::JobLog::Vacation::Period'); | ||
| 81 | } | ||
| 82 | $times->{expected} += WORK_SECONDS | ||
| 83 | if is_workday $self->start; | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | sub display { | ||
| 88 | my ( $self, $format, $columns, $screen_width, $show_year ) = @_; | ||
| 89 | return if $self->is_empty; | ||
| 90 | |||
| 91 | # cache some bits from the $columns hash | ||
| 92 | my ( $show_times, $show_durations, $show_tags, $show_descriptions ) = | ||
| 93 | @{ $columns->{formats} }{qw(time duration tags description)}; | ||
| 94 | my $show_date = $columns->{date}; | ||
| 95 | my ( $tag_width, $description_width ) = | ||
| 96 | @{ $columns->{widths} }{qw(tags description)}; | ||
| 97 | |||
| 98 | # date | ||
| 99 | if ($show_date) { | ||
| 100 | my $f = $show_year ? '%A, %e %B, %Y' : '%A, %e %B'; | ||
| 101 | print $self->start->strftime($f), "\n"; | ||
| 102 | } | ||
| 103 | |||
| 104 | # activities | ||
| 105 | for my $s ( @{ $self->synopses } ) { | ||
| 106 | my @lines; | ||
| 107 | push @lines, [ $s->time_fmt ] if $show_times; | ||
| 108 | push @lines, [ duration( $s->duration ) ] if $show_durations; | ||
| 109 | push @lines, wrap( $s->tag_string, $tag_width ) if $show_tags; | ||
| 110 | push @lines, $screen_width == -1 | ||
| 111 | ? [ $s->description ] | ||
| 112 | : wrap( $s->description, $description_width ) | ||
| 113 | if $show_descriptions; | ||
| 114 | my $count = _pad_lines( \@lines ); | ||
| 115 | |||
| 116 | for my $i ( 0 .. $count ) { | ||
| 117 | say sprintf $format, _gather( \@lines, $i ); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | print "\n" | ||
| 121 | if $show_times | ||
| 122 | || $show_durations | ||
| 123 | || $show_tags | ||
| 124 | || $show_descriptions | ||
| 125 | || $show_date; | ||
| 126 | } | ||
| 127 | |||
| 128 | # add blank lines to short columns | ||
| 129 | # returns the number of lines to print | ||
| 130 | sub _pad_lines { | ||
| 131 | my ($lines) = @_; | ||
| 132 | my $max = 0; | ||
| 133 | for my $column (@$lines) { | ||
| 134 | $max = @$column if @$column > $max; | ||
| 135 | } | ||
| 136 | for my $column (@$lines) { | ||
| 137 | push @$column, '' while @$column < $max; | ||
| 138 | } | ||
| 139 | return $max - 1; | ||
| 140 | } | ||
| 141 | |||
| 142 | # collect the pieces of columns corresponding to a particular line to print | ||
| 143 | sub _gather { | ||
| 144 | my ( $lines, $i ) = @_; | ||
| 145 | my @line; | ||
| 146 | for my $column (@$lines) { | ||
| 147 | push @line, $column->[$i]; | ||
| 148 | } | ||
| 149 | return @line; | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | sub pseudo_event { | ||
| 154 | my ($self) = @_; | ||
| 155 | unless ( $self->{pseudo_event} ) { | ||
| 156 | my $e = | ||
| 157 | App::JobLog::Log::Event->new( | ||
| 158 | App::JobLog::Log::Line->new( time => $self->start ) ); | ||
| 159 | $e->end = $self->end; | ||
| 160 | $self->{pseudo_event} = $e; | ||
| 161 | } | ||
| 162 | return $self->{pseudo_event}; | ||
| 163 | } | ||
| 164 | |||
| 165 | 1; | ||
| 166 | |||
| 167 | __END__ |