#!/usr/bin/env perl

# This program is part of Percona Toolkit: http://www.percona.com/software/
# See "COPYRIGHT, LICENSE, AND WARRANTY" at the end of this file for legal
# notices and disclaimers.

# ###########################################################################
# This is a combination of modules and programs in one -- a runnable module.
# http://www.perl.com/pub/a/2006/07/13/lightning-articles.html?page=last
# Or, look it up in the Camel book on pages 642 and 643 in the 3rd edition.
#
# Check at the end of this package for the call to main() which actually runs
# the program.
# ###########################################################################
package pt_align;

use strict;
use warnings FATAL => 'all';

use List::Util qw( max );

sub main {
   local *ARGV; # In the extremely rare case that this is run as a module,
                # not resetting ARGV (the filehandle) could cause problems.

   @ARGV = @_;  # set global ARGV for this package

   # Read all lines
   my @lines;
   my %word_count;
   while ( <> ) {
      my $line = $_;
      my @words = $line =~ m/(\S+)/g;
      push @lines, \@words;
      $word_count{ scalar @words }++;
   }
   
   # Find max number of words per line
   my @wc = reverse sort { $word_count{$a}<=>$word_count{$b} } keys %word_count;
   my $m_words = $wc[0];
   
   # Filter out non-conformists
   @lines = grep { scalar @$_ == $m_words } @lines;
   die "I need at least 2 lines" unless @lines > 1;
   
   # Find the widths and alignments of each column
   my @fmt;
   foreach my $i ( 0 .. $m_words-1 ) {
      my $m_len = max(map { length($_->[$i]) } @lines);
      my $code  = $lines[1]->[$i] =~ m/[^0-9.-]/
                ? "%-${m_len}s"
                : "%${m_len}s";
      push @fmt, $code;
   }
   my $fmt = join(' ', @fmt) . "\n";
   
   # Print!
   foreach my $l ( @lines ) {
      printf $fmt, @$l;
   }
}

# ############################################################################
# Run the program.
# ############################################################################
if ( !caller ) { exit main(@ARGV); }

1; # Because this is a module as well as a script.

# ############################################################################
# Documentation
# ############################################################################
=pod

=head1 NAME

pt-align - Align output from other tools to columns.

=head1 SYNOPSIS

Usage: pt-align [FILES]

pt-align aligns output from other tools to columns.  If no FILES are specified,
STDIN is read.

If a tool prints the following output,

   DATABASE TABLE   ROWS
   foo      bar      100
   long_db_name table  1
   another  long_name 500

then pt-align reprints the output as,

   DATABASE     TABLE     ROWS
   foo          bar        100
   long_db_name table        1
   another      long_name  500

=head1 RISKS

The following section is included to inform users about the potential risks,
whether known or unknown, of using this tool.  The two main categories of risks
are those created by the nature of the tool (e.g. read-only tools vs. read-write
tools) and those created by bugs.

pt-align is a read-only tool.  It should be very low-risk.

At the time of this release, we know of no bugs that could cause serious harm
to users.

The authoritative source for updated information is always the online issue
tracking system.  Issues that affect this tool will be marked as such.  You can
see a list of such issues at the following URL:
L<http://www.percona.com/bugs/pt-align>.

See also L<"BUGS"> for more information on filing bugs and getting help.

=head1 DESCRIPTION

pt-align reads lines and splits them into words.  It counts how many
words each line has, and if there is one number that predominates, it assumes
this is the number of words in each line.  Then it discards all lines that
don't have that many words, and looks at the 2nd line that does.  It assumes
this is the first non-header line.  Based on whether each word looks numeric
or not, it decides on column alignment.  Finally, it goes through and decides
how wide each column should be, and then prints them out.

This is useful for things like aligning the output of vmstat or iostat so it
is easier to read.

=head1 OPTIONS

This tool does not have any command-line options.

=head1 ENVIRONMENT

This tool does not use any environment variables.

=head1 SYSTEM REQUIREMENTS

You need Perl, and some core packages that ought to be installed in any
reasonably new version of Perl.

=head1 BUGS

For a list of known bugs, see L<http://www.percona.com/bugs/pt-align>.

Please report bugs at L<https://bugs.launchpad.net/percona-toolkit>.
Include the following information in your bug report:

=over

=item * Complete command-line used to run the tool

=item * Tool L<"--version">

=item * MySQL version of all servers involved

=item * Output from the tool including STDERR

=item * Input files (log/dump/config files, etc.)

=back

If possible, include debugging output by running the tool with C<PTDEBUG>;
see L<"ENVIRONMENT">.

=head1 DOWNLOADING

Visit L<http://www.percona.com/software/percona-toolkit/> to download the
latest release of Percona Toolkit.  Or, get the latest release from the
command line:

   wget percona.com/get/percona-toolkit.tar.gz

   wget percona.com/get/percona-toolkit.rpm

   wget percona.com/get/percona-toolkit.deb

You can also get individual tools from the latest release:

   wget percona.com/get/TOOL

Replace C<TOOL> with the name of any tool.

=head1 AUTHORS

Baron Schwartz, Brian Fraser, and Daniel Nichter

=head1 ABOUT PERCONA TOOLKIT

This tool is part of Percona Toolkit, a collection of advanced command-line
tools developed by Percona for MySQL support and consulting.  Percona Toolkit
was forked from two projects in June, 2011: Maatkit and Aspersa.  Those
projects were created by Baron Schwartz and developed primarily by him and
Daniel Nichter, both of whom are employed by Percona.  Visit
L<http://www.percona.com/software/> for more software developed by Percona.

=head1 COPYRIGHT, LICENSE, AND WARRANTY

This program is copyright 2010-2011 Baron Schwartz, 2011-2012 Percona Inc.
Feedback and improvements are welcome.

THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, version 2; OR the Perl Artistic License.  On UNIX and similar
systems, you can issue `man perlgpl' or `man perlartistic' to read these
licenses.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA  02111-1307  USA.

=head1 VERSION

pt-align 2.1.2

=cut
