summaryrefslogtreecommitdiff
path: root/bin/vzds
blob: b272ebf38c9edefa0e9fa7e90d0ad56eb38442c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/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');
}