chiark / gitweb /
83e3afe642e719c3105f119dea7805d2a2cfb8e3
[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 #  In each case:
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)
60 #
61 #   Small3:
62 #   Small4:
63 #   Small5:
64 #   Small6:
65 #   Small7:
66 #   Small8:
67 #   Small9:
68 #   Small10:
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
73 #            Small5    3456*
74 #            Small8    __123456    (where _ are spaces)
75 #
76 #   Small4S:
77 #   Small6S:   Small6T:
78 #   Small8S:   Small9T:
79 #   Small10S:
80 #       same but split into two lines (S) or three lines (T) eg
81 #            Small4S  45     Small6T   _3
82 #                     6*               45
83 #                                      6*
84 #
85 #   Git4   Git4S
86 #   Git6   Git6S   Git6T
87 #   Git8   Git8S
88 #   Git9           Git9T
89 #   Git10  Git10S
90 #       git-rev-parse HEAD   (prefix of requested length)
91 #       eg if the commitid is abcdef0123...
92 #            Git5    abcd*
93
94 #   Full3
95 #   Full4
96 #   Full5
97 #   Full6
98 #   Full7
99 #   Full8
100 #   Full9
101 #   Full10
102 #       git-rev-list --first-parent --count HEAD
103 #       git-rev-parse HEAD
104 #       eg
105 #            Full6    abcdef       Full8     abcdef01
106 #                     23456*                 _123456*
107 #
108 #   Full6T
109 #   Full9T
110 #   Full12T
111 #   Full15T
112 #       as Full but commit is split over two lines
113 #       for a 3-line message; eg
114 #             Full9T    abc
115 #                       de*
116 #                       456
117 #
118 #   FontDemo
119 #
120 #   Arg0, Arg1, ...
121 #       Strings passed on command line
122
123 sub p { print @_ or die $!; }
124
125 p <<'END';
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;
135 END
136
137 sub chrmodname ($) {
138     my ($chr) = @_;
139     my $chrx = sprintf '%#x', ord $chr;
140     return "Commitid__chr_$chrx";
141 }
142
143 our $gtm_demo_i = -1;
144 our $gtm_demo_j;
145 our @gtm_demo_o;
146
147 sub gentextmodule_demo_start_batch ($;$) {
148     ($gtm_demo_i, $gtm_demo_j) = @_;
149     $gtm_demo_j //= 0;
150 }
151
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";
158     my $y = @lines;
159     my $cols = 1;
160     foreach my $line (@lines) {
161         $y--;
162         my $x = 0;
163         foreach my $chr (split //, $line) {
164             p sprintf "  translate([%d * 0.8, %d * 1.2]) %s();\n",
165                 $x, $y, chrmodname $chr
166                 if $chr =~ m/\S/;
167             $x++;
168         }
169         $cols = $x if $x > $cols;
170     }
171     p " }\n";
172     p "}\n";
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";
176     p "}\n";
177
178     p sprintf "function %s_sz() = Commitid__scale() * 0.1 * [ %d, %d ];\n",
179         $modb, 2 * ($cols * 4 - 1), 2 * (@lines * 6 - 1);
180
181     push @gtm_demo_o, <<END;
182  translate([$gtm_demo_i * st[0], $gtm_demo_j * st[1]]) {
183   difference(){
184    color("blue") translate([-e,-e]) square(${modb}_sz() + 2*[e,e]);
185    square(${modb}_sz());
186   }
187   ${modb}_2D();
188 }
189 END
190     $gtm_demo_j++;
191 }
192
193 our @demo;
194
195 sub parsefont () {
196     my %cellmap;
197     for (;;) {
198         $_ = <DATA> // die;
199         last if %cellmap && !m/\S/;
200         next unless m/\S/;
201         chomp;
202         s{^(.) }{};
203         $cellmap{$1} = $_;
204     }
205     my %chrpolys;
206     while (<DATA>) {
207         next unless m/\S/;
208         chomp;
209         my @chrs = split / /, $_;
210         <DATA> !~ m/\S/ or die;
211         foreach my $row (reverse 0..4) {
212             $_ = <DATA>;
213             chomp;
214             s{^}{ };
215             $_ .= ' ' x 8;
216             m{\S/\S} and die;
217             s{/(?=\s)}{L}g;
218             s{/(?=\S)}{r}g;
219             s{\\(?=\s)}{l}g;
220             s{\\(?=\S)}{R}g;
221             p "// $_\n";
222             foreach my $chr (@chrs) {
223                 s{^ }{} or die "$chr $_ ?";
224                 foreach my $col (0..2) {
225                     my @verts;
226                     if (s{^ }{}) {
227                     } elsif (s{^\S}{}) {
228                         my $f = $cellmap{$&};
229                         die unless $f;
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 ];
233                     } else {
234                         die "$_ ?";
235                     }
236                 }
237             }
238             die "$_ ?" if m{\S};
239         }    
240     }
241
242     my $demo = '';
243     my $democols = 6;
244     foreach my $chr (sort keys %chrpolys) {
245         my $mod = chrmodname $chr;
246         p "module $mod () {\n";
247         foreach my $poly (@{ $chrpolys{$chr} }) {
248             p " polygon([";
249             my $delim = "";
250             foreach my $pt (@$poly) {
251                 p $delim;
252                 $pt =~ s{\d{5}}{$&,};
253                 $pt =~ s{\b\d}{$&.}g;
254                 p "[$pt]";
255                 $delim = ',';
256             }
257             p "]);\n";
258         }
259         p "}\n";
260         $demo .= $chr;
261     }
262     @demo = reverse $demo =~ m{.{1,$democols}}go;
263 }
264
265 parsefont();
266
267 our $do_git; # contains may chars 'c' (count) and/or 'o' (object)
268 our $do_git_untracked = 1;
269 our $argcounter;
270
271 sub rjustt ($$;$) { # right justify and truncate (ie, pad and truncate at left)
272                     # always includes prefix
273     my ($sz, $whole, $prefix) = @_;
274     $prefix //= '';
275     my $lw = length $whole;
276     my $spare = $sz - $lw - (length $prefix);
277     return
278         ($spare > 0 ? (' ' x $spare) : '').
279         $prefix.
280         substr($whole, ($spare < 0 ? -$spare : 0));
281 }
282
283 sub ljustt ($$;$) {
284     my ($sz, $whole, $suffix) = @_;
285     $suffix //= '';
286     $sz -= length $suffix;
287     return sprintf "%-${sz}.${sz}s%s", $whole, $suffix;
288 }
289
290 sub gentextmodule_q ($$$) {
291     my ($form, $s, $lines) = @_;
292     $gtm_demo_j++;
293     my $l = length $s;
294     return if $l % $lines;
295     my $e = $l/$lines;
296     return if $e < 2;
297     $gtm_demo_j--;
298     gentextmodule($form, $s =~ m/.{$e}/g);
299 }
300
301 sub gentextmodule_plusq ($$) {
302     my ($form, $s) = @_;
303     gentextmodule($form, $s);
304     gentextmodule_q("${form}S", $s, 2);
305     gentextmodule_q("${form}T", $s, 3);
306 }
307
308 our @gcmd;
309
310 sub gitrun_start () {
311     open F, "-|", @gcmd or die "$gcmd[0]: start: $!";
312 }
313
314 sub gitrun_done (;$) {
315     my ($errok) = @_;
316     $?=0; $!=0;
317     return if close F;
318     return if $errok;
319     die $! if $!;
320     die "@gcmd failed ($?)\n";
321 }
322
323 sub gitoutput (@) {
324     (@gcmd) = (qw(git), @_);
325     gitrun_start;
326     $_ = <F>;
327     gitrun_done;
328     defined or die "@gcmd produced no output";
329     chomp or die "@gcmd produced no final newline";
330     $_;
331 }
332
333 sub do_git () {
334     return unless $do_git;
335
336     @gcmd = qw(git status --porcelain);
337     push @gcmd, qw(--untracked=no) unless $do_git_untracked;
338
339     my $git_dirty = '';
340     gitrun_start;
341     while (<F>) {
342         if (m/^\?\?/ && $do_git_untracked) {
343             $git_dirty = '+';
344             next;
345         }
346         $git_dirty = '*';
347         last;
348     }
349     gitrun_done($git_dirty eq '*');
350
351     my $git_count;
352     my $git_object;
353
354     if ($do_git =~ m/c/) {
355         $git_count = gitoutput qw(rev-list --first-parent --count HEAD);
356     }
357     if ($do_git =~ m/o/) {
358         $git_object = gitoutput qw(rev-parse HEAD);
359     }
360
361     foreach my $sz (3..10) {
362         gentextmodule_demo_start_batch($sz-3);
363
364         gentextmodule_plusq("Small$sz", rjustt($sz, $git_count.$git_dirty))
365             if defined $git_count;
366
367         gentextmodule_plusq("Git$sz", ljustt($sz, $git_object, $git_dirty))
368             if defined $git_object;
369
370         if (defined $git_count && defined $git_object) {
371             gentextmodule("Full$sz",
372                           ljustt($sz, $git_object, ''),
373                           rjustt($sz, $git_count.$git_dirty));
374
375             if (!($sz % 2)) {
376                 my $e = $sz/2;
377                 gentextmodule("Full".($e*3)."T",
378                               ljustt($e*2, $git_object, $git_dirty)
379                               =~ m/.{$e}/g,
380                               rjustt($e, $git_count));
381             }
382         }
383     }
384 }    
385
386 while (@ARGV) {
387     $_ = shift;
388     if (m/^--(no)?-git$/) {
389         $do_git = $1 ? '' : 'co';
390     } elsif (m/^---git=object$/) {
391         $do_git = 'o';
392     } elsif (m/^-i$/) {
393         $do_git_untracked = 0;
394     } elsif (m/^-t(.*)$/) {
395         my $form = $1;
396         die "bad usage: -t needs string argument\n";
397         $_ = shift;
398         gentextmodule($form, split /\n/, $_);
399         $argcounter //= 0;
400     } elsif (m/^[^-]/) {
401         gentextmodule("Arg$argcounter", $_);
402         $argcounter++;
403     } else {
404         die "bad usage: unknown option \`$_'\n";
405     }
406 }
407
408 $do_git //= defined($argcounter) ? '' : 'co';
409
410 gentextmodule_demo_start_batch(-1);
411 gentextmodule('FontDemo', @demo);
412
413 do_git();
414
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;
419 p "}\n";
420
421 flush STDOUT or die $!;
422 close STDOUT or die $!;
423
424 __DATA__
425
426 # 00 20 22 02
427 l 00 20 02
428 r 00 20 22
429 L 00 22 02
430 R 20 22 02
431 > 00 20 22 02 11
432 < 00 20 11 22 02
433
434 0 1 2 3 4 5 6 7 8 9
435
436 /#\  r  /#\ ##\ # # ### //  ### /#\ /#\
437 # # /#    #   # # # #   #     # # # # #
438 # #  #  /#/ ##< \## ##\ ##\  // >#< \##
439 # #  #  #     #   #   # # #  #  # #   #
440 \#/ /#\ ### ##/   # ##/ \#/  #  \#/ ##/
441
442 a b c d e f
443
444     #         #     /##
445     #   /##   # /#\ #
446 /## ##\ #   /## #r# ###
447 # # # # #   # # #/  #
448 \## ##/ \## \## \#/ #
449
450 + *
451
452     # #
453  #  \#/
454 ### ###
455  #  /#\
456     # #