From 73b6f2c176994c3c78f17f47547ee5c464a8a17c Mon Sep 17 00:00:00 2001
From: "Alan J. Pippin" <ajp@pippins.net>
Date: Mon, 3 Mar 2008 07:14:00 -0700
Subject: [PATCH] Changed precision of gigabyte sizes to be xx.xx. Made column
 width of filesystem dynamically sized.

---
 zfs-log-parser      |  2 +-
 zfs-snapshot-totals | 15 +++++++++------
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/zfs-log-parser b/zfs-log-parser
index 7149609..5657725 100755
--- a/zfs-log-parser
+++ b/zfs-log-parser
@@ -33,7 +33,7 @@ sub adjust_duration {
 
 sub adjust_data {
     my ($data) = @_;
-    if($data > ($giga)) { $data = int($data / $giga); $data = "$data"."Gb"; }
+    if($data > ($giga)) { $data = $data / $giga; $data = sprintf("%2.2fGb",$data); }
     elsif($data > ($mega)) { $data = int($data / $mega); $data = "$data"."Mb"; }
     elsif($data > ($kilo)) { $data = int($data / $kilo); $data = "$data"."Kb"; }
     return $data;
diff --git a/zfs-snapshot-totals b/zfs-snapshot-totals
index 09abb74..be223d9 100755
--- a/zfs-snapshot-totals
+++ b/zfs-snapshot-totals
@@ -5,11 +5,12 @@
 $kilo = 1024;
 $mega = 1024 * 1024;
 $giga = 1024 * 1024 * 1024;
+$maxlen = 0;
 
 sub adjust_size
 {
   my ($size) = @_;
-  if($size > ($giga)) { $size = int($size / $giga); $size = "$size"."G"; }
+  if($size > ($giga)) { $size = $size / $giga; $size = sprintf("%2.2fG",$size); }
   elsif($size > ($mega)) { $size = int($size / $mega); $size = "$size"."M"; }
   elsif($size > ($kilo)) { $size = int($size / $kilo); $size = "$size"."K"; }
   return $size;
@@ -21,6 +22,7 @@ foreach $snapshot (@snapshots)
   if($snapshot =~ /(\S+)\@(\S+)\s+(\S+)\s+/) {
     $filesystem = $1;
     $size = $3;
+    if(length($filesystem) > $maxlen) { $maxlen = length($filesystem); }
     if($size =~ /k/i) { $size = $size * $kilo; }
     if($size =~ /m/i) { $size = $size * $mega; }
     if($size =~ /g/i) { $size = $size * $giga; }
@@ -29,18 +31,19 @@ foreach $snapshot (@snapshots)
   }
 }
 
-printf "%-30s %-15s %-10s\n","ZFS Filesystem","Snapshots Size","Num Snapshots";
-printf "%-30s %-15s %-10s\n","--","--","--";
+$maxlen=$maxlen+2;
+printf "%-${maxlen}s %-15s %-10s\n","ZFS Filesystem","Snapshots Size","Num Snapshots";
+printf "%-${maxlen}s %-15s %-10s\n","--","--","--";
 foreach $key (sort keys %totals)
 {
   $size = $totals{$key}{size};
   $total_size += $size;
   $total_snapshots += $totals{$key}{snapshots};
   $size = &adjust_size($size);
-  printf "%-30s %-15s %-10s\n", $key, $size, $totals{$key}{snapshots};
+  printf "%-${maxlen}s %-15s %-10s\n", $key, $size, $totals{$key}{snapshots};
 }
 
 $total_size = &adjust_size($total_size);
-printf "%-30s %-15s %-10s\n","--","--","--";
-printf "%-30s %-15s %-10s\n","Total Snapshots",$total_size,$total_snapshots;
+printf "%-${maxlen}s %-15s %-10s\n","--","--","--";
+printf "%-${maxlen}s %-15s %-10s\n","Total Snapshots",$total_size,$total_snapshots;
 
-- 
2.34.1