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