-
Notifications
You must be signed in to change notification settings - Fork 13
/
ran
executable file
·71 lines (61 loc) · 1.71 KB
/
ran
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 -wl
#
# Advanced replacement for seq(1)
#
# Adam Spiers <ran@adamspiers.org>
#
# $Id$
use Getopt::Long;
my %opts;
GetOptions(
\%opts,
'help',
'delimiter=s', 'format=s', 'lines', 'space-pad|p', 'zero-pad|z',
) or usage();
usage() if @ARGV == 0 or $opts{help};
my $delimiter = delimiter();
my @items = eval "@ARGV";
my $format = get_format(\@items);
my @formatted_items = map { sprintf $format, $_ } @items;
print join qqexpand($delimiter), @formatted_items;
sub usage {
warn @_, "\n" if @_;
die <<EOUSAGE;
Usage: ran [options] <Perl list>
Options:
-d, --delimiter=STR Set output delimiter
-l, --lines Set output delimiter to \\n
-f, --format=STR Use printf format string
-p, --space-pad Equalize width by padding with spaces
-z, --zero-pad Equalize width by padding with leading zeroes
EOUSAGE
}
sub qqexpand {
my ($delimiter) = @_;
$delimiter =~ s!\\([efnrt]|x\d\d?|0\d{2,3})!qq["$&"]!gee;
return $delimiter;
}
sub get_format {
my ($items) = @_;
my @foo = map { defined $opts{$_} ? 1 : 0 } qw(format space-pad zero-pad);
usage("You must only select one output format.\n")
if $foo[0] + $foo[1] + $foo[2] > 1;
return '%0' . max_width($items) . 'd' if $opts{'zero-pad'};
return '%' . max_width($items) . 's' if $opts{'space-pad'};
return $opts{format} if $opts{'format'};
return '%s';
}
sub max_width {
my ($items) = @_;
my $max_width = 0;
for (@$items) {
$max_width = length if length > $max_width;
}
return $max_width;
}
sub delimiter {
usage("You cannot specify both --delimiter and --lines.\n")
if $opts{delimiter} && $opts{lines};
return "\n" if $opts{lines};
return $opts{delimiter} || ' ';
}