aboutsummaryrefslogtreecommitdiff
path: root/ponysaylist.pl
blob: 401d83baff3ffb66289be96f827adb80a7b45492 (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
#!/usr/bin/perl

# ponysaylist
# Prints a list of ponies in columns
# 
# Licensed under WTFPL
# See COPYING for details

# Author: Mattias Andrée, maandree@kth.se


$first = 1;
$scrw = 1;
$maxw = 1;

foreach $arg (@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);
    }
}

$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;
    }
}


$rows = int (($items + $cols - 1) / $cols);
$i = 0;
@rowlist = ();

while ($i < $items)
{   $row = 0;
    while (($row < $rows) and ($i < $items))
    {
	$rowlist[$row] .= "  " unless ($i < $rows);
	$rowlist[$row] .= $list[$i];
	$row += 1;
	$i += 1;
}   }

foreach $row (@rowlist)
{
    print $row."\n";
}