aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorspider-mario <spidermario@free.fr>2012-07-24 17:13:01 +0200
committerspider-mario <spidermario@free.fr>2012-07-28 12:22:16 +0200
commita30723e36dc48cb6a0ef0d086f363482bf82105a (patch)
treeb789a307c41fe8d5dc25c54323a16d83f59a1bc7
parent39f455eea7d2689bdb875e5cbc0899d1a79f0c5f (diff)
downloadponysay-a30723e36dc48cb6a0ef0d086f363482bf82105a.tar.gz
ponysay-a30723e36dc48cb6a0ef0d086f363482bf82105a.tar.bz2
ponysay-a30723e36dc48cb6a0ef0d086f363482bf82105a.zip
Rewrote ponysaylist.pl.
-rwxr-xr-xponysaylist.pl71
1 files changed, 20 insertions, 51 deletions
diff --git a/ponysaylist.pl b/ponysaylist.pl
index 401d83b..0e2db56 100755
--- a/ponysaylist.pl
+++ b/ponysaylist.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# ponysaylist
# Prints a list of ponies in columns
@@ -9,63 +9,32 @@
# Author: Mattias Andrée, maandree@kth.se
-$first = 1;
-$scrw = 1;
-$maxw = 1;
+use strict;
+use warnings;
+use utf8;
+use feature qw(say);
+use integer;
+use List::Util qw(max);
-foreach $arg (@ARGV)
-{
+my $scrw = shift @ARGV;
+
+for (@ARGV) {
# Format names from ponyies names
- $arg =~ s/([a-z])([A-Z])/\1 \2/;
- #$arg =~ s/_(.*)/\t(\1)/; ## Incompatible with `ponysay -L`
-
- if ($first == 1)
- { $first = 0;
- $scrw = $arg;
- }
- else
- { $w = length $arg;
- $maxw = $w if ($w > $maxw);
- }
+ s/(?<=[a-z])(?=[A-Z])/ /;
+ s/_(.*)/\t($1)/;
}
-$cols = int (($scrw + 2) / ($maxw + 2));
-$cols = 1 if ($cols < 1);
-
-
-@list = ();
-
-$first = 1;
-$items = 0;
-foreach $arg (@ARGV)
-{
- if ($first == 1)
- { $first = 0;
- }
- else
- { $ws = $maxw - (length $arg);
- push @list, $arg.(" "x$ws);
- $items += 1;
- }
-}
+my $maxw = max map {length} @ARGV;
+my $cols = max 1, (($scrw + 2) / ($maxw + 2));
-$rows = int (($items + $cols - 1) / $cols);
-$i = 0;
-@rowlist = ();
+my @list = map {sprintf "%-${maxw}s", $_} @ARGV;
-while ($i < $items)
-{ $row = 0;
- while (($row < $rows) and ($i < $items))
- {
- $rowlist[$row] .= " " unless ($i < $rows);
- $rowlist[$row] .= $list[$i];
- $row += 1;
- $i += 1;
-} }
+my $rows = (@list + $cols - 1) / $cols;
-foreach $row (@rowlist)
-{
- print $row."\n";
+my @rowlist;
+for my $i (0 .. $#list) {
+ push @{$rowlist[$i % $rows]}, $list[$i];
}
+say join ' ', @$_ for @rowlist;