chiark / gitweb /
commitid.scad.pl: make if into a block (nfc)
[reprap-play.git] / commitid.scad.pl
1 #!/usr/bin/perl -w
2 use strict;
3
4 $SIG{__WARN__} = sub { die @_; };
5
6 #  xxx much of the comment below is TODO
7 #
8 # Usage:
9 #
10 #   .../commitid.scad.pl [OPTION...] [STRING...] >commitid.scad.new \
11 #     && mv -f commitid.scad.new commitid.scad
12 #
13 # Options:
14 #
15 #   --git    generate git commit indications, as shown below
16 #            (this is the default if no strings are requested with -t)
17 #
18 #   --git=object
19 #            generate git commit indication based on commit object only
20 #            (ie avoid counting commits)
21 #
22 #   -i       do not generate `+' if git-untracked files are present
23 #
24 #   [-t[FORM]] TEXT
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 + *
29 #
30 # We generate a physical indication of which commit was used.
31 #
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 #
38 # For each form we have
39 #
40 #    module Commitid_Form_2D() { ... }
41 #    module Commitid_Form() { ... }
42 #    function Commitid_Form_sz()     => [ x, y ]
43 #
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.
47 #
48 # And we provide
49 #
50 #   function Commitid_pixelsz()    // $Commitid_pixelsz * $Commitid_scale
51 #   function Commitid_depth()      // see above
52 #
53 # We can generate these forms:
54 #
55 #   Small3:
56 #   Small4:
57 #   Small5:
58 #   Small6:
59 #   Small7:
60 #   Small8:
61 #   Small9:
62 #   Small10:
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
66 #       eg
67 #            Small4    1070
68 #       If tree is dirty, + or * is suffixed, reducing number of
69 #       digits by 1.
70 #
71 #   Small4S:
72 #   Small6S:   Small6T:
73 #   Small8S:   Small9T:
74 #   Small10S:
75 #       same but split into two lines (S) or three lines (T) eg
76 #            Small4S  10     Small6t    1
77 #                     70               07
78 #                                      0*
79 #
80 #   Git4   Git4S
81 #   Git6   Git6S   Git6T
82 #   Git8   Git8S
83 #   Git9           Git9T
84 #   Git10  Git10S
85 #       git-rev-parse HEAD   (prefix of requested length)
86 #       eg
87 #            Git6    82f2a2
88 #       If tree is dirty, + or * is suffixed to commitid,
89 #       reducing number of hex digits by 1.
90
91 #   Full3
92 #   Full4
93 #   Full5
94 #   Full6
95 #   Full7
96 #   Full8
97 #   Full9
98 #   Full10
99 #       git-rev-list --first-parent --count HEAD
100 #       git-rev-parse HEAD
101 #       eg
102 #            Full6      1070
103 #                     82f2a2
104 #       If tree is dirty, + or * is suffixed to count (but not to
105 #       commitid) reducing number of digits by 1.
106 #
107 #   FontDemo
108 #
109 #   Arg0, Arg1, ...
110 #       Strings passed on command line
111
112 sub p { print @_ or die $!; }
113
114 p <<'END';
115 // *** AUTOGENERATED - DO NOT EDIT *** //
116 function Commitid_pixelsz() =
117   ($Commitid_pixelsz       ? $Commitid_pixelsz       : 0.8) *
118   ($Commitid_scale         ? $Commitid_scale         : 1.0);
119 function Commitid_depth() =
120   ($Commitid_depth         ? $Commitid_depth         : Commitid_pixelsz()/2) *
121   ($Commitid_depth_scale   ? $Commitid_depth_scale   : 1.0);
122 function Commitid__scale() =
123   Commitid_pixelsz() / 0.2;
124 END
125
126 sub chrmodname ($) {
127     my ($chr) = @_;
128     my $chrx = sprintf '%#x', ord $chr;
129     return "Commitid__chr_$chrx";
130 }
131
132 our $gtm_demo_i = -1;
133 our $gtm_demo_j;
134 our @gtm_demo_o;
135
136 sub gentextmodule_demo_start_batch ($;$) {
137     ($gtm_demo_i, $gtm_demo_j) = @_;
138     $gtm_demo_j //= 0;
139 }
140
141 sub gentextmodule ($@) {
142     my ($form, @lines) = @_;
143     my $modb = "Commitid_$form";
144     p "module ${modb}_2D(){\n";
145     p " // |$_|\n" foreach @lines;
146     p " scale(Commitid__scale()){\n";
147     my $y = @lines;
148     my $cols = 1;
149     foreach my $line (@lines) {
150         $y--;
151         my $x = 0;
152         foreach my $chr (split //, $line) {
153             p sprintf "  translate([%d * 0.8, %d * 1.2]) %s();\n",
154                 $x, $y, chrmodname $chr
155                 if $chr =~ m/\S/;
156             $x++;
157         }
158         $cols = $x if $x > $cols;
159     }
160     p " }\n";
161     p "}\n";
162     p "module ${modb}(){\n";
163     p " d=Commitid_depth();\n";
164     p " translate([0,0,-d]) linear_extrude(height=d*2) ${modb}_2D();\n";
165     p "}\n";
166
167     p sprintf "function %s_sz() = Commitid__scale() * 0.1 * [ %d, %d ];\n",
168         $modb, 2 * ($cols * 4 - 1), 2 * (@lines * 6 - 1);
169
170     push @gtm_demo_o, <<END;
171  translate([$gtm_demo_i * st[0], $gtm_demo_j * st[1]]) {
172   difference(){
173    color("blue") translate([-e,-e]) square(${modb}_sz() + 2*[e,e]);
174    square(${modb}_sz());
175   }
176   ${modb}_2D();
177 }
178 END
179     $gtm_demo_j++;
180 }
181
182 our @demo;
183
184 sub parsefont () {
185     my %cellmap;
186     for (;;) {
187         $_ = <DATA> // die;
188         last if %cellmap && !m/\S/;
189         next unless m/\S/;
190         chomp;
191         s{^(.) }{};
192         $cellmap{$1} = $_;
193     }
194     my %chrpolys;
195     while (<DATA>) {
196         next unless m/\S/;
197         chomp;
198         my @chrs = split / /, $_;
199         <DATA> !~ m/\S/ or die;
200         foreach my $row (reverse 0..4) {
201             $_ = <DATA>;
202             chomp;
203             s{^}{ };
204             $_ .= ' ' x 8;
205             m{\S/\S} and die;
206             s{/(?=\s)}{L}g;
207             s{/(?=\S)}{r}g;
208             s{\\(?=\s)}{l}g;
209             s{\\(?=\S)}{R}g;
210             p "// $_\n";
211             foreach my $chr (@chrs) {
212                 s{^ }{} or die "$chr $_ ?";
213                 foreach my $col (0..2) {
214                     my @verts;
215                     if (s{^ }{}) {
216                     } elsif (s{^\S}{}) {
217                         my $f = $cellmap{$&};
218                         die unless $f;
219                         $f =~ s/\b\d/ sprintf '%05d', $col*2000 + $&*1025 /ge;
220                         $f =~ s/\d\b/ sprintf '%05d', $row*2000 + $&*1025 /ge;
221                         push @{ $chrpolys{$chr} }, [ split / /, $f ];
222                     } else {
223                         die "$_ ?";
224                     }
225                 }
226             }
227             die "$_ ?" if m{\S};
228         }    
229     }
230
231     my $demo = '';
232     my $democols = 6;
233     foreach my $chr (sort keys %chrpolys) {
234         my $mod = chrmodname $chr;
235         p "module $mod () {\n";
236         foreach my $poly (@{ $chrpolys{$chr} }) {
237             p " polygon([";
238             my $delim = "";
239             foreach my $pt (@$poly) {
240                 p $delim;
241                 $pt =~ s{\d{5}}{$&,};
242                 $pt =~ s{\b\d}{$&.}g;
243                 p "[$pt]";
244                 $delim = ',';
245             }
246             p "]);\n";
247         }
248         p "}\n";
249         $demo .= $chr;
250     }
251     @demo = reverse $demo =~ m{.{1,$democols}}go;
252 }
253
254 parsefont();
255
256 our $do_git; # contains may chars 'c' (count) and/or 'o' (object)
257 our $do_git_untracked = 1;
258 our $argcounter;
259
260 sub rjustt ($$) { # right justify and truncate (ie, pad and truncate at left)
261     my ($sz, $whole) = @_;
262     my $lw = length $whole;
263     return $lw > $sz
264         ? substr($whole, $lw-$sz)
265         : sprintf "%${sz}s", $whole;
266 }
267
268 sub ljustt ($$$) { # always includes $suffix
269     my ($sz, $whole, $suffix) = @_;
270     $sz -= length $suffix;
271     return sprintf "%-${sz}.${sz}s%s", $whole, $suffix;
272 }
273
274 sub gentextmodule_q ($$$) {
275     my ($form, $s, $lines) = @_;
276     $gtm_demo_j++;
277     my $l = length $s;
278     return if $l % $lines;
279     my $e = $l/$lines;
280     return if $e < 2;
281     $gtm_demo_j--;
282     gentextmodule($form, $s =~ m/.{$e}/g);
283 }
284
285 sub gentextmodule_plusq ($$) {
286     my ($form, $s) = @_;
287     gentextmodule($form, $s);
288     gentextmodule_q("${form}S", $s, 2);
289     gentextmodule_q("${form}T", $s, 3);
290 }
291
292 our @gcmd;
293
294 sub gitrun_start () {
295     open F, "-|", @gcmd or die "$gcmd[0]: start: $!";
296 }
297
298 sub gitrun_done (;$) {
299     my ($errok) = @_;
300     $?=0; $!=0;
301     return if close F;
302     return if $errok;
303     die $! if $!;
304     die "@gcmd failed ($?)\n";
305 }
306
307 sub gitoutput (@) {
308     (@gcmd) = (qw(git), @_);
309     gitrun_start;
310     $_ = <F>;
311     gitrun_done;
312     defined or die "@gcmd produced no output";
313     chomp or die "@gcmd produced no final newline";
314     $_;
315 }
316
317 sub do_git () {
318     return unless $do_git;
319
320     @gcmd = qw(git status --porcelain);
321     push @gcmd, qw(--untracked=no) unless $do_git_untracked;
322
323     my $git_dirty = '';
324     gitrun_start;
325     while (<F>) {
326         if (m/^\?\?/ && $do_git_untracked) {
327             $git_dirty = '+';
328             next;
329         }
330         $git_dirty = '*';
331         last;
332     }
333     gitrun_done($git_dirty eq '*');
334
335     my $git_count;
336     my $git_object;
337
338     if ($do_git =~ m/c/) {
339         $git_count = gitoutput qw(rev-list --first-parent --count HEAD);
340     }
341     if ($do_git =~ m/o/) {
342         $git_object = gitoutput qw(rev-parse HEAD);
343     }
344
345     foreach my $sz (3..10) {
346         gentextmodule_demo_start_batch($sz-3);
347
348         gentextmodule_plusq("Small$sz", rjustt($sz, $git_count.$git_dirty))
349             if defined $git_count;
350
351         gentextmodule_plusq("Git$sz", ljustt($sz, $git_object, $git_dirty))
352             if defined $git_object;
353
354         if (defined $git_count && defined $git_object) {
355             gentextmodule("Full$sz",
356                           rjustt($sz, $git_count.$git_dirty),
357                           ljustt($sz, $git_object, ''));
358         }
359     }
360 }    
361
362 while (@ARGV) {
363     $_ = shift;
364     if (m/^--(no)?-git$/) {
365         $do_git = $1 ? '' : 'co';
366     } elsif (m/^---git=object$/) {
367         $do_git = 'o';
368     } elsif (m/^-i$/) {
369         $do_git_untracked = 0;
370     } elsif (m/^-t(.*)$/) {
371         my $form = $1;
372         die "bad usage: -t needs string argument\n";
373         $_ = shift;
374         gentextmodule($form, split /\n/, $_);
375         $argcounter //= 0;
376     } elsif (m/^[^-]/) {
377         gentextmodule("Arg$argcounter", $_);
378         $argcounter++;
379     } else {
380         die "bad usage: unknown option \`$_'\n";
381     }
382 }
383
384 $do_git //= defined($argcounter) ? '' : 'co';
385
386 gentextmodule_demo_start_batch(-1);
387 gentextmodule('FontDemo', @demo);
388
389 do_git();
390
391 p "module Commitid_2DDemo(){\n";
392 p " st = Commitid__scale() * [ 10, 5 ];\n";
393 p " e  = Commitid_pixelsz();\n";
394 p $_ foreach @gtm_demo_o;
395 p "}\n";
396
397 flush STDOUT or die $!;
398 close STDOUT or die $!;
399
400 __DATA__
401
402 # 00 20 22 02
403 l 00 20 02
404 r 00 20 22
405 L 00 22 02
406 R 20 22 02
407 > 00 20 22 02 11
408 < 00 20 11 22 02
409
410 0 1 2 3 4 5 6 7 8 9
411
412 /#\  r  /#\ ##\ # # ### //  ### /#\ /#\
413 # # /#    #   # # # #   #     # # # # #
414 # #  #  /#/ ##< \## ##\ ##\  // >#< \##
415 # #  #  #     #   #   # # #  #  # #   #
416 \#/ /#\ ### ##/   # ##/ \#/  #  \#/ ##/
417
418 a b c d e f
419
420     #         #     /##
421     #   /##   # /#\ #
422 /## ##\ #   /## #r# ###
423 # # # # #   # # #/  #
424 \## ##/ \## \## \#/ #
425
426 + *
427
428     # #
429  #  \#/
430 ### ###
431  #  /#\
432     # #