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)
38 # For each form we have
40 # module Commitid_Form_2D() { ... }
41 # module Commitid_Form() { ... }
42 # function Commitid_Form_sz() => [ x, y ]
44 # These have their origin in the bottom left corner. The 3D model
45 # is a positive, has its origin halfway through, and is twice the
46 # depth in height, so it can be added or subtracted.
50 # function Commitid_pixelsz() // $Commitid_pixelsz * $Commitid_scale
51 # function Commitid_depth() // see above
53 # We can generate these forms:
56 # if tree is dirty, * is suffixed or prefixed to count or commitid
57 # if tree has untracked files, + is added
58 # (where it is added depends on the Form; in any case it does not
59 # change the size, but steals space from digits)
61 # Small2 Small3 ... Small10:
62 # git rev-list --first-parent --count HEAD
63 # typically 3-4 characters but we allow for up to 6
64 # padded with zeroes; if too long we reduce mod 10^n
65 # eg if the count is 123456
67 # Small8 __123456 (where _ are spaces)
69 # Small2S Small4S ... Small10S:
71 # same but split into two lines (S) or three lines (T) eg
72 # Small4S 45 Small6T _3
76 # Git2 Git3 ... Git10:
77 # Git4S Git6S ... Git10S:
79 # git-rev-parse HEAD (prefix of requested length)
80 # eg if the commitid is abcdef0123...
83 # Full4 Full6 ... Full20:
84 # git-rev-list --first-parent --count HEAD
88 # Full6 abcdef Full8 abcdef01
91 # Full6T Full9T ... Full30T
92 # as Full but commit is split over two lines
93 # for a 3-line message; eg
101 # Strings passed on command line
103 sub p { print @_ or die $!; }
106 // *** AUTOGENERATED - DO NOT EDIT *** //
107 function Commitid_pixelsz() =
108 ($Commitid_pixelsz ? $Commitid_pixelsz : 0.8) *
109 ($Commitid_scale ? $Commitid_scale : 1.0);
110 function Commitid_depth() =
111 ($Commitid_depth ? $Commitid_depth : Commitid_pixelsz()/2) *
112 ($Commitid_depth_scale ? $Commitid_depth_scale : 1.0);
113 function Commitid__scale() =
114 Commitid_pixelsz() / 0.2;
119 my $chrx = sprintf '%#x', ord $chr;
120 return "Commitid__chr_$chrx";
123 our $gtm_demo_i = -1;
127 sub gentextmodule_demo_start_batch () {
132 sub gentextmodule ($@) {
133 my ($form, @lines) = @_;
134 my $modb = "Commitid_$form";
135 p "module ${modb}_2D(){\n";
136 p " // |$_|\n" foreach @lines;
137 p " scale(Commitid__scale()){\n";
140 foreach my $line (@lines) {
143 foreach my $chr (split //, $line) {
144 p sprintf " translate([%d * 0.8, %d * 1.2]) %s();\n",
145 $x, $y, chrmodname $chr
149 $cols = $x if $x > $cols;
153 p "module ${modb}(){\n";
154 p " d=Commitid_depth();\n";
155 p " translate([0,0,-d]) linear_extrude(height=d*2) ${modb}_2D();\n";
158 p sprintf "function %s_sz() = Commitid__scale() * 0.1 * [ %d, %d ];\n",
159 $modb, 2 * ($cols * 4 - 1), 2 * (@lines * 6 - 1);
161 push @gtm_demo_o, <<END;
162 translate([$gtm_demo_i * st[0], $gtm_demo_j * st[1]]) {
164 color("blue") translate([-e,-e]) square(${modb}_sz() + 2*[e,e]);
165 square(${modb}_sz());
179 last if %cellmap && !m/\S/;
189 my @chrs = split / /, $_;
190 <DATA> !~ m/\S/ or die;
191 foreach my $row (reverse 0..4) {
202 foreach my $chr (@chrs) {
203 s{^ }{} or die "$chr $_ ?";
204 foreach my $col (0..2) {
208 my $f = $cellmap{$&};
210 $f =~ s/\b\d/ sprintf '%05d', $col*2000 + $&*1025 /ge;
211 $f =~ s/\d\b/ sprintf '%05d', $row*2000 + $&*1025 /ge;
212 push @{ $chrpolys{$chr} }, [ split / /, $f ];
224 foreach my $chr (sort keys %chrpolys) {
225 my $mod = chrmodname $chr;
226 p "module $mod () {\n";
227 foreach my $poly (@{ $chrpolys{$chr} }) {
230 foreach my $pt (@$poly) {
232 $pt =~ s{\d{5}}{$&,};
233 $pt =~ s{\b\d}{$&.}g;
242 @demo = reverse $demo =~ m{.{1,$democols}}go;
247 our $do_git; # contains may chars 'c' (count) and/or 'o' (object)
248 our $do_git_untracked = 1;
251 sub rjustt ($$;$) { # right justify and truncate (ie, pad and truncate at left)
252 # always includes prefix
253 my ($sz, $whole, $prefix) = @_;
255 my $lw = length $whole;
256 my $spare = $sz - $lw - (length $prefix);
258 ($spare > 0 ? (' ' x $spare) : '').
260 substr($whole, ($spare < 0 ? -$spare : 0));
264 my ($sz, $whole, $suffix) = @_;
266 $sz -= length $suffix;
267 return sprintf "%-${sz}.${sz}s%s", $whole, $suffix;
271 my ($form, @lines) = @_;
272 gentextmodule($form, @lines);
275 sub genform_q ($$$) {
276 my ($form, $s, $lines) = @_;
279 return if $l % $lines;
283 genform($form, $s =~ m/.{$e}/g);
286 sub genform_plusq ($$) {
289 genform_q("${form}S", $s, 2);
290 genform_q("${form}T", $s, 3);
295 sub gitrun_start () {
296 open F, "-|", @gcmd or die "$gcmd[0]: start: $!";
299 sub gitrun_done (;$) {
305 die "@gcmd failed ($?)\n";
309 (@gcmd) = (qw(git), @_);
313 defined or die "@gcmd produced no output";
314 chomp or die "@gcmd produced no final newline";
319 return unless $do_git;
321 @gcmd = qw(git status --porcelain);
322 push @gcmd, qw(--untracked=no) unless $do_git_untracked;
327 if (m/^\?\?/ && $do_git_untracked) {
334 gitrun_done($git_dirty eq '*');
339 if ($do_git =~ m/c/) {
340 $git_count = gitoutput qw(rev-list --first-parent --count HEAD);
342 if ($do_git =~ m/o/) {
343 $git_object = gitoutput qw(rev-parse HEAD);
346 foreach my $sz (2..10) {
347 gentextmodule_demo_start_batch();
349 genform_plusq("Small$sz", rjustt($sz, $git_count, $git_dirty))
350 if defined $git_count;
352 genform_plusq("Git$sz", ljustt($sz, $git_object, $git_dirty))
353 if defined $git_object;
355 if (defined $git_count && defined $git_object) {
356 genform("Full".($sz*2),
357 ljustt($sz, $git_object),
358 rjustt($sz, $git_count, $git_dirty));
361 genform("Full".($e*3)."T",
362 ljustt($e*2, $git_object, $git_dirty)
364 rjustt($e, $git_count));
371 if (m/^--(no)?-git$/) {
372 $do_git = $1 ? '' : 'co';
373 } elsif (m/^---git=object$/) {
376 $do_git_untracked = 0;
377 } elsif (m/^-t(.*)$/) {
379 die "bad usage: -t needs string argument\n";
381 gentextmodule($form, split /\n/, $_);
384 gentextmodule("Arg$argcounter", $_);
387 die "bad usage: unknown option \`$_'\n";
391 $do_git //= defined($argcounter) ? '' : 'co';
393 gentextmodule_demo_start_batch();
394 gentextmodule('FontDemo', @demo);
398 p "module Commitid_2DDemo(){\n";
399 p " st = Commitid__scale() * [ 10, 5 ];\n";
400 p " e = Commitid_pixelsz();\n";
401 p $_ foreach @gtm_demo_o;
404 flush STDOUT or die $!;
405 close STDOUT or die $!;
419 /#\ r /#\ ##\ # # ### // ### /#\ /#\
420 # # /# # # # # # # # # # # #
421 # # # /#/ ##< \## ##\ ##\ // >#< \##
422 # # # # # # # # # # # # #
423 \#/ /#\ ### ##/ # ##/ \#/ # \#/ ##/
429 /## ##\ # /## #r# ###
431 \## ##/ \## \## \#/ #