public inbox for gentoo-perl@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-perl] g-cpan patch
@ 2006-09-09 23:19 dams
  2006-09-11  9:34 ` Christian Hartmann
  2006-09-11 16:47 ` Chris White
  0 siblings, 2 replies; 4+ messages in thread
From: dams @ 2006-09-09 23:19 UTC (permalink / raw
  To: gentoo-perl

[-- Attachment #1: Type: text/plain, Size: 374 bytes --]

Hi

While looking at the g-cpan code, I have coded some stuff, here is the patch. Remarks welcome. If you are pleased with it, I'll commit it.

Features/changes :

- recoded the small options verification
- implemented a Fake CPAN::Frontend, to catch the output of the CPAN search
- added a small spin animation to the cpan search
- wrote the cpan search result output

dams

[-- Attachment #2: g-cpan_patch.diff --]
[-- Type: application/octet-stream, Size: 3990 bytes --]

Index: bin/g-cpan
===================================================================
--- bin/g-cpan	(revision 98)
+++ bin/g-cpan	(working copy)
@@ -44,9 +44,13 @@
 use Getopt::Long;
 Getopt::Long::Configure("bundling");
 
-# Init all options (has to be done to perform the 'sum test' later)
-my ($debug, $verbose, $search, $install, $upgrade, $generate, $list, $pretend, $ask, $cpan_reload, $buildpkg, $buildpkgonly, $log) =
-  (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
+# Declare options
+# First, the main switches
+my @main_switches = \my ($search, $list, $install, $generate, $ask, $buildpkg, $buildpkgonly);
+# Then, additional switches
+my @additional_switches = \my ($upgrade, $pretend);
+# Then, the normal options
+my ($debug, $verbose, $cpan_reload, $log);
 
 # Set colors here so we can use them at will anywhere :)
 my $green = color("bold green");
@@ -118,20 +122,20 @@
 # Array that will contain the portage friendly version of the values passed to install
 my %really_install;
 
-# Output error if more than one switch is activated
+# Output error if more than one main switch is activated
 #
-if ($search + $list + $install + $generate + $ask + $buildpkg + $buildpkgonly > 1)
+
+if ( (grep { defined $$_ } @main_switches) > 1)
 {
     print_err($prog, "You can't combine actions with each other.\n");
     print_out("${white}Please consult ${cyan}$prog ${green}--help${reset} or ${cyan}man $prog${reset} for more information\n\n");
     exit();
 }
 
-if ($search + $list + $install + $generate + $upgrade + $pretend + $ask + $buildpkg + $buildpkgonly == 0)
+if ( ! grep { defined $$_ } @main_switches, @additional_switches )
 {
     print_err($prog, "You haven't told $prog what to do.\n");
     print_out("${white}Please consult ${cyan}$prog ${green}--help${reset} or ${cyan}man $prog${reset} for more information\n\n");
-
     exit();
 }
 
@@ -306,8 +310,7 @@
 # Set portage_categories to our defined list of perl_dirs
 $gcpan_run->{portage_categories} = \@perl_dirs;
 
-# Taking care of Searches. This has to be improved a lot, since it uses a call to
-# CPAN Shell to do the job, thus making it impossible to have a clean output..
+# Taking care of Searches.
 if ($search)
 {
     foreach my $expr (@ARGV)
@@ -324,17 +327,51 @@
         {
             print_info($prog, "No ebuild exists, pulling up CPAN listings for $expr");
 
-        }
-
         # Assume they gave us module-name instead of module::name
         # which is bad, because CPAN can't convert it ;p
 
-        print_ok($prog, "Searching for $expr on CPAN") if ($verbose);
-        unless (CPAN::Shell->i("/$expr/"))
-        {
-            $expr =~ s/-/::/g;
+            print_ok($prog, "Searching for $expr on CPAN") if ($verbose);
+
+	    my @search_results;
+
+            # Let's define a CPAN::Frontend to use our printing methods
+	    {
+	        package Gentoo::CPAN::FakeFrontend;
+                my $spin = 0;
+		sub myprint {
+		    my ($self, $text) = @_;
+		    print "\r".('/', '-', '\\', '|')[$spin++%4];
+		    # if there is only one result, the string is different
+		    $text =~ /Module id = (.*)/ and push(@search_results, $1), return;
+		    # in the case of multiple result
+		    if ($text =~ /Module\s+(\S+)/) {
+		        foreach (split(/\n/, $text)) {
+		            $_ =~ /Module\s+(\S+)/ or next;
+			    push @search_results, $1;
+		        }
+		    }
+		}
+	    }
+		    
+	    $CPAN::Frontend = "Gentoo::CPAN::FakeFrontend";
+		
             CPAN::Shell->i("/$expr/");
-        }
+            if (!@search_results) {
+                $expr =~ s/-/::/g;
+                CPAN::Shell->i("/$expr/");
+            }
+
+            # remove the spin
+            print "\r \r";
+
+	    # now, @search_results should contain the matching modules strings, if any
+            if (@search_results) {
+	        print_info('g-cpan', @search_results . " results found : @search_results");
+	    } else {
+	        print_warn('no result found.');
+	    }
+
+	}
     }
 
     exit;

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-09-12 19:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-09-09 23:19 [gentoo-perl] g-cpan patch dams
2006-09-11  9:34 ` Christian Hartmann
2006-09-12 19:10   ` dams
2006-09-11 16:47 ` Chris White

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox