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 + * (`*' glyph is `=/=').
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:
63 # git rev-list --first-parent --count HEAD
64 # typically 3-4 characters but we allow for up to 6
65 # padded with zeroes; if too long we reduce mod 10^n
68 # If tree is dirty, + or * is suffixed, reducing number of
75 # same but split into two lines eg
83 # git-rev-parse HEAD (prefix of requested length)
86 # If tree is dirty, + or * is suffixed to commitid,
87 # reducing number of hex digits by 1.
97 # git-rev-list --first-parent --count HEAD
102 # If tree is dirty, + or * is suffixed to count (but not to
103 # commitid) reducing number of digits by 1.
108 # Strings passed on command line
110 sub p { print @_ or die $!; }
113 // *** AUTOGENERATED - DO NOT EDIT *** //
114 function Commitid_pixelsz() =
115 ($Commitid_pixelsz ? $Commitid_pixelsz : 0.8) *
116 ($Commitid_scale ? $Commitid_scale : 1.0);
117 function Commitid_depth() =
118 ($Commitid_depth ? $Commitid_depth : Commitid_pixelsz()/2) *
119 ($Commitid_depth_scale ? $Commitid_depth_scale : 1.0);
120 function Commitid__scale() =
121 Commitid_pixelsz() / 0.2;
126 my $chrx = sprintf '%#x', ord $chr;
127 return "Commitid__chr_$chrx";
130 our $gtm_demo_i = -1;
134 sub gentextmodule_demo_next_batch(){
138 gentextmodule_demo_next_batch();
140 sub gentextmodule ($@) {
141 my ($form, @lines) = @_;
142 my $modb = "Commitid_$form";
143 p "module ${modb}_2D(){\n";
144 p " // |$_|\n" foreach @lines;
145 p " scale(Commitid__scale()){\n";
148 foreach my $line (@lines) {
151 foreach my $chr (split //, $line) {
152 next if $chr !~ m/\S/;
153 p sprintf " translate([%d * 0.8, %d * 1.2]) %s();\n",
154 $x, $y, chrmodname $chr;
157 $cols = $x if $x > $cols;
161 p "module ${modb}(){\n";
162 p " d=Commitid_depth();\n";
163 p " translate([0,0,-d]) linear_extrude(height=d*2) ${modb}_2D();\n";
166 p sprintf "function %s_sz() = Commitid__scale() * 0.1 * [ %d, %d ];\n",
167 $modb, 2 * ($cols * 4 - 1), 2 * (@lines * 6 - 1);
170 " translate([$gtm_demo_i * st[0], $gtm_demo_j * st[1]])".
181 last if %cellmap && !m/\S/;
191 my @chrs = split / /, $_;
192 <DATA> !~ m/\S/ or die;
193 foreach my $row (reverse 0..4) {
204 foreach my $chr (@chrs) {
205 s{^ }{} or die "$chr $_ ?";
206 foreach my $col (0..2) {
210 my $f = $cellmap{$&};
212 $f =~ s/\b\d/ sprintf '%05d', $col*2000 + $&*1025 /ge;
213 $f =~ s/\d\b/ sprintf '%05d', $row*2000 + $&*1025 /ge;
214 push @{ $chrpolys{$chr} }, [ split / /, $f ];
226 foreach my $chr (sort keys %chrpolys) {
227 my $mod = chrmodname $chr;
228 p "module $mod () {\n";
229 foreach my $poly (@{ $chrpolys{$chr} }) {
232 foreach my $pt (@$poly) {
234 $pt =~ s{\d{5}}{$&,};
235 $pt =~ s{\b\d}{$&.}g;
244 @demo = reverse $demo =~ m{.{1,$democols}}go;
249 our $do_git; # contains may chars 'c' (count) and/or 'o' (object)
250 our $do_git_untracked = 1;
253 sub rjustt ($$) { # right justify and truncate (ie, pad and truncate at left)
254 my ($sz, $whole) = @_;
255 my $lw = length $whole;
257 ? substr($whole, $lw-$sz)
258 : sprintf "%${sz}s", $whole;
261 sub ljustt ($$$) { # always includes $suffix
262 my ($sz, $whole, $suffix) = @_;
263 $sz -= length $suffix;
264 return sprintf "%-${sz}.${sz}s%s", $whole, $suffix;
267 sub gentextmodule_plusq ($$) {
270 gentextmodule($form, $s);
271 if (!($l & 1) && $l>=4) {
272 gentextmodule("${form}S", substr($s,0,$l/2), substr($s,$l/2));
278 sub gitrun_start () {
279 open F, "-|", @gcmd or die "$gcmd[0]: start: $!";
282 sub gitrun_done (;$) {
288 die "@gcmd failed ($?)\n";
292 (@gcmd) = (qw(git), @_);
296 defined or die "@gcmd produced no output";
297 chomp or die "@gcmd produced no final newline";
302 return unless $do_git;
304 @gcmd = qw(git status --porcelain);
305 push @gcmd, qw(--untracked=no) unless $do_git_untracked;
310 if (m/^\?\?/ && $do_git_untracked) {
317 gitrun_done($git_dirty eq '*');
322 if ($do_git =~ m/c/) {
323 $git_count = gitoutput qw(rev-list --first-parent --count HEAD);
325 if ($do_git =~ m/o/) {
326 $git_object = gitoutput qw(rev-parse HEAD);
329 foreach my $sz (3..10) {
330 gentextmodule_demo_next_batch();
332 gentextmodule_plusq("Small$sz", rjustt($sz, $git_count.$git_dirty))
333 if defined $git_count;
335 gentextmodule_plusq("Git$sz", ljustt($sz, $git_object, $git_dirty))
336 if defined $git_object;
338 gentextmodule("Full$sz",
339 rjustt($sz, $git_count.$git_dirty),
340 ljustt($sz, $git_object, ''))
341 if defined $git_count && defined $git_object;
347 if (m/^--(no)?-git$/) {
348 $do_git = $1 ? '' : 'co';
349 } elsif (m/^---git=object$/) {
352 $do_git_untracked = 0;
353 } elsif (m/^-t(.*)$/) {
355 die "bad usage: -t needs string argument\n";
357 gentextmodule($form, split /\n/, $_);
360 gentextmodule("Arg$argcounter", $_);
363 die "bad usage: unknown option \`$_'\n";
367 $do_git //= defined($argcounter) ? '' : 'co';
369 gentextmodule('FontDemo', @demo);
373 p "module Commitid_2DDemo(){\n";
374 p " st = Commitid__scale() * [ 10, 5 ];\n";
375 p $_ foreach @gtm_demo_o;
378 flush STDOUT or die $!;
379 close STDOUT or die $!;
393 /#\ r /#\ ##\ # # ### // ### /#\ /#\
394 # # /# # # # # # # # # # # #
395 # # # /#/ ##< \## ##\ ##\ // >#< \##
396 # # # # # # # # # # # # #
397 \#/ /#\ ### ##/ # ##/ \#/ # \#/ ##/
403 /## ##\ # /## #r# ###
405 \## ##/ \## \## \#/ #