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