-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpad
More file actions
executable file
·132 lines (90 loc) · 2.52 KB
/
pad
File metadata and controls
executable file
·132 lines (90 loc) · 2.52 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env perl -w
#
# pad: pad lines to a given length
#
# 2006-10-03: Written by Steven J. DeRose.
# 2007-12-07 sjd: -l, -c, Getopt.
# 2010-09-10 sjd: perldoc, cleanup.
#
# To do:
# Add --column n to say where to insert padding
# Integrate into 'align'.
#
use strict;
use Getopt::Long;
our $VERSION_DATE = "2012-09-12";
my $column = 0;
my $left = 0;
my $padchar = " ";
my $quiet = 0;
my $width = 80;
my $verbose = 0;
# Process options
#
Getopt::Long::Configure ("ignore_case");
my $result = GetOptions(
"c=s" => \$padchar,
"col|column=i" => \$column,
"h|help|?" => sub { system "perldoc $0"; exit; },
"l|left!" => \$left,
"q|quiet!" => \$quiet,
"v|verbose+" => \$verbose,
"version" => sub {
die "Version of $VERSION_DATE, by Steven J. DeRose.\n";
},
"w|width=i" => \$width
);
($result) || die "pad: Bad options.\n";
# Validate and default options
#
(length($padchar)==1) ||
die "-c (pad character) must specify a single character.\n";
($column >= 0) ||
die "-col (column to insert padding at) must be >= 0.\n";
###############################################################################
# Main
#
while (my $line = <>) {
chomp($line);
my $needed = $width - length($line);
my $pad = ($needed > 0) ? ($padchar x $needed) : "";
if ($left) {
print "$pad$line\n";
}
elsif ($column > 0 && $column < length($line)) {
print substr($line,0,$column) . $pad . substr($line,$column) . "\n";
}
else {
print "$line$pad\n";
}
}
exit;
###############################################################################
###############################################################################
###############################################################################
#
=pod
=head1 Usage
pad [options]
Adds padding characters to the end of each line, to make a certain length.
=head1 Options
=over
=item * B<-c> I<char>
Set pad character to use (default: space).
=item * B<--column> I<n>
Insert padding at column I<n>.
=item * B<-l>
Pad on left instead of right.
=item * B<--version>
Display version info and exit.
=item * B<-w> I<num>
Set width to pads lines to (default: 80)
=back
=head1 Related commands
C<align>
=head1 Ownership
This work by Steven J. DeRose is licensed under a Creative Commons
Attribution-Share Alike 3.0 Unported License. For further information on
this license, see L<http://creativecommons.org/licenses/by-sa/3.0/>.
For the most recent version, see L<http://www.derose.net/steve/utilities/>.
=cut