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