Checksumcolor: Difference between revisions

m
m (→‎{{header|Perl 6}}: minor tweaks)
Line 268:
=={{header|zkl}}==
{{trans|OCaml}}
<lang zkl>var [const] colorscolorRGBs=T(T(15, 0, 0), T(0 ,15, 0), T(15, 15, 0),
T(T(15, 0, 0), "31"15), T(T(0 ,15, 0), "32"15), T(T(15 0, 15, 015), "33"),
T(T( 0, 0, 15), "34"), T(T(15, 0, 15), "35"),colorTxt =T(T( 0"31", 15"32", 15)"33","34","35", "36"),; // esc[<ab>m
);
fcn squareDist(rgb1,rgb2){ rgb2.zipWith('-,rgb1).apply("pow",2).sum(0) }
fcn colorize(chksum){ // "check sum text" --> ansi color escape sequence
k:=chksum.len()/3*3; // every three digits gets its own color
chksum[0,k].pump(String,T(Void.Read,2), fcn(r,g,b){
// find color closest to these three digits of check sum
rgb,m,colorCode := vm.arglist.apply("toInt",16), 676, "";
// minMaxNs returns indexes of min and max (in list of ints)
foreach cex in (colors){
rgb,m,colorCode := vm.arglist.apply("toInt",16), 676,: // f("a";,"b","c")-->(10,11,12)
sqd:=squareDist(cex[0],rgb);
colorRGBs.apply(squareDist.fp(_)) : (0).minMaxNs(_)[0] : colorTxt[_] :
if(sqd<m) colorCode,m = cex[1],sqd;
"\e[%s;1m%c%c%c\e[00m".fmt(colorCode_, r,g,b)
}
"\e[%s;1m%c%c%c\e[00m".fmt(colorCode, r,g,b)
})
.append("\e[00m0m",chksum[k,*]); // reset color, rest of check sum
}</lang>
Fake "md5sum coreutils-* | zkl checksumcolor.zkl" for testing
<lang zkl>re,lines := RegExp("([A-Fa-f0-9]+)([ \t]+.+)"),
#<<<
Line 301 ⟶ 299:
}</lang>
{{out}}
<pre>Same as the OCaml entry</pre>
This is what we would do to implement "md5sum chksum.zkl | zkl chksum"
(instead of the above test code)
<lang zkl>re:=RegExp("([A-Fa-f0-9]+)([ \t]+.+)");
foreach cexline in (colorsFile.stdin){
if(re.search(line)){
chksum,txt := re.matched[1,*];
println(colorize(chksum),txt);
} else print(line);
}</lang>
Anonymous user