chiark / gitweb /
multiple sort specs
authorian <ian>
Wed, 14 Apr 2004 19:47:15 +0000 (19:47 +0000)
committerian <ian>
Wed, 14 Apr 2004 19:47:15 +0000 (19:47 +0000)
farnell/farnell-find

index 629d5f9061cb0dd0347f7a1e6942223177e39c56..779aea100a1c4b3f2a81f4c2b457e0e3f08ba12a 100755 (executable)
@@ -1,13 +1,11 @@
 #!/usr/bin/perl -w
 
-# FIXME
-#  this program is broken
-#  run it on farnell-1 and you'll see
-#  it seems very confused, particularly about defined parts
-
 # Input format: lines, #-comments, blank lines ignored.
 # Indent level is relevant, but only 0, 1, >1 relevant.
 #
+# Usages:
+#   TBD
+#
 #   <Circuit Name>
 #        Starts a circuit.
 #        Circuit names must start with an uppercase letter.
@@ -84,8 +82,8 @@ our(%show);
 # $show{$item}{Info}      )  strings ready to print
 # $show{$item}{Price}     calculated value
 
-our($sorthow);
-# desc or price
+our(@sorthow);
+# list of desc price avail
 
 our($partfrac_unique)= 'aaaa'; # increments
 
@@ -196,17 +194,24 @@ sub read_spec () {
 
 sub itemsortmap ($) {
     my ($item) = @_;
-    my ($z, $bi, $title);
-    if ($sorthow eq 'price') {
-       $z= sprintf "%030.20f ", 1.0 / ($show{$item}{Price} + 0.0001);
-    } else {
-       $z= '';
-    }
+    my ($o, $bi, $how, $title);
+    $o= '';
     $bi= $iteminfo{$item};
-    return "? $z $item" if !defined $bi;
-    $title= $bi->{Description};
-    $title .= "\n".$1 if $title =~ s/^(\[[^][]+\]) //;
-    return "$z $title";
+    if (!defined $bi) { return "? $item\n"; }
+    foreach $how (@sorthow) {
+       if ($how eq 'price') {
+           $o .= sprintf "%030.20f ", 1.0 / ($show{$item}{Price} + 0.0001);
+       } elsif ($how eq 'desc') {
+           $title= $bi->{Description};
+           $title .= "\n".$1 if $title =~ s/^(\[[^][]+\]) //;
+           $o .= "$title\n";
+       } elsif ($how eq 'avail') {
+           $o .= $bi->{Avail};
+       } else {
+           die "unknown sort option \`$o'\n";
+       }
+    }
+    return $o;
 }
 
 sub analyse_spec () {
@@ -283,8 +288,8 @@ sub analyse_spec () {
        $avail{$avail}= 1;
        $avail= ' '.$stockmap{$avail} if exists $stockmap{$avail};
        $show{$item}{Head}=
-           sprintf("%5d%1s %-11s %-${desclen}.${desclen}s".
-                   " %6.3f %6.2f%1s %s\n",
+           sprintf("%4d%1s %-10s %-${desclen}.${desclen}s".
+                   " %7.3f %7.2f%1s %s\n",
                    $toorder, $notechar, $item, $bi->{Description},
                    $bi->{Price}, $price, $notechar, $avail);
        $show{$item}{Info}= '';
@@ -293,16 +298,17 @@ sub analyse_spec () {
        foreach $desc (@{ $itemdesc{$item} }) {
            $show{$item}{Info} .=
                sprintf("%*s %s\n",
-                       (5+1+1+11),'',
+                       (4+1+1+10),'',
                        $desc);
        }
        foreach $ii (@{ $iteminstances{$item} }) {
            $ql= $ii->{Qty}; $qr= '';
            if ($ql =~ s/\D\d+$//) { $qr= $&; }
            if ($ql =~ s,(\d+)/[a-z]+$,,) { $qr= "$1/"; }
+           if ($ql =~ s/\=//) { $qr .= $&; }
            $show{$item}{Info} .=
                sprintf("%*s %5s%-2s %s\n",
-                       (5+1+5),'',
+                       (4+1+4),'',
                        $ql,$qr,
                        $ii->{Use});
        }
@@ -403,10 +409,11 @@ if (@ARGV==1 && $ARGV[0] =~ m/^\d\d\d\-/) {
     foreach $k (sort keys %$chr) {
        printf "%-20s %s\n", $k, $chr->{$k} or die $!;
     }
-} elsif (@ARGV==1 || @ARGV==2) {
-    open F, "< $ARGV[0]" or die $!;
-    $sorthow= @ARGV>1 ? $ARGV[1] : 'price';
-    die unless $sorthow !~ m/^(?:cost|desc)$/;
+} elsif (@ARGV>=1) {
+    my ($filename);
+    ($filename, @sorthow) = @ARGV;
+    @sorthow= qw(avail price) if !@sorthow;
+    open F, "< $filename" or die $!;
     read_spec();
     close F or die $!;
     analyse_spec();