#!/usr/bin/env perl use strict; use warnings; use 5.010; use autodie; use Date::Format; use GD; use Getopt::Std; use POSIX 'ceil'; use Simplestore; use Term::ANSIColor; my %opts; getopts('a:cfgt:', \%opts); my $count = shift // 5; my $file = '/home/derf/stuff/work/zivildienst/schichten'; my $table = Simplestore::load($file); my ($w, $h) = (($count < 8) ? ($count * 40) : (7 * 40), ceil($count / 7) * 40); my $alpha = $opts{'a'} // 0; my $im = GD::Image->new($w, $h); $im->saveAlpha(1); my $black = $im->colorAllocateAlpha( 0, 0, 0, $alpha); my $gray = $im->colorAllocateAlpha(127, 127, 127, $alpha); my $lgray = $im->colorAllocateAlpha(191, 191, 191, $alpha); my $white = $im->colorAllocateAlpha(255, 255, 255, $alpha); my $blue = $im->colorAllocateAlpha(200, 200, 255, $alpha); my $green = $im->colorAllocateAlpha(200, 255, 200, $alpha); my $yellow = $im->colorAllocateAlpha(255, 255, 200, $alpha); my $red = $im->colorAllocateAlpha(255, 191, 191, $alpha); my $text; my $start = time() - (3600 * 3); # ^^^^^^^^ # Nerd daychange adjustment (3am makes more sense) if ($opts{'t'}) { $start -= $opts{'t'} * (3600 * 24); } for my $i (1 .. $count) { my @time = localtime($start + (3600 * 24 * $i)); my $day = strftime('%a', @time); my $mday = strftime('%d', @time); my $date = strftime('%m/%d', @time); my $cur = $table->{ $date }; my $colour = $white; my $x_off = 40 * (($i - 1) % 7); my $y_off = int(($i - 1) / 7) * 40; if (not defined $cur) { next; } if ($cur !~ /^ \d+ ( \. \d+)? $/x) { $im->filledRectangle($x_off + 1, $y_off + 1, $x_off + 39, $y_off + 39, $colour); $im->string(gdSmallFont, $x_off + 2, $y_off + 2, "${day} ${mday}", $black); $im->rectangle($x_off, $y_off, $x_off + 40, $y_off + 40, $gray); $text .= strftime("%a %b %d\n", @time); next; } if ($cur ~~ [qw[s S]]) { $cur = 13; } if ($cur < 8) { $colour = $blue; $text .= color('white on_blue'); } elsif ($cur < 10) { $colour = $green; $text .= color('black on_green'); } elsif ($cur < 13) { $colour = $yellow; $text .= color('black on_yellow'); } else { $colour = $red; $text .= color('white on_red'); } $im->filledRectangle($x_off + 1, $y_off + 1, $x_off + 39, $y_off + 39, $colour); $im->string(gdSmallFont, $x_off + 2, $y_off + 2, "${day} ${mday}", $black); $im->string(gdSmallFont, $x_off + 6, $y_off + 20, sprintf('%02d:%02d', $cur, ($cur - int($cur)) * 60), $black); $im->rectangle($x_off, $y_off, $x_off + 40, $y_off + 40, $gray); $text .= sprintf( "%s%s %02d:%02d -> %02d:%02d\n", strftime("%a %b %d", @time), color('reset'), int($cur), ($cur - int($cur)) * 60, $cur + 8, ($cur - int($cur)) * 60, ); } $im->rectangle(0, 0, $w - 1, $h -1, $gray); if ($opts{'c'}) { print $text; } if ($opts{'g'}) { open(my $out_fh, '>', '/tmp/vzds.png'); binmode $out_fh; print $out_fh $im->png(); close($out_fh); } if ($opts{'f'}) { exec('feh', '/tmp/vzds.png'); }