#!/usr/local/bin/perl
#
# cpan-release-counts - ascii graph of releases per year
#                       for user, or across all users
#
use 5.006;
use strict;
use warnings;
use CPAN::ReleaseHistory 0.03;
use Getopt::Long;

my $MAX_BAR_SIZE = 50;
my $BAR_CHAR     = '#';
my $iterator     = CPAN::ReleaseHistory->new()->release_iterator(well_formed => 1);
my $USER;
my %by_year;

GetOptions('user|u=s', \$USER,
           'char|c=s', \$BAR_CHAR,
           'width|w=i', \$MAX_BAR_SIZE,
          ) || die "usage: $0 [-user PAUSE-ID] [-char <char>] [-width N]\n";

$USER = uc($USER) if defined($USER);

while (my $release = $iterator->next_release) {
    next if $USER && $release->distinfo->cpanid ne $USER;
    my $year = (gmtime($release->timestamp))[5] + 1900;
    $by_year{$year}++;
}

my $max                      = (sort { $b <=> $a } values %by_year)[0];
my $count_width              = (sort { $b <=> $a } map { length($_) } values %by_year)[0];
my $scale                    = $max < $MAX_BAR_SIZE ? 1 : $max / $MAX_BAR_SIZE;
my ($first_year, $last_year) = (sort keys %by_year)[0,-1];

foreach my $year ($first_year .. $last_year) {
    printf " %d (%${count_width}d) %s\n",
           $year, $by_year{$year}||0, $BAR_CHAR x (($by_year{$year}||0) / $scale);
}

=head1 NAME

cpan-release-counts - display graph of CPAN releases by year for single user or all users

=head1 SYNOPSIS

 cpan-release-counts [--user NEILB] [--char =] [--width 40]

=head1 DESCRIPTION

B<cpan-release-counts> generates a text graph of all CPAN releases by year,
either across all users, or for a specific user.
The most likely usage is to see your personal release history:

 % cpan-release-counts --user RJBS

 2003 (  4)
 2004 (137) #############
 2005 ( 87) ########
 2006 (277) ##########################
 2007 (238) ######################
 2008 (219) #####################
 2009 (382) ####################################
 2010 (379) ####################################
 2011 (195) ##################
 2012 (172) ################
 2013 (519) #################################################
 2014 ( 58) #####

If you don't specify a user, you'll get a graph of all releases each year.
You can also change the character used when drawing the bars,
and the maximum width used for the longest bar:

 % cpan-release-counts --char = --width 30

 1995 (  193)
 1996 (  634)
 1997 ( 1403) =
 1998 ( 2253) ==
 1999 ( 2705) ===
 2000 ( 3475) ====
 2001 ( 5649) ======
 2002 ( 7511) ========
 2003 (10129) ===========
 2004 (11331) =============
 2005 (12410) ==============
 2006 (12773) ==============
 2007 (14536) ================
 2008 (18534) =====================
 2009 (21078) ========================
 2010 (22225) =========================
 2011 (21893) =========================
 2012 (22669) ==========================
 2013 (25768) ==============================
 2014 ( 3502) ====

=head1 SEE ALSO

L<CPAN::ReleaseHistory> is used to get data for all CPAN releases.

=head1 REPOSITORY

L<https://github.com/neilbowers/CPAN-ReleaseHistory>

=head1 AUTHOR

Neil Bowers E<lt>neilb@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Neil Bowers <neilb@cpan.org>.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

