summaryrefslogtreecommitdiff
path: root/bin/vwrap
blob: 27abf1942cc0b956995b3f77de12cd7fda76518f (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
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use autodie;

use GD;
use Getopt::Std;
use IPC::Run qw(run);

my %opts;

getopts('a:cfgh:o:w:C:', \%opts);

my $alpha  = $opts{a} // 0;
my $width  = $opts{w} // 200;
my $height = $opts{h} // 80;

my $outfile = $opts{o} // '/tmp/vwrap.png';

my $im = GD::Image->new($width, $height);
$im->saveAlpha(1);

my $white = $im->colorAllocateAlpha(255, 255, 255, $alpha);
my $gray  = $im->colorAllocateAlpha(127, 127, 127, $alpha);
my $fg = $im->colorAllocateAlpha(  0,   0,   0, $alpha);
my $bg = $im->colorAllocateAlpha(  0,   0,   0, 127);
my $buf;

if ($opts{'C'}) {
	$fg = $im->colorAllocateAlpha(split(qr{,}, $opts{'C'}), $alpha);
}

run(\@ARGV, \undef, \$buf, \*STDERR);

$buf =~ s/ä/a/g;
$buf =~ s/ö/o/g;
$buf =~ s/ü/u/g;

my @lines = split(/\n/, $buf);

if ($opts{'c'}) {
	say join("\n", @lines);
}
elsif ($opts{'g'}) {
	$im->filledRectangle(0, 0, $width, $height, $bg);

	my $used_height = @lines * 10;

	if ($used_height > $height) {
		$used_height = $height;
	}

	if ($used_height) {
		$used_height += 8;
		$im->filledRectangle(0, 0, $width, $used_height, $white);
		$im->rectangle(0, 0, $width, $used_height, $gray);
	}

	for my $i (0 .. $#lines) {
		$lines[$i] =~ s{\t}{};
		$im->string(gdSmallFont, 2, 2 + (10 * $i), $lines[$i], $fg);
	}

	open(my $out_fh, '>', $outfile);
	binmode $out_fh;
	print $out_fh $im->png();
	close($out_fh);
}

if ($opts{'f'}) {
	exec('feh', $outfile);
}