From 7b8795f231fe069892a92bff73345efd6f4def9e Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 7 Mar 2011 17:56:06 +0100 Subject: Move bar_png from vdf to Derf::Visual --- bin/vdf | 53 ++++++++++----------------------------------------- lib/Derf/Visual.pm | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 43 deletions(-) create mode 100644 lib/Derf/Visual.pm diff --git a/bin/vdf b/bin/vdf index dbaf055..cbf676f 100755 --- a/bin/vdf +++ b/bin/vdf @@ -8,6 +8,7 @@ use 5.010; use autodie; +use Derf::Visual 'GD_bar'; use Filesys::Df; use GD; use Getopt::Std; @@ -40,47 +41,6 @@ sub bar_console { ); } -sub bar_png { - my ($percent, $w, $h) = @_; - my $bar = GD::Image->new($w, $h); - my $l_int = 6; - - my ($w_o, $w_w) = ($w * 0.8, $w * 0.9); - - my $black = $bar->colorAllocateAlpha( 0, 0, 0, $alpha); - my $gray = $bar->colorAllocateAlpha(127, 127, 127, $alpha); - my $lgray = $bar->colorAllocateAlpha(191, 191, 191, $alpha); - my $ok_b = $bar->colorAllocateAlpha(220, 240, 220, $alpha); - my $ok_f = $bar->colorAllocateAlpha( 59, 191, 50, $alpha); - my $wa_b = $bar->colorAllocateAlpha(240, 240, 220, $alpha); - my $wa_f = $bar->colorAllocateAlpha(191, 191, 50, $alpha); - my $cr_b = $bar->colorAllocateAlpha(240, 220, 220, $alpha); - my $cr_f = $bar->colorAllocateAlpha(191, 0, 0, $alpha); - my $white = $bar->colorAllocateAlpha(255, 255, 255, $alpha); - - my $vwidth = sprintf("%d", ($w- 2) * $percent / 100); - - $bar->rectangle(0, 0, $w - 1, $h - 1, $gray); - $bar->filledRectangle(1, 1, $w - 2, $h - 2, $cr_b); - $bar->filledRectangle(1, 1, $w_w - 1, $h - 2, $wa_b); - $bar->filledRectangle(1, 1, $w_o - 1, $h - 2, $ok_b); - $bar->filledRectangle(1, 1, $vwidth , $h - 2, $ok_f); - - if ($vwidth > $w_o) { - $bar->filledRectangle($w_o, 1, $vwidth, $h - 2, $wa_f); - } - - if ($vwidth > $w_w) { - $bar->filledRectangle($w_w, 1, $vwidth, $h - 2, $cr_f); - } - - for my $i (1 .. $w / $l_int) { - $bar->line($i * $l_int, 1, $i * $l_int, $h - 2, $white); - } - - return $bar; -} - sub show_df_console { my $mp_width = 10; @@ -156,8 +116,15 @@ sub show_df_png { $im->string(gdSmallFont, $offset_free, $spacing * $i + $offset_top, sprintf("%6s", format_size($mount->[4]->{'bavail'})), $black); - $im->copy(bar_png($percent, $bar_width, $bar_height), $offset_left, - $spacing * $i + $bar_top, 0, 0, $bar_width, $bar_height); + $im->copy(GD_bar( + width => $bar_width, + height => $bar_height, + alpha => $alpha, + percent_filled => $percent, + lined => 6, + ), + $offset_left, $spacing * $i + $bar_top, 0, 0, $bar_width, + $bar_height); } open(my $out_fh, '>', '/tmp/vdf.png'); diff --git a/lib/Derf/Visual.pm b/lib/Derf/Visual.pm new file mode 100644 index 0000000..ee64040 --- /dev/null +++ b/lib/Derf/Visual.pm @@ -0,0 +1,56 @@ +package Derf::Visual; +use strict; +use warnings; +use 5.010; +use base 'Exporter'; +use GD; + +our @EXPORT_OK = qw(GD_bar); +our $VERSION = '0.1'; + +sub GD_bar { + my (%conf) = @_; + my ($w, $h) = @conf{'width', 'height'}; + my $percent = $conf{percent_filled}; + my $alpha = $conf{alpha}; + + my $bar = GD::Image->new($w, $h); + + my ($w_o, $w_w) = ($w * 0.8, $w * 0.9); + + my $black = $bar->colorAllocateAlpha( 0, 0, 0, $alpha); + my $gray = $bar->colorAllocateAlpha(127, 127, 127, $alpha); + my $lgray = $bar->colorAllocateAlpha(191, 191, 191, $alpha); + my $ok_b = $bar->colorAllocateAlpha(220, 240, 220, $alpha); + my $ok_f = $bar->colorAllocateAlpha( 59, 191, 50, $alpha); + my $wa_b = $bar->colorAllocateAlpha(240, 240, 220, $alpha); + my $wa_f = $bar->colorAllocateAlpha(191, 191, 50, $alpha); + my $cr_b = $bar->colorAllocateAlpha(240, 220, 220, $alpha); + my $cr_f = $bar->colorAllocateAlpha(191, 0, 0, $alpha); + my $white = $bar->colorAllocateAlpha(255, 255, 255, $alpha); + + my $vwidth = sprintf("%d", ($w - 2) * $percent / 100); + + $bar->rectangle(0, 0, $w - 1, $h - 1, $gray); + $bar->filledRectangle(1, 1, $w - 2, $h - 2, $cr_b); + $bar->filledRectangle(1, 1, $w_w - 1, $h - 2, $wa_b); + $bar->filledRectangle(1, 1, $w_o - 1, $h - 2, $ok_b); + $bar->filledRectangle(1, 1, $vwidth , $h - 2, $ok_f); + + if ($vwidth > $w_o) { + $bar->filledRectangle($w_o, 1, $vwidth, $h - 2, $wa_f); + } + + if ($vwidth > $w_w) { + $bar->filledRectangle($w_w, 1, $vwidth, $h - 2, $cr_f); + } + + if ($conf{lined}) { + for my $i (1 .. $w / $conf{lined}) { + $bar->line($i * $conf{lined}, 1, $i * $conf{lined}, $h - 2, + $white); + } + } + + return $bar; +} -- cgit v1.2.3