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)
69 # git rev-list --first-parent --count HEAD
70 # typically 3-4 characters but we allow for up to 6
71 # padded with zeroes; if too long we reduce mod 10^n
72 # eg if the count is 123456
74 # Small8 __123456 (where _ are spaces)
80 # same but split into two lines (S) or three lines (T) eg
81 # Small4S 45 Small6T _3
90 # git-rev-parse HEAD (prefix of requested length)
91 # eg if the commitid is abcdef0123...
102 # git-rev-list --first-parent --count HEAD
105 # Full6 abcdef Full8 abcdef01
112 # as Full but commit is split over two lines
113 # for a 3-line message; eg
121 # Strings passed on command line
123 sub p { print @_ or die $!; }
126 // *** AUTOGENERATED - DO NOT EDIT *** //
127 function Commitid_pixelsz() =
128 ($Commitid_pixelsz ? $Commitid_pixelsz : 0.8) *
129 ($Commitid_scale ? $Commitid_scale : 1.0);
130 function Commitid_depth() =
131 ($Commitid_depth ? $Commitid_depth : Commitid_pixelsz()/2) *
132 ($Commitid_depth_scale ? $Commitid_depth_scale : 1.0);
133 function Commitid__scale() =
134 Commitid_pixelsz() / 0.2;
139 my $chrx = sprintf '%#x', ord $chr;
140 return "Commitid__chr_$chrx";
143 our $gtm_demo_i = -1;
147 sub gentextmodule_demo_start_batch ($;$) {
148 ($gtm_demo_i, $gtm_demo_j) = @_;
152 sub gentextmodule ($@) {
153 my ($form, @lines) = @_;
154 my $modb = "Commitid_$form";
155 p "module ${modb}_2D(){\n";
156 p " // |$_|\n" foreach @lines;
157 p " scale(Commitid__scale()){\n";
160 foreach my $line (@lines) {
163 foreach my $chr (split //, $line) {
164 p sprintf " translate([%d * 0.8, %d * 1.2]) %s();\n",
165 $x, $y, chrmodname $chr
169 $cols = $x if $x > $cols;
173 p "module ${modb}(){\n";
174 p " d=Commitid_depth();\n";
175 p " translate([0,0,-d]) linear_extrude(height=d*2) ${modb}_2D();\n";
178 p sprintf "function %s_sz() = Commitid__scale() * 0.1 * [ %d, %d ];\n",
179 $modb, 2 * ($cols * 4 - 1), 2 * (@lines * 6 - 1);
181 push @gtm_demo_o, <<END;
182 translate([$gtm_demo_i * st[0], $gtm_demo_j * st[1]]) {
184 color("blue") translate([-e,-e]) square(${modb}_sz() + 2*[e,e]);
185 square(${modb}_sz());
199 last if %cellmap && !m/\S/;
209 my @chrs = split / /, $_;
210 <DATA> !~ m/\S/ or die;
211 foreach my $row (reverse 0..4) {
222 foreach my $chr (@chrs) {
223 s{^ }{} or die "$chr $_ ?";
224 foreach my $col (0..2) {
228 my $f = $cellmap{$&};
230 $f =~ s/\b\d/ sprintf '%05d', $col*2000 + $&*1025 /ge;
231 $f =~ s/\d\b/ sprintf '%05d', $row*2000 + $&*1025 /ge;
232 push @{ $chrpolys{$chr} }, [ split / /, $f ];
244 foreach my $chr (sort keys %chrpolys) {
245 my $mod = chrmodname $chr;
246 p "module $mod () {\n";
247 foreach my $poly (@{ $chrpolys{$chr} }) {
250 foreach my $pt (@$poly) {
252 $pt =~ s{\d{5}}{$&,};
253 $pt =~ s{\b\d}{$&.}g;
262 @demo = reverse $demo =~ m{.{1,$democols}}go;
267 our $do_git; # contains may chars 'c' (count) and/or 'o' (object)
268 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);
279 ($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;
290 sub gentextmodule_q ($$$) {
291 my ($form, $s, $lines) = @_;
294 return if $l % $lines;
298 gentextmodule($form, $s =~ m/.{$e}/g);
301 sub gentextmodule_plusq ($$) {
303 gentextmodule($form, $s);
304 gentextmodule_q("${form}S", $s, 2);
305 gentextmodule_q("${form}T", $s, 3);
310 sub gitrun_start () {
311 open F, "-|", @gcmd or die "$gcmd[0]: start: $!";
314 sub gitrun_done (;$) {
320 die "@gcmd failed ($?)\n";
324 (@gcmd) = (qw(git), @_);
328 defined or die "@gcmd produced no output";
329 chomp or die "@gcmd produced no final newline";
334 return unless $do_git;
336 @gcmd = qw(git status --porcelain);
337 push @gcmd, qw(--untracked=no) unless $do_git_untracked;
342 if (m/^\?\?/ && $do_git_untracked) {
349 gitrun_done($git_dirty eq '*');
354 if ($do_git =~ m/c/) {
355 $git_count = gitoutput qw(rev-list --first-parent --count HEAD);
357 if ($do_git =~ m/o/) {
358 $git_object = gitoutput qw(rev-parse HEAD);
361 foreach my $sz (3..10) {
362 gentextmodule_demo_start_batch($sz-3);
364 gentextmodule_plusq("Small$sz", rjustt($sz, $git_count.$git_dirty))
365 if defined $git_count;
367 gentextmodule_plusq("Git$sz", ljustt($sz, $git_object, $git_dirty))
368 if defined $git_object;
370 if (defined $git_count && defined $git_object) {
371 gentextmodule("Full$sz",
372 ljustt($sz, $git_object, ''),
373 rjustt($sz, $git_count.$git_dirty));
377 gentextmodule("Full".($e*3)."T",
378 ljustt($e*2, $git_object, $git_dirty)
380 rjustt($e, $git_count));
388 if (m/^--(no)?-git$/) {
389 $do_git = $1 ? '' : 'co';
390 } elsif (m/^---git=object$/) {
393 $do_git_untracked = 0;
394 } elsif (m/^-t(.*)$/) {
396 die "bad usage: -t needs string argument\n";
398 gentextmodule($form, split /\n/, $_);
401 gentextmodule("Arg$argcounter", $_);
404 die "bad usage: unknown option \`$_'\n";
408 $do_git //= defined($argcounter) ? '' : 'co';
410 gentextmodule_demo_start_batch(-1);
411 gentextmodule('FontDemo', @demo);
415 p "module Commitid_2DDemo(){\n";
416 p " st = Commitid__scale() * [ 10, 5 ];\n";
417 p " e = Commitid_pixelsz();\n";
418 p $_ foreach @gtm_demo_o;
421 flush STDOUT or die $!;
422 close STDOUT or die $!;
436 /#\ r /#\ ##\ # # ### // ### /#\ /#\
437 # # /# # # # # # # # # # # #
438 # # # /#/ ##< \## ##\ ##\ // >#< \##
439 # # # # # # # # # # # # #
440 \#/ /#\ ### ##/ # ##/ \#/ # \#/ ##/
446 /## ##\ # /## #r# ###
448 \## ##/ \## \## \#/ #