mdisk: Use color, red for error messages and green for success
[sunny256-utils.git] / center
blob7b60dbba8e1a1b26f9526622e63d778ae3a33597
1 #!/usr/bin/env perl
3 #==================================================
4 # center
5 # File ID: 54673c7e-5d37-11df-bc5d-90e6ba3022ac
6 # Centers text
7 #==================================================
9 use strict;
10 use warnings;
11 use Getopt::Std;
13 our ($opt_w, $opt_h) =
14 ( "", 0);
15 getopts('hw:');
17 if ($opt_h) {
18 print(<<END);
20 Syntax: $0 [-w width] [file [files ...]]
22 Centers text from stdin or files. If no width is specified, the longest
23 line will be used.
25 END
26 exit 0;
29 my @Lines = <>;
30 my $Longest = 0;
32 if ($opt_w) {
33 if ($opt_w <= 0) {
34 die("$opt_w: Invalid line length");
35 } else {
36 $Longest = $opt_w;
38 } else {
39 for (@Lines) {
40 chomp;
41 s/^\s*(.*?)\s*$/$1/;
42 $Longest = length($_) if (length($_) > $Longest);
46 for my $Curr (@Lines) {
47 chomp($Curr);
48 $Curr =~ s/^\s*(.*?)\s*$/$1/;
49 my $curr_len = length($Curr);
50 if ($curr_len < $Longest) {
51 $Curr = (" " x int(($Longest-$curr_len)/2)) . $Curr;
52 $Curr .= " " x ($Longest-length($Curr));
54 print("$Curr\n");