chiark / gitweb /
commitid.scad.pl: Wip do_git_best
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 5 Feb 2016 20:22:54 +0000 (20:22 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 5 Feb 2016 20:22:54 +0000 (20:22 +0000)
commitid.scad.pl

index 3c409f5416925e1ba548eaf9f2ba824f5588e991..e51318aaf26ba755d3be754fd6a867f64770898f 100755 (executable)
@@ -248,6 +248,8 @@ our $do_git; # contains may chars 'c' (count) and/or 'o' (object)
 our $do_git_untracked = 1;
 our $argcounter;
 
+our @forms;
+
 sub rjustt ($$;$) { # right justify and truncate (ie, pad and truncate at left)
                     # always includes prefix
     my ($sz, $whole, $prefix) = @_;
@@ -270,6 +272,13 @@ sub ljustt ($$;$) {
 sub genform ($@) {
     my ($form, @lines) = @_;
     gentextmodule($form, @lines);
+    my $f = {
+       Form => $form,
+       Chars => (length join '', @lines),
+       Lines => (scalar @lines),
+       Ambiguous => ($form =~ m/Full/ && !grep { m/\W/ } @lines),
+    };
+    push @forms, $f;
 }
 
 sub genform_q ($$$) {
@@ -364,7 +373,45 @@ sub do_git () {
                    rjustt($e, $git_count));
        }
     }
-}    
+}
+
+sub do_some_best ($$) {
+    my ($modname, $formre) = @_;
+    p "module Commitid_$modname(max_sz) {\n";
+    foreach my $f (
+        sort {
+           $b->{Chars} <=> $a->{Chars} or
+           $a->{Lines} <=> $b->{Chars}
+        }
+        grep {
+           $_->{Form} =~ m/$formre/ &&
+           !$_->{Ambiguous}
+       }
+        @forms
+    ) {
+        p "// $f->{Form}\n";
+    }
+    p "}\n";
+}
+
+sub do_git_best () {
+    return unless $do_git;
+
+    # Auto-computer for `best fit'
+    #
+    # We have two best fit approaches: with count, and git-object-id-only
+    #
+    # For `with count', we only ever include the git object id if the
+    # result would be unambigous.  That means that at least one space
+    # or punctuation was generated.
+    #
+    # We sort the options by firstly number of characters
+    # (decreasing), and then by number of lines (increasing) and
+    # try each one both ways round.
+
+    do_some_best('BestCount', 'Small|Full') if $do_git =~ m/c/;
+    do_some_best('BestObjid', 'Git|Full') if $do_git =~ m/o/;
+}
 
 while (@ARGV) {
     $_ = shift;
@@ -394,6 +441,7 @@ gentextmodule_demo_start_batch();
 gentextmodule('FontDemo', @demo);
 
 do_git();
+do_git_best();
 
 p "module Commitid_2DDemo(){\n";
 p " st = Commitid__scale() * [ 10, 5 ];\n";