| 1 | package App::JobLog::Log::Format; | ||
| 2 | $App::JobLog::Log::Format::VERSION = '1.042'; | ||
| 3 | # ABSTRACT: pretty printer for log | ||
| 4 | |||
| 5 | |||
| 6 | use Exporter 'import'; | ||
| 7 | our @EXPORT_OK = qw( | ||
| 8 | display | ||
| 9 | duration | ||
| 10 | single_interval | ||
| 11 | summary | ||
| 12 | wrap | ||
| 13 | ); | ||
| 14 | |||
| 15 | use Modern::Perl; | ||
| 16 | use App::JobLog::Config qw( | ||
| 17 | day_length | ||
| 18 | is_workday | ||
| 19 | precision | ||
| 20 | ); | ||
| 21 | use App::JobLog::Log::Synopsis qw(collect :merge); | ||
| 22 | use Text::WrapI18N qw(); | ||
| 23 | use App::JobLog::TimeGrammar qw(parse); | ||
| 24 | |||
| 25 | use constant TAG_COLUMN_LIMIT => 10; | ||
| 26 | use constant MARGIN => 5; | ||
| 27 | |||
| 28 | # minimum width of description column | ||
| 29 | use constant MIN_WIDTH => 20; | ||
| 30 | use constant DURATION_FORMAT => '%0.' . precision . 'f'; | ||
| 31 | |||
| 32 | |||
| 33 | sub summary { | ||
| 34 | my ( $phrase, $test, $hidden, $do_notes ) = @_; | ||
| 35 | |||
| 36 | # we skip flex days if the events are at all filtered | ||
| 37 | my $skip_flex = !$do_notes && ( $test || 0 ); | ||
| 38 | $test //= sub { $_[0] }; | ||
| 39 | my ( $start, $end ) = parse $phrase; | ||
| 40 | my $show_year = $start->year < $end->year; | ||
| 41 | unless ($skip_flex) { | ||
| 42 | |||
| 43 | # if we are chopping off any of the first and last days we ignore flex time | ||
| 44 | $skip_flex = 1 | ||
| 45 | unless $do_notes || _break_of_dawn($start) && _witching_hour($end); | ||
| 46 | } | ||
| 47 | my $method = $do_notes ? 'find_notes' : 'find_events'; | ||
| 48 | my $events = App::JobLog::Log->new->$method( $start, $end ); | ||
| 49 | my @days = @{ _days( $start, $end, $skip_flex, $do_notes ) }; | ||
| 50 | my @periods = | ||
| 51 | $hidden->{vacation} ? () : App::JobLog::Vacation->new->periods; | ||
| 52 | |||
| 53 | # drop the vacation days that can't be relevant | ||
| 54 | unless ( $hidden->{vacation} ) { | ||
| 55 | my $e = | ||
| 56 | App::JobLog::Log::Event->new( | ||
| 57 | App::JobLog::Log::Line->new( time => $start ) ); | ||
| 58 | $e->end = $end; | ||
| 59 | for ( my $i = 0 ; $i < @periods ; $i++ ) { | ||
| 60 | my $p = $periods[$i]; | ||
| 61 | if ( $skip_flex && $p->flex || !$p->conflicts($e) ) { | ||
| 62 | splice @periods, $i, 1; | ||
| 63 | $i--; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | # collect events into days | ||
| 69 | my @gathered; | ||
| 70 | for my $big_e (@$events) { | ||
| 71 | for my $e ( $big_e->split_days ) { | ||
| 72 | if ( $e = $test->($e) ) { | ||
| 73 | push @gathered, shift @days | ||
| 74 | while @days && $days[0]->end < $e->start; | ||
| 75 | for my $d (@days) { | ||
| 76 | if ( $e->intersects( $d->pseudo_event ) ) { | ||
| 77 | push @{ $d->events }, $e; | ||
| 78 | last; | ||
| 79 | } | ||
| 80 | |||
| 81 | # I believe these is_open bits are mistaken | ||
| 82 | # last if $e->is_open; | ||
| 83 | unless ($do_notes) { | ||
| 84 | last if $d->start > $e->end; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | } | ||
| 90 | unshift @days, @gathered; | ||
| 91 | |||
| 92 | # add in vacation times | ||
| 93 | for my $p (@periods) { | ||
| 94 | for my $d (@days) { | ||
| 95 | if ( is_workday( $d->start ) && $p->conflicts( $d->pseudo_event ) ) | ||
| 96 | { | ||
| 97 | my $clone = $p->clone; | ||
| 98 | if ( $clone->fixed ) { | ||
| 99 | push @{ $d->events }, $clone->overlap( $d->start, $d->end ); | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | $clone->start = $d->start->clone; | ||
| 103 | if ( $clone->flex ) { | ||
| 104 | $d->{deferred} = $clone; | ||
| 105 | } | ||
| 106 | else { | ||
| 107 | $clone->end = | ||
| 108 | $clone->start->clone->add( hours => day_length ); | ||
| 109 | push @{ $d->vacation }, $clone; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | # delete empty days | ||
| 117 | for ( my $i = 0 ; $i < @days ; $i++ ) { | ||
| 118 | my $d = $days[$i]; | ||
| 119 | if ( $d->is_empty && !is_workday( $d->start ) ) { | ||
| 120 | splice @days, $i, 1; | ||
| 121 | $i--; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | # fix deferred flex time and ensure events are chronologically ordered | ||
| 126 | for my $d (@days) { | ||
| 127 | my $flex = $d->{deferred}; | ||
| 128 | my @events = @{ $d->events }; | ||
| 129 | if ($flex) { | ||
| 130 | delete $d->{deferred}; | ||
| 131 | my $tr = $d->time_remaining; | ||
| 132 | if ( $tr > 0 ) { | ||
| 133 | $flex->end = $flex->start->clone->add( seconds => $tr ); | ||
| 134 | push @events, $flex; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | $d->{events} = [ sort { $a->cmp($b) } @events ] if @events > 1; | ||
| 138 | } | ||
| 139 | |||
| 140 | return \@days, $show_year; | ||
| 141 | } | ||
| 142 | |||
| 143 | # whether the date is the first moment in its day | ||
| 144 | sub _break_of_dawn { | ||
| 145 | my ($date) = @_; | ||
| 146 | return $date->hour == 0 && $date->minute == 0 && $date->second == 0; | ||
| 147 | } | ||
| 148 | |||
| 149 | # whether the date is the last moment in its day | ||
| 150 | sub _witching_hour { | ||
| 151 | my ($date) = @_; | ||
| 152 | return $date->hour == 23 && $date->minute == 59 && $date->second == 59; | ||
| 153 | } | ||
| 154 | |||
| 155 | # create a list of days about which we wish to collect information | ||
| 156 | sub _days { | ||
| 157 | my ( $start, $end, $skip_flex, $doing_notes ) = @_; | ||
| 158 | my @days; | ||
| 159 | my $b1 = $start; | ||
| 160 | my $b2 = $start->clone->add( days => 1 )->truncate( to => 'day' ); | ||
| 161 | while ( $b2 < $end ) { | ||
| 162 | push @days, | ||
| 163 | App::JobLog::Log::Day->new( | ||
| 164 | start => $b1, | ||
| 165 | end => $b2, | ||
| 166 | skip_flex => $skip_flex, | ||
| 167 | $doing_notes ? ( notes => 1 ) : (), | ||
| 168 | ); | ||
| 169 | $b1 = $b2; | ||
| 170 | $b2 = $b2->clone->add( days => 1 ); | ||
| 171 | } | ||
| 172 | push @days, | ||
| 173 | App::JobLog::Log::Day->new( | ||
| 174 | start => $b1, | ||
| 175 | end => $end, | ||
| 176 | skip_flex => $skip_flex, | ||
| 177 | $doing_notes ? ( notes => 1 ) : (), | ||
| 178 | ); | ||
| 179 | return \@days; | ||
| 180 | } | ||
| 181 | |||
| 182 | |||
| 183 | sub display { | ||
| 184 | my ( $days, $merge_level, $hidden, $screen_width, $show_year ) = @_; | ||
| 185 | |||
| 186 | if (@$days) { | ||
| 187 | collect $_, $merge_level for @$days; | ||
| 188 | my @synopses = map { @{ $_->synopses } } @$days; | ||
| 189 | |||
| 190 | my $columns = { | ||
| 191 | time => single_interval($merge_level) && !$hidden->{time}, | ||
| 192 | date => !$hidden->{date}, | ||
| 193 | tags => !$hidden->{tags}, | ||
| 194 | description => !$hidden->{description}, | ||
| 195 | duration => !$hidden->{duration}, | ||
| 196 | }; | ||
| 197 | $show_year &&= $columns->{date}; | ||
| 198 | my $format = _define_format( \@synopses, $columns, $screen_width ); | ||
| 199 | |||
| 200 | # keep track of various durations | ||
| 201 | my $times = { | ||
| 202 | total => 0, | ||
| 203 | untagged => 0, | ||
| 204 | expected => 0, | ||
| 205 | vacation => 0, | ||
| 206 | tags => {} | ||
| 207 | }; | ||
| 208 | |||
| 209 | # display synopses and add up durations | ||
| 210 | for my $d (@$days) { | ||
| 211 | $d->times($times); | ||
| 212 | $d->display( $format, $columns, $screen_width, $show_year ); | ||
| 213 | } | ||
| 214 | |||
| 215 | unless ( $hidden->{totals} ) { | ||
| 216 | my ( $m1, $m2 ) = | ||
| 217 | ( length 'TOTAL HOURS', length duration( $times->{total} ) ); | ||
| 218 | my @keys = keys %{ $times->{tags} }; | ||
| 219 | push @keys, 'UNTAGGED' if $times->{untagged}; | ||
| 220 | push @keys, 'VACATION' if $times->{vacation}; | ||
| 221 | for my $tag (@keys) { | ||
| 222 | my $l = length $tag; | ||
| 223 | $m1 = $l if $l > $m1; | ||
| 224 | } | ||
| 225 | $format = sprintf " %%-%ds %%%ds\n", $m1, $m2; | ||
| 226 | printf $format, 'TOTAL HOURS', duration( $times->{total} ); | ||
| 227 | printf $format, 'VACATION', duration( $times->{vacation} ) | ||
| 228 | if $times->{vacation}; | ||
| 229 | if ( %{ $times->{tags} } ) { | ||
| 230 | printf $format, 'UNTAGGED', duration( $times->{untagged} ) | ||
| 231 | if $times->{untagged}; | ||
| 232 | for my $key ( sort keys %{ $times->{tags} } ) { | ||
| 233 | my $d = $times->{tags}{$key}; | ||
| 234 | printf $format, $key, duration($d); | ||
| 235 | } | ||
| 236 | } | ||
| 237 | } | ||
| 238 | } | ||
| 239 | else { | ||
| 240 | say 'No events in interval specified.'; | ||
| 241 | } | ||
| 242 | } | ||
| 243 | |||
| 244 | # generate printf format for synopses | ||
| 245 | # returns format and wrap widths for tags and descriptions | ||
| 246 | sub _define_format { | ||
| 247 | my ( $synopses, $hash, $screen_width ) = @_; | ||
| 248 | |||
| 249 | #determine maximum width of each column | ||
| 250 | my $widths; | ||
| 251 | for my $s (@$synopses) { | ||
| 252 | if ( $hash->{tags} ) { | ||
| 253 | my $w1 = $hash->{widths}{tags} || 0; | ||
| 254 | my $ts = $s->tag_string; | ||
| 255 | if ( $screen_width > -1 && length $ts > TAG_COLUMN_LIMIT ) { | ||
| 256 | my $wrapped = wrap( $ts, TAG_COLUMN_LIMIT ); | ||
| 257 | $ts = ''; | ||
| 258 | for my $line (@$wrapped) { | ||
| 259 | $ts = $line if length $line > length $ts; | ||
| 260 | } | ||
| 261 | } | ||
| 262 | my $w2 = length $ts; | ||
| 263 | $hash->{widths}{tags} = $w2 if $w2 > $w1; | ||
| 264 | } | ||
| 265 | if ( $hash->{time} ) { | ||
| 266 | my $w1 = $hash->{widths}{time} || 0; | ||
| 267 | my $w2 = length $s->time_fmt; | ||
| 268 | $hash->{widths}{time} = $w2 if $w2 > $w1; | ||
| 269 | } | ||
| 270 | if ( $hash->{duration} ) { | ||
| 271 | my $w1 = $hash->{widths}{duration} || 0; | ||
| 272 | my $w2 = length duration( $s->duration ); | ||
| 273 | $hash->{widths}{duration} = $w2 if $w2 > $w1; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | my $margins = 0; | ||
| 277 | if ( $hash->{tags} && $hash->{widths}{tags} ) { | ||
| 278 | $margins++; | ||
| 279 | $hash->{formats}{tags} = sprintf '%%-%ds', $hash->{widths}{tags}; | ||
| 280 | |||
| 281 | # there seems to be a bug in Text::Wrap that requires tinkering with the column width | ||
| 282 | $hash->{widths}{tags}++; | ||
| 283 | } | ||
| 284 | if ( $hash->{time} && $hash->{widths}{time} ) { | ||
| 285 | $margins++; | ||
| 286 | $hash->{formats}{time} = sprintf '%%%ds', $hash->{widths}{time}; | ||
| 287 | } | ||
| 288 | if ( $hash->{duration} && $hash->{widths}{duration} ) { | ||
| 289 | $margins++; | ||
| 290 | $hash->{formats}{duration} = sprintf '%%%ds', $hash->{widths}{duration}; | ||
| 291 | } | ||
| 292 | if ( $hash->{description} ) { | ||
| 293 | if ( $screen_width == -1 ) { | ||
| 294 | $hash->{formats}{description} = '%s'; | ||
| 295 | } | ||
| 296 | else { | ||
| 297 | $margins++; | ||
| 298 | my $max_description = $screen_width; | ||
| 299 | for my $col (qw(time duration tags)) { | ||
| 300 | $max_description -= $hash->{widths}{$col} || 0; | ||
| 301 | } | ||
| 302 | $max_description -= $margins * 2; # left margins | ||
| 303 | $max_description -= MARGIN; # margin on the right | ||
| 304 | $max_description = MIN_WIDTH if $max_description < MIN_WIDTH; | ||
| 305 | $hash->{widths}{description} = $max_description; | ||
| 306 | $hash->{formats}{description} = sprintf '%%-%ds', $max_description; | ||
| 307 | } | ||
| 308 | } | ||
| 309 | |||
| 310 | my $format = ''; | ||
| 311 | for my $col (qw(time duration tags description)) { | ||
| 312 | my $f = $hash->{formats}{$col}; | ||
| 313 | $format .= " $f" if $f; | ||
| 314 | } | ||
| 315 | return $format; | ||
| 316 | } | ||
| 317 | |||
| 318 | |||
| 319 | sub wrap { | ||
| 320 | my ( $text, $columns ) = @_; | ||
| 321 | my @ar; | ||
| 322 | eval { | ||
| 323 | $Text::WrapI18N::columns = $columns; | ||
| 324 | my $s = Text::WrapI18N::wrap( '', '', $text ); | ||
| 325 | @ar = $s =~ /^.*$/mg; | ||
| 326 | }; | ||
| 327 | return \@ar unless $@; | ||
| 328 | return [$text]; | ||
| 329 | } | ||
| 330 | |||
| 331 | |||
| 332 | sub single_interval { | ||
| 333 | $_[0] == MERGE_ADJACENT | ||
| 334 | || $_[0] == MERGE_ADJACENT_SAME_TAGS | ||
| 335 | || $_[0] == MERGE_NONE; | ||
| 336 | } | ||
| 337 | |||
| 338 | |||
| 339 | sub duration { sprintf DURATION_FORMAT, $_[0] / ( 60 * 60 ) } | ||
| 340 | |||
| 341 | 1; | ||
| 342 | |||
| 343 | __END__ |