From: Mark Wooding Date: Fri, 24 Jul 2015 17:50:56 +0000 (+0100) Subject: lib/Odin.pm, bin/*.userv: New function for printing tabular reports. X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/odin-cgi/commitdiff_plain/cc346ee1adf852ba0f4322bc04a78ec64206d37a lib/Odin.pm, bin/*.userv: New function for printing tabular reports. --- diff --git a/bin/pastebin.userv b/bin/pastebin.userv index ed7ba7a..fb3b740 100755 --- a/bin/pastebin.userv +++ b/bin/pastebin.userv @@ -6,7 +6,6 @@ use Odin; use DBI; use Encode; use Encode::Locale; -use POSIX; my $BAD = 0; @@ -46,9 +45,8 @@ EOF FROM odin_pastebin WHERE owner = ? ORDER BY stamp", undef, $Odin::WHO)}) { my ($tag, $stamp, $lang, $title) = @$r; - my $t = strftime "%Y-%m-%d %H:%M:%S %z", localtime $stamp; - printf "%-25s %-12s %-16s %s\n", - $t, $tag, $lang, encode locale => $title; + Odin::print_columns Odin::fmt_time $stamp => 25, + $tag => 12, $lang => 16, (encode locale => $title) => 0; } } elsif ($op eq "new") { my $op = Odin::OptParse->new(@ARGV); diff --git a/bin/shorturl.userv b/bin/shorturl.userv index edad9f6..3bb1c8a 100755 --- a/bin/shorturl.userv +++ b/bin/shorturl.userv @@ -4,7 +4,6 @@ use lib "lib"; use Odin; use DBI; -use POSIX; Odin::cmdline_who; @@ -27,8 +26,7 @@ EOF FROM odin_shorturl WHERE owner = ? ORDER BY stamp", undef, $Odin::WHO)}) { my ($tag, $stamp, $url) = @$r; - my $t = strftime "%Y-%m-%d %H:%M:%S %z", localtime $stamp; - printf "%-25s %-12s %s\n", $t, $tag, $url; + Odin::print_columns Odin::fmt_time $stamp => 25, $tag => 12, $url =>0; } } elsif ($op eq "new") { @ARGV == 1 or Odin::fail "usage: new URL"; diff --git a/lib/Odin.pm b/lib/Odin.pm index 26a2f80..e373dcc 100644 --- a/lib/Odin.pm +++ b/lib/Odin.pm @@ -5,6 +5,7 @@ package Odin; use DBI; use Digest::SHA qw(sha256_hex); use MIME::Base64; +use POSIX; ###-------------------------------------------------------------------------- ### Early utilities. @@ -87,6 +88,26 @@ sub nice_name ($) { return lc $s; } +sub print_columns (@) { + my @col = reverse @_; + my @fmt = (); + my @val = (); + while (@col && $col[1] eq "") { splice @col, 0, 2; } + my ($wd, $v) = splice @col, 0, 2; + push @fmt, "%s"; push @val, $v; + while (@col) { + my ($wd, $v) = splice @col, 0, 2; + push @fmt, "%-${wd}s"; + push @val, $v; + } + printf join(" ", reverse @fmt) . "\n", reverse @val; +} + +sub fmt_time ($) { + my ($t) = @_; + return $t == -1 ? "--" : strftime "%Y-%m-%d %H:%M:%S %z", localtime $t; +} + ###-------------------------------------------------------------------------- ### Database utilities.