-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcenter
executable file
·55 lines (45 loc) · 991 Bytes
/
center
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
#!/usr/bin/env perl
#==================================================
# center
# File ID: 54673c7e-5d37-11df-bc5d-90e6ba3022ac
# Centers text
#==================================================
use strict;
use warnings;
use Getopt::Std;
our ($opt_w, $opt_h) =
( "", 0);
getopts('hw:');
if ($opt_h) {
print(<<END);
Syntax: $0 [-w width] [file [files ...]]
Centers text from stdin or files. If no width is specified, the longest
line will be used.
END
exit 0;
}
my @Lines = <>;
my $Longest = 0;
if ($opt_w) {
if ($opt_w <= 0) {
die("$opt_w: Invalid line length");
} else {
$Longest = $opt_w;
}
} else {
for (@Lines) {
chomp;
s/^\s*(.*?)\s*$/$1/;
$Longest = length($_) if (length($_) > $Longest);
}
}
for my $Curr (@Lines) {
chomp($Curr);
$Curr =~ s/^\s*(.*?)\s*$/$1/;
my $curr_len = length($Curr);
if ($curr_len < $Longest) {
$Curr = (" " x int(($Longest-$curr_len)/2)) . $Curr;
$Curr .= " " x ($Longest-length($Curr));
}
print("$Curr\n");
}