X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=commitid.scad.pl;h=e51318aaf26ba755d3be754fd6a867f64770898f;hb=f8a450af5183bd37b15ebb2cd85047c45066f474;hp=3c409f5416925e1ba548eaf9f2ba824f5588e991;hpb=d7c014f47126ae74aec94aa004f46b459bb1facd;p=reprap-play.git diff --git a/commitid.scad.pl b/commitid.scad.pl index 3c409f5..e51318a 100755 --- a/commitid.scad.pl +++ b/commitid.scad.pl @@ -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";