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