4 $SIG{__WARN__} = sub { die @_; };
6 # xxx much of the comment below is TODO
10 # .../commitid.scad.pl [OPTION...] [STRING...] >commitid.scad.new \
11 # && mv -f commitid.scad.new commitid.scad
15 # --git generate git commit indications, as shown below
16 # (this is the default if no strings are requested with -t)
19 # generate git commit indication based on commit object only
20 # (ie avoid counting commits)
22 # -i do not generate `+' if git-untracked files are present
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 + * (`*' glyph is `=/=').
30 # We generate a physical indication of which commit was used.
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)
38 # For each form we have
40 # module Commitid_Form_2D() { ... }
41 # module Commitid_Form() { ... }
42 # function Commitid_Form_sz() => [ x, y ]
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.
50 # function Commitid_pixelsz() // $Commitid_pixelsz * $Commitid_scale
51 # function Commitid_depth() // see above
53 # We can generate these forms:
61 # git rev-list --first-parent --count HEAD
62 # typically 3-4 characters but we allow for up to 6
63 # padded with zeroes; if too long we reduce mod 10^n
66 # If tree is dirty, + or * is suffixed, reducing number of
72 # same but in two lines eg
79 # git-rev-parse HEAD (prefix of requested length)
82 # If tree is dirty, + or * is suffixed to commitid,
83 # reducing number of hex digits by 1.
91 # git-rev-list --first-parent --count HEAD
96 # If tree is dirty, + or * is suffixed to count (but not to
97 # commitid) reducing number of digits by 1.
102 # Strings passed on command line
104 sub p { print @_ or die $!; }
107 // *** AUTOGENERATED - DO NOT EDIT *** //
108 function Commitid_pixelsz() =
109 ($Commitid_pixelsz ? $Commitid_pixelsz : 0.8) *
110 ($Commitid_scale ? $Commitid_scale : 1.0);
111 function Commitid_depth() =
112 ($Commitid_depth ? $Commitid_depth : Commitid_pixelsz()/2) *
113 ($Commitid_depth_scale ? $Commitid_depth_scale : 1.0);
114 function Commitid__scale() =
115 Commitid_pixelsz() / 0.2;
120 my $chrx = sprintf '%#x', ord $chr;
121 return "Commitid__chr_$chrx";
124 sub gentextmodule ($@) {
125 my ($form, @lines) = @_;
126 my $modb = "Commitid_$form";
127 p "module ${modb}_2D(){\n";
128 p " // |$_|\n" foreach @lines;
129 p " scale(Commitid__scale()){\n";
132 foreach my $line (@lines) {
135 foreach my $chr (split //, $line) {
136 next if $chr !~ m/\S/;
137 p sprintf " translate([%d * 0.8, %d * 1.2]) %s();\n",
138 $x, $y, chrmodname $chr;
141 $cols = $x if $x > $cols;
145 p "module ${modb}(){\n";
146 p " d=Commitid_depth();\n";
147 p " translate([0,0,-d]) linear_extrude(height=d*2) ${modb}_2D();\n";
149 p sprintf "function %s_sz() = Commitid__scale() * 0.1 * [ %d, %d ];\n",
150 $modb, 2 * ($cols * 4 - 1), 2 * (@lines * 6 - 1);
159 last if %cellmap && !m/\S/;
169 my @chrs = split / /, $_;
170 <DATA> !~ m/\S/ or die;
171 foreach my $row (reverse 0..4) {
182 foreach my $chr (@chrs) {
183 s{^ }{} or die "$chr $_ ?";
184 foreach my $col (0..2) {
188 my $f = $cellmap{$&};
190 $f =~ s/\b\d/ sprintf '%05d', $col*2000 + $&*1025 /ge;
191 $f =~ s/\d\b/ sprintf '%05d', $row*2000 + $&*1025 /ge;
192 push @{ $chrpolys{$chr} }, [ split / /, $f ];
204 foreach my $chr (sort keys %chrpolys) {
205 my $mod = chrmodname $chr;
206 p "module $mod () {\n";
207 foreach my $poly (@{ $chrpolys{$chr} }) {
210 foreach my $pt (@$poly) {
212 $pt =~ s{\d{5}}{$&,};
213 $pt =~ s{\b\d}{$&.}g;
222 @demo = reverse $demo =~ m{.{1,$democols}}go;
227 our $do_git; # contains may chars 'c' (count) and/or 'o' (object)
228 our $do_git_untracked = 1;
231 sub rjustt ($$) { # right justify and truncate (ie, pad and truncate at left)
232 my ($sz, $whole) = @_;
233 my $lw = length $whole;
235 ? substr($whole, $lw-$sz)
236 : sprintf "%${sz}s", $whole;
239 sub ljustt ($$$) { # always includes $suffix
240 my ($sz, $whole, $suffix) = @_;
241 $sz -= length $suffix;
242 return sprintf "%-${sz}.${sz}s%s", $whole, $suffix;
245 sub gentextmodule_plusq ($$) {
248 gentextmodule($form, $s);
249 if (!($l & 1) && $l>=4) {
250 gentextmodule("${form}Q", substr($s,0,$l/2), substr($s,$l/2));
256 sub gitrun_start () {
257 open F, "-|", @gcmd or die "$gcmd[0]: start: $!";
260 sub gitrun_done (;$) {
266 die "@gcmd failed ($?)\n";
270 (@gcmd) = (qw(git), @_);
274 defined or die "@gcmd produced no output";
275 chomp or die "@gcmd produced no final newline";
280 return unless $do_git;
282 @gcmd = qw(git status --porcelain);
283 push @gcmd, qw(--untracked=no) unless $do_git_untracked;
288 if (m/^\?\?/ && $do_git_untracked) {
295 gitrun_done($git_dirty eq '*');
300 if ($do_git =~ m/c/) {
301 $git_count = gitoutput qw(rev-list --first-parent --count HEAD);
303 if ($do_git =~ m/o/) {
304 $git_object = gitoutput qw(rev-parse HEAD);
307 foreach my $sz (3..8) {
308 gentextmodule_plusq("Tiny$sz", rjustt($sz, $git_count.$git_dirty))
309 if defined $git_count;
311 gentextmodule_plusq("Git$sz", ljustt($sz, $git_object, $git_dirty))
312 if defined $git_object;
314 gentextmodule("Small$sz",
315 rjustt($sz, $git_count.$git_dirty),
316 ljustt($sz, $git_object, ''))
317 if defined $git_count && defined $git_object;
323 if (m/^--(no)?-git$/) {
324 $do_git = $1 ? '' : 'co';
325 } elsif (m/^---git=object$/) {
328 $do_git_untracked = 0;
329 } elsif (m/^-t(.*)$/) {
331 die "bad usage: -t needs string argument\n";
333 gentextmodule($form, split /\n/, $_);
336 gentextmodule("Arg$argcounter", $_);
339 die "bad usage: unknown option \`$_'\n";
343 $do_git //= defined($argcounter) ? '' : 'co';
345 gentextmodule('FontDemo', @demo);
349 flush STDOUT or die $!;
350 close STDOUT or die $!;
364 /#\ r /#\ ##\ # # ### // ### /#\ /#\
365 # # /# # # # # # # # # # # #
366 # # # /#/ ##< \## ##\ ##\ // >#< \##
367 # # # # # # # # # # # # #
368 \#/ /#\ ### ##/ # ##/ \#/ # \#/ ##/
374 /## ##\ # /## #r# ###
376 \## ##/ \## \## \#/ #