4 $SIG{__WARN__} = sub { die @_; };
6 # xxx much of the comment below is TODO
10 # .../commitid.scad.pl [OPTION...] [STRING...] >commitid.scad.new \
11 # && mv -f commitid.scad.new commitid.scad
15 # --git generate git commit indications, as shown below
16 # (this is the default if no strings are requested with -t)
19 # generate git commit indication based on commit object only
20 # (ie avoid counting commits)
22 # -i do not generate `+' if git-untracked files are present
25 # generate a form FORM containing TEXT
26 # TEXT can contain newlines (final newline usually undesirable)
27 # if FORM not specified, generates Arg0 Arg1 Arg2 in sequence
28 # character set is SPC 0-9 a-f + *
30 # We generate a physical indication of which commit was used.
32 # We provide for scaling factors with dynamic variables:
33 # $Commitid_pixelsz if not set, we use 0.8 } multiplied
34 # $Commitid_scale if not set, we use 1.0 } together
35 # $Commitid_depth if not set, we use xy pixel size from above / 2
36 # $Commitid_depth_scale if not set, we use 1.0 (multiplies depth above)
37 # $Commitid_max_best_scale if not set, we use 2.0 (max mult of xy in Best)
39 # We have auto-sizing modules:
41 # module Commitid_BestCount_2D(max_sz, margin=Commitid_pixelsz()) { ... }
42 # module Commitid_BestCount (max_sz, margin=Commitid_pixelsz()) { ... }
43 # module Commitid_BestObjid_2D(max_sz, margin=Commitid_pixelsz()) { ... }
44 # module Commitid_BestObjid (max_sz, margin=Commitid_pixelsz()) { ... }
45 # // max_sz should be [x,y]
47 # For each form we have
49 # module Commitid_Form_2D() { ... }
50 # module Commitid_Form() { ... }
51 # function Commitid_Form_sz() => [ x, y ]
53 # These have their origin in the bottom left corner. The 3D model
54 # is a positive, has its origin halfway through, and is twice the
55 # depth in height, so it can be added or subtracted.
59 # function Commitid_pixelsz() // $Commitid_pixelsz * $Commitid_scale
60 # function Commitid_depth() // see above
62 # We can generate these forms:
65 # if tree is dirty, * is suffixed or prefixed to count or commitid
66 # if tree has untracked files, + is added
67 # (where it is added depends on the Form; in any case it does not
68 # change the size, but steals space from digits)
70 # Small2 Small3 ... Small10:
71 # git rev-list --first-parent --count HEAD
72 # typically 3-4 characters but we allow for up to 6
73 # padded with zeroes; if too long we reduce mod 10^n
74 # eg if the count is 123456
76 # Small8 _*123456 (where _ are spaces)
77 # the git objectid is included if it will fit
78 # unambiguously and usefully eg
81 # Small2S Small4S ... Small10S:
83 # same but split into two lines (S) or three lines (T) eg
84 # Small4S 45 Small6T _3
88 # Git2 Git3 ... Git10:
89 # Git4S Git6S ... Git10S:
91 # git-rev-parse HEAD (prefix of requested length)
92 # eg if the commitid is abcdef0123...
95 # Full4 Full6 ... Full20:
96 # git-rev-list --first-parent --count HEAD
100 # Full6 abcdef Full8 abcdef01
103 # Full6T Full9T ... Full30T
104 # as Full but commit is split over two lines
105 # for a 3-line message; eg
113 # Strings passed on command line
115 sub p { print @_ or die $!; }
118 // *** AUTOGENERATED - DO NOT EDIT *** //
119 function Commitid_pixelsz() =
120 ($Commitid_pixelsz ? $Commitid_pixelsz : 0.8) *
121 ($Commitid_scale ? $Commitid_scale : 1.0);
122 function Commitid_depth() =
123 ($Commitid_depth ? $Commitid_depth : Commitid_pixelsz()/2) *
124 ($Commitid_depth_scale ? $Commitid_depth_scale : 1.0);
125 function Commitid__scale() =
126 Commitid_pixelsz() / 0.2;
131 my $chrx = sprintf '%#x', ord $chr;
132 return "Commitid__chr_$chrx";
135 our $gtm_demo_i = -1;
139 sub gentextmodule_demo_start_batch () {
144 sub gen3dmodule ($@) {
145 my ($modb,@argl) = (@_);
146 p "module ${modb}(".(join ', ', @argl)."){\n";
147 p " d=Commitid_depth();\n";
148 p " translate([0,0,-d]) linear_extrude(height=d*2)\n";
149 p " ${modb}_2D(".(join ',', map { m/=/ ? $` : $_ } @argl).");\n";
153 sub gentextmodule ($@) {
154 my ($form, @lines) = @_;
155 my $modb = "Commitid_$form";
156 p "module ${modb}_2D(){\n";
157 p " // |$_|\n" foreach @lines;
158 p " scale(Commitid__scale()){\n";
161 foreach my $line (@lines) {
164 foreach my $chr (split //, $line) {
165 p sprintf " translate([%d * 0.8, %d * 1.2]) %s();\n",
166 $x, $y, chrmodname $chr
170 $cols = $x if $x > $cols;
176 p sprintf "function %s_sz() = Commitid__scale() * 0.1 * [ %d, %d ];\n",
177 $modb, 2 * ($cols * 4 - 1), 2 * (@lines * 6 - 1);
179 push @gtm_demo_o, <<END;
180 translate([$gtm_demo_i * st[0], $gtm_demo_j * st[1]]) {
182 color("blue") translate([-e,-e]) square(${modb}_sz() + 2*[e,e]);
183 square(${modb}_sz());
197 last if %cellmap && !m/\S/;
207 my @chrs = split / /, $_;
208 <DATA> !~ m/\S/ or die;
209 foreach my $row (reverse 0..4) {
220 foreach my $chr (@chrs) {
221 s{^ }{} or die "$chr $_ ?";
222 foreach my $col (0..2) {
226 my $f = $cellmap{$&};
228 $f =~ s/\b\d/ sprintf '%05d', $col*2000 + $&*1025 /ge;
229 $f =~ s/\d\b/ sprintf '%05d', $row*2000 + $&*1025 /ge;
230 push @{ $chrpolys{$chr} }, [ split / /, $f ];
242 foreach my $chr (sort keys %chrpolys) {
243 my $mod = chrmodname $chr;
244 p "module $mod () {\n";
245 foreach my $poly (@{ $chrpolys{$chr} }) {
248 foreach my $pt (@$poly) {
250 $pt =~ s{\d{5}}{$&,};
251 $pt =~ s{\b\d}{$&.}g;
260 @demo = reverse $demo =~ m{.{1,$democols}}go;
265 our $do_git; # contains may chars 'c' (count) and/or 'o' (object)
266 our $do_git_untracked = 1;
271 sub rjustt ($$;$) { # right justify and truncate (ie, pad and truncate at left)
272 # always includes prefix
273 my ($sz, $whole, $prefix) = @_;
275 my $lw = length $whole;
276 my $spare = $sz - $lw - (length $prefix);
278 ($spare > 0 ? (' ' x $spare) : '').
280 substr($whole, ($spare < 0 ? -$spare : 0));
284 my ($sz, $whole, $suffix) = @_;
286 $sz -= length $suffix;
287 return sprintf "%-${sz}.${sz}s%s", $whole, $suffix;
291 my ($form, @lines) = @_;
292 gentextmodule($form, @lines);
295 Chars => (length join '', @lines),
296 Lines => (scalar @lines),
297 Ambiguous => ($form =~ m/Full/ && !grep { m/\W/ } @lines),
302 sub genform_q ($$$) {
303 my ($form, $s, $lines) = @_;
306 return if $l % $lines;
310 genform($form, $s =~ m/.{$e}/g);
313 sub genform_plusq ($$) {
316 genform_q("${form}S", $s, 2);
317 genform_q("${form}T", $s, 3);
322 sub gitrun_start () {
323 open F, "-|", @gcmd or die "$gcmd[0]: start: $!";
326 sub gitrun_done (;$) {
332 die "@gcmd failed ($?)\n";
336 (@gcmd) = (qw(git), @_);
340 defined or die "@gcmd produced no output";
341 chomp or die "@gcmd produced no final newline";
346 return unless $do_git;
348 @gcmd = qw(git status --porcelain);
349 push @gcmd, qw(--untracked=no) unless $do_git_untracked;
354 if (m/^\?\?/ && $do_git_untracked) {
361 gitrun_done($git_dirty eq '*');
366 if ($do_git =~ m/c/) {
367 $git_count = gitoutput qw(rev-list --first-parent --count HEAD);
369 if ($do_git =~ m/o/) {
370 $git_object = gitoutput qw(rev-parse HEAD);
373 foreach my $sz (2..10) {
374 gentextmodule_demo_start_batch();
376 if (defined($git_count)) {
377 my $smallstr = rjustt($sz, $git_count, $git_dirty);
378 if (defined($git_object) && $sz >= length($git_count) + 3) {
379 $smallstr = $git_object;
380 $smallstr .= ($git_dirty || ' ');
381 $smallstr .= $git_count;
382 $smallstr = rjustt($sz, $smallstr);
384 genform_plusq("Small$sz", $smallstr);
387 genform_plusq("Git$sz", ljustt($sz, $git_object, $git_dirty))
388 if defined $git_object;
390 if (defined $git_count && defined $git_object) {
391 genform("Full".($sz*2),
392 ljustt($sz, $git_object),
393 rjustt($sz, $git_count, $git_dirty));
396 genform("Full".($e*3)."T",
397 ljustt($e*2, $git_object, $git_dirty)
399 rjustt($e, $git_count));
404 sub do_some_best ($$) {
405 my ($modname, $formre) = @_;
406 my $fullmodname = "Commitid_${modname}_2D";
407 my @argl = qw(max_sz margin=Commitid_pixelsz());
408 p "module $fullmodname(".(join ', ', @argl).") {\n";
409 my $mbs = '$Commitid_max_best_scale';
410 p " sc_max = $mbs ? $mbs : 2;\n";
411 p " sz = max_sz - 2*[margin,margin];\n";
415 $b->{Chars} <=> $a->{Chars} or
416 $a->{Lines} <=> $b->{Chars}
419 $_->{Form} =~ m/$formre/ &&
424 my $form = $f->{Form};
425 p " sz_$form = Commitid_${form}_sz();\n";
426 foreach my $rot (qw(0 1)) {
427 my $id = "${form}_r${rot}";
428 p " sc_$id = min(sc_max";
429 foreach my $xy (qw(0 1)) {
430 p ",sz[$xy]/sz_$form","[",(($xy xor $rot)+0),"]";
433 push @do, " if (sc_$id >= 1.0";
434 push @do, " && sc_$id >= sc_${form}_r1" if !$rot;
436 push @do, " translate([margin,margin]) scale(sc_$id)\n";
437 push @do, " rotate(90) translate([0,-sz_$form"."[1]])\n" if $rot;
438 push @do, " Commitid_${form}_2D();\n";
444 echo("$fullmodname could not fit anything in", max_sz);
450 gen3dmodule "Commitid_$modname", @argl;
454 return unless $do_git;
456 # Auto-computer for `best fit'
458 # We have two best fit approaches: with count, and git-object-id-only
460 # For `with count', we only ever include the git object id if the
461 # result would be unambigous. That means that at least one space
462 # or punctuation was generated.
464 # We sort the options by firstly number of characters
465 # (decreasing), and then by number of lines (increasing) and
466 # try each one both ways round.
468 do_some_best('BestCount', 'Small|Full') if $do_git =~ m/c/;
469 do_some_best('BestObjid', 'Git|Full') if $do_git =~ m/o/;
474 if (m/^--(no)?-git$/) {
475 $do_git = $1 ? '' : 'co';
476 } elsif (m/^---git=objid$/i) {
479 $do_git_untracked = 0;
480 } elsif (m/^-t(.*)$/) {
482 die "bad usage: -t needs string argument\n";
484 gentextmodule($form, split /\n/, $_);
487 gentextmodule("Arg$argcounter", $_);
490 die "bad usage: unknown option \`$_'\n";
494 $do_git //= defined($argcounter) ? '' : 'co';
496 gentextmodule_demo_start_batch();
497 gentextmodule('FontDemo', @demo);
502 p "module Commitid_2DDemo(){\n";
503 p " st = Commitid__scale() * [ 10, 5 ];\n";
504 p " e = Commitid_pixelsz();\n";
505 p $_ foreach @gtm_demo_o;
508 flush STDOUT or die $!;
509 close STDOUT or die $!;
523 /#\ r /#\ ##\ # # ### // ### /#\ /#\
524 # # /# # # # # # # # # # # #
525 # # # /#/ ##< \## ##\ ##\ // >#< \##
526 # # # # # # # # # # # # #
527 \#/ /#\ ### ##/ # ##/ \#/ # \#/ ##/
533 /## ##\ # /## #r# ###
535 \## ##/ \## \## \#/ #