Checksumcolor: Difference between revisions

m
(Added Perl example)
m (→‎{{header|Wren}}: Minor tidy)
 
(19 intermediate revisions by 10 users not shown)
Line 10:
<span style="color:#A00">b25</span><span style="color:#AA0">9b2</span><span style="color:#A00">936</span><span style="color:#AA0">bb4</span><span style="color:#A00">600</span><span style="color:#0AA">9be</span><span style="color:#0A0">3f5</span><span style="color:#AA0">cc0</span><span style="color:#0A0">6a12c3</span>2d coreutils-8.30.tar.gz
<span style="color:#00A">03c</span><span style="color:#A00">f26420</span><span style="color:#AA0">de5</span><span style="color:#00A">66c306</span><span style="color:#A00">d34</span><span style="color:#0AA">0df</span><span style="color:#00A">52f</span><span style="color:#0AA">6cc</span>d7 coreutils-8.31.tar.gz
=={{header|BASIC}}==
==={{header|QBasic}}===
{{works with|QBasic}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">DECLARE SUB colourhex (s$)
 
CLS
colourhex ("#0123456789ABCDEF")
END
 
SUB colourhex (s$)
FOR i = 1 TO LEN(s$)
ch$ = MID$(s$, i, 1)
k = INSTR("123456789ABCDEF", ch$)
COLOR 15 - k, k
PRINT ch$;
NEXT
COLOR 15, 0 'text_color, background_color
END SUB</syntaxhighlight>
 
==={{header|FreeBASIC}}===
{{trans|Phix}}
<syntaxhighlight lang="freebasic">Sub colourhex(s As String)
For i As Integer = 1 To Len(s)
Dim As String*1 ch = Mid(s,i,1)
Dim As Integer k = Instr("123456789ABCDEF", Ucase(ch))
Color 15-k, k
Print ch;
Next
Color Rgb(255,255,255), Rgb(0,0,0)
End Sub
 
colourhex("#0123456789ABCDEF")
Sleep</syntaxhighlight>
=={{header|Go}}==
{{trans|OCaml}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 124 ⟶ 156:
cat()
}
}</langsyntaxhighlight>
 
{{out}}
Line 130 ⟶ 162:
Same as OCaml entry.
</pre>
=={{header|Julia}}==
Usage: md5sum *.* | julia thisfile.jl
<syntaxhighlight lang="julia">while(occursin(r"^[\d\w]{32}", (begin s = readline() end)))
(crc, restofline) = split(s, " ", limit=2)
for i in 1:3:length(crc)-3
print("\e[38;2", join([";$(16 * parse(Int, string(c), base=16))"
for c in crc[i:i+2]], ""), "m", crc[i:i+2])
end
println("\e[0m", crc[end-1:end], " ", restofline)
end
</syntaxhighlight>{{out}}
Same as the OCaml entry.
=={{header|Nim}}==
{{trans|Go}}
Using the functions provided by the <code>terminal</code> module which avoids manipulating ANSI codes.
 
Usage: md5sum *.* | ./colorchecksum
 
<syntaxhighlight lang="nim">import re, terminal
 
const Colors = [((15, 0, 0), fgRed),
(( 0, 15, 0), fgGreen),
((15, 15, 0), fgYellow),
(( 0, 0, 15), fgBlue),
((15, 0, 15), fgMagenta),
(( 0, 15, 15), fgCyan)]
 
let Re = re"^([A-Fa-f0-9]+)([ \t]+.+)$"
 
type RGB = tuple[r, g, b: int]
 
func squareDist(c1, c2: RGB): int =
let xd = c1.r - c2.r
let yd = c1.g - c2.g
let zd = c1.b - c2.b
result = xd * xd + yd * yd + zd * zd
 
 
func intValue(c: char): int =
case c
of 'a'..'f': ord(c) - ord('a') + 10
of 'A'..'F': ord(c) - ord('A') + 10
of '0'..'9': ord(c) - ord('0')
else: raise newException(ValueError, "incorrect input")
 
 
proc printColor(s: string) =
var k = 0
for i in 0..<(s.len div 3):
let j = i * 3
let c1 = s[j]
let c2 = s[j+1]
let c3 = s[j+2]
k = j + 3
let rgb: RGB = (c1.intValue(), c2.intValue(), c3.intValue())
var m = 676
var color = fgDefault
for cex in Colors:
let sqd = squareDist(cex[0], rgb)
if sqd < m:
color = cex[1]
m = sqd
stdout.setForegroundColor(color)
stdout.write c1, c2, c3
 
setForegroundColor(fgDefault)
for j in k..s.high: stdout.write s[j]
 
 
proc colorChecksum() =
for line in stdin.lines:
var s: array[2, string]
if line.match(Re, s):
printColor(s[0])
echo s[1]
else:
echo line
 
proc cat() =
for line in stdin.lines: echo line
 
if stdout.isatty: colorChecksum()
else: cat()</syntaxhighlight>
 
{{out}}
Same output as that of OCaml program.
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">#load "unix.cma"
#load "str.cma"
 
Line 206 ⟶ 322:
if Unix.isatty Unix.stdout
then color_checksum ()
else cat ()</langsyntaxhighlight>
 
{{out}}
Line 214 ⟶ 330:
<span style="color:#A00">b25</span><span style="color:#AA0">9b2</span><span style="color:#A00">936</span><span style="color:#AA0">bb4</span><span style="color:#A00">600</span><span style="color:#0AA">9be</span><span style="color:#0A0">3f5</span><span style="color:#AA0">cc0</span><span style="color:#0A0">6a12c3</span>2d coreutils-8.30.tar.gz
<span style="color:#00A">03c</span><span style="color:#A00">f26420</span><span style="color:#AA0">de5</span><span style="color:#00A">66c306</span><span style="color:#A00">d34</span><span style="color:#0AA">0df</span><span style="color:#00A">52f</span><span style="color:#0AA">6cc</span>d7 coreutils-8.31.tar.gz
 
=={{header|Perl}}==
{{trans|Sidef}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use Term::ANSIColor qw<colored :constants256>;
Line 225 ⟶ 340:
print colored($_, 'ansi' . hex $_) for $cs =~ /(..)/g;
print " $fn\n";
}</langsyntaxhighlight>
{{out}}
<font face='fixedsys, lucida console, terminal, vga, monospace' style='line-height: 1; letter-spacing: 0; font-size: 12pt'>
Line 232 ⟶ 347:
<span style="color:#afaf87;">90</span><span style="color:#00ffaf;">31</span><span style="color:#ff5fff;">cf</span><span style="filter: contrast(70%) brightness(190%);color:green;">0a</span><span style="color:#ff00af;">c7</span><span style="color:#f5f5f5;">ff</span><span style="color:#d75faf;">a9</span><span style="color:#87af87;">6c</span><span style="color:#ff87ff;">d5</span><span style="color:#d787d7;">b0</span><span style="color:#5fafd7;">4a</span><span style="color:#d75f87;">a8</span><span style="color:purple;">05</span><span style="color:#5f87af;">43</span><span style="color:#d70087;">a2</span><span style="color:#8787d7;">68</span> ref/test/sample.txt<br>
</font>
=={{header|Phix}}==
{{libheader|Phix/online}}
Since text_color() accepts 0..15 we may as well just do it digit-by-digit,
but avoid (eg) black-on-black by using the inverse background colour as well.<br>
You can run this online [http://phix.x10.mx/p2js/Checksumcolor.htm here].
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">colourhex</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">upper</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"123456789ABCDEF"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">text_color</span><span style="color: #0000FF;">(</span><span style="color: #000000;">15</span><span style="color: #0000FF;">-</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">bk_color</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">text_color</span><span style="color: #0000FF;">(</span><span style="color: #004600;">BRIGHT_WHITE</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">bk_color</span><span style="color: #0000FF;">(</span><span style="color: #004600;">BLACK</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">colourhex</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"123456789ABCDEF\n"</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
Varies between windows and linux, but something a bit like this (which is a mock-up), or better yet just click on that link above:
<div width:600px; font-size:100%; font-family: Monaco, monospace;">
<span style="background-color:black; color:white;">1</span><span style="background-color:red; color:magenta;">2</span><span style="background-color:lilac; color:green;">3</span><span style="background-color:cyan; color:brown;">4</span><span style="background-color:purple; color:gray;">5</span><span style="background-color:orange; color:pink;">6</span><span style="background-color:indigo; color:mint;">7</span><span style="background-color:mint; color:indigo;">8</span><span style="background-color:pink; color:orange;">9</span><span style="background-color:gray; color:purple;">A</span><span style="background-color:brown; color:cyan;">B</span><span style="background-color:green; color:lilac;">C</span><span style="background-color:magenta; color:red;">D</span><span style="background-color:yellow; color:blue;">E</span><span style="background-color:white; color:black;">F</span>
</div>
=={{header|PicoLisp}}==
<syntaxhighlight lang="picolisp">(in NIL
(until (eof)
(do 16
(let C (pack (char) (char))
(prin "^[[38;5;" (hex C) "m" C "^[[0m") ) )
(prinl (line T)) ) )</syntaxhighlight>
=={{header|Python}}==
<syntaxhighlight lang="python">
#!/usr/bin/env python
"""Colorize MD5 or SHA checksums read from stdin or files.
 
Tested with Python 2.7 and 3.8.
"""
 
from __future__ import unicode_literals
 
import argparse
import fileinput
import os
import sys
 
from functools import partial
from itertools import count
from itertools import takewhile
 
 
ANSI_RESET = "\u001b[0m"
 
RED = (255, 0, 0)
GREEN = (0, 255, 0)
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
MAGENTA = (255, 0, 255)
CYAN = (0, 255, 255)
 
ANSI_PALETTE = {
RED: "\u001b[31m",
GREEN: "\u001b[32m",
YELLOW: "\u001b[33m",
BLUE: "\u001b[34m",
MAGENTA: "\u001b[35m",
CYAN: "\u001b[36m",
}
 
# Some alternative, 8-bit colors. This is just one row from the table at
# https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
_8BIT_PALETTE = {
(0xAF, 0x00, 0x00): "\u001b[38;5;124m",
(0xAF, 0x00, 0x5F): "\u001b[38;5;125m",
(0xAF, 0x00, 0x87): "\u001b[38;5;126m",
(0xAF, 0x00, 0xAF): "\u001b[38;5;127m",
(0xAF, 0x00, 0xD7): "\u001b[38;5;128m",
(0xAF, 0x00, 0xFF): "\u001b[38;5;129m",
(0xAF, 0x5F, 0x00): "\u001b[38;5;130m",
(0xAF, 0x5F, 0x5F): "\u001b[38;5;131m",
(0xAF, 0x5F, 0x87): "\u001b[38;5;132m",
(0xAF, 0x5F, 0xAF): "\u001b[38;5;133m",
(0xAF, 0x5F, 0xD7): "\u001b[38;5;134m",
(0xAF, 0x5F, 0xFF): "\u001b[38;5;135m",
(0xAF, 0x87, 0x00): "\u001b[38;5;136m",
(0xAF, 0x87, 0x5F): "\u001b[38;5;137m",
(0xAF, 0x87, 0x87): "\u001b[38;5;138m",
(0xAF, 0x87, 0xAF): "\u001b[38;5;139m",
(0xAF, 0x87, 0xD7): "\u001b[38;5;140m",
(0xAF, 0x87, 0xFF): "\u001b[38;5;141m",
(0xAF, 0xAF, 0x00): "\u001b[38;5;142m",
(0xAF, 0xAF, 0x5F): "\u001b[38;5;143m",
(0xAF, 0xAF, 0x87): "\u001b[38;5;144m",
(0xAF, 0xAF, 0xAF): "\u001b[38;5;145m",
(0xAF, 0xAF, 0xD7): "\u001b[38;5;146m",
(0xAF, 0xAF, 0xFF): "\u001b[38;5;147m",
(0xAF, 0xD7, 0x00): "\u001b[38;5;148m",
(0xAF, 0xD7, 0x5F): "\u001b[38;5;149m",
(0xAF, 0xD7, 0x87): "\u001b[38;5;150m",
(0xAF, 0xD7, 0xAF): "\u001b[38;5;151m",
(0xAF, 0xD7, 0xD7): "\u001b[38;5;152m",
(0xAF, 0xD7, 0xFF): "\u001b[38;5;153m",
(0xAF, 0xFF, 0x00): "\u001b[38;5;154m",
(0xAF, 0xFF, 0x5F): "\u001b[38;5;155m",
(0xAF, 0xFF, 0x87): "\u001b[38;5;156m",
(0xAF, 0xFF, 0xAF): "\u001b[38;5;157m",
(0xAF, 0xFF, 0xD7): "\u001b[38;5;158m",
(0xAF, 0xFF, 0xFF): "\u001b[38;5;159m",
}
 
 
def error(msg):
"""Exit with an error message."""
sys.stderr.write(msg)
sys.stderr.write(os.linesep)
sys.exit(1)
 
 
def rgb(group):
"""Derive an RGB color from a hexadecimal string."""
nibbles_per_channel = len(group) // 3
max_val = 16 ** nibbles_per_channel - 1
nibbles = chunked(group, nibbles_per_channel)
 
# Transform hex values into the range 0 to 255.
return tuple((int(n, 16) * 255) // max_val for n in nibbles)
 
 
def distance(color, other):
"""Return the difference between two colors. Both ``color`` and ``other``
are three-tuples of RGB values.
 
Uses a simplfied Euclidean distance, as described at
https://en.wikipedia.org/wiki/Color_difference#sRGB
"""
return sum((o - s) ** 2 for s, o in zip(color, other))
 
 
def chunked(seq, n):
"""Split the given sequence into chunks of size `n`. The last item in the
sequence could have a length less than `n`.
"""
return takewhile(len, (seq[i : i + n] for i in count(0, n)))
 
 
def escape(group, palette):
"""Return the given checksum group wrapped with ANSI escape codes."""
key = partial(distance, other=rgb(group.ljust(3, "0")))
ansi_color = min(palette, key=key)
return "".join([palette[ansi_color], group, ANSI_RESET])
 
 
def colorize(line, group_size=3, palette=ANSI_PALETTE):
"""Write a colorized version of the given checksum to stdout."""
checksum, filename = line.split(None, 1)
escaped = [escape(group, palette) for group in chunked(checksum, group_size)]
sys.stdout.write(" ".join(["".join(escaped), filename]))
 
 
def html_colorize(checksum, group_size=3, palette=ANSI_PALETTE):
"""Helper function for generating colorized checksums suitable for display
on RosettaCode."""
 
def span(group):
key = partial(distance, other=rgb(group.ljust(3, "0")))
ansi_color = min(palette, key=key)
int_val = int.from_bytes(ansi_color, byteorder="big")
hex_val = hex(int_val)[2:].rjust(6, "0")
return '<span style="color:#{}">{}</span>'.format(hex_val, group)
 
checksum, filename = line.split(None, 1)
escaped = [span(group) for group in chunked(checksum, group_size)]
sys.stdout.write(" ".join(["".join(escaped), filename]))
 
 
if __name__ == "__main__":
# Command line interface
parser = argparse.ArgumentParser(description="Color checksum.")
 
parser.add_argument(
"-n",
type=int,
default=3,
help="Color the checksum in groups of size N. Defaults to 3.",
)
 
parser.add_argument(
"-e",
"--extended-palette",
action="store_true",
help="Use the extended 8-bit palette. Defaults to False.",
)
 
parser.add_argument(
"--html",
action="store_true",
help="Output checksum groups wrapped with 'span' tags instead of ANSI escape sequences.",
)
 
parser.add_argument("files", nargs="*", default="-", metavar="FILE")
 
args = parser.parse_args()
 
if sys.stdout.isatty():
 
palette = ANSI_PALETTE
if args.extended_palette:
palette = _8BIT_PALETTE
 
colorize_func = colorize
if args.html:
colorize_func = html_colorize
 
for line in fileinput.input(files=args.files):
colorize_func(line, group_size=args.n, palette=palette)
else:
# Piped or redirected. Don't colorize.
for line in fileinput.input(files=args.files):
sys.stdout.write(line)
</syntaxhighlight>
{{out}}
$ md5sum tmp/coreutils-* | checksumcolor
<span style="color:#ffff00">ab0</span><span style="color:#00ff00">6d6</span><span style="color:#ffff00">894</span><span style="color:#ff0000">975</span><span style="color:#ffff00">897</span><span style="color:#00ffff">1fe</span><span style="color:#ff0000">744</span><span style="color:#ffff00">db6</span><span style="color:#00ff00">6b5</span><span style="color:#0000ff">728</span><span style="color:#00ff00">16</span> tmp/coreutils-8.30.tar.xz
<span style="color:#ff0000">000</span><span style="color:#ffff00">9a2</span><span style="color:#0000ff">24d</span><span style="color:#ffff00">8e2</span><span style="color:#ff00ff">88e</span><span style="color:#00ffff">8ec</span><span style="color:#0000ff">406</span><span style="color:#ffff00">ef0</span><span style="color:#00ff00">161</span><span style="color:#ffff00">f92</span><span style="color:#ff0000">93</span> tmp/coreutils-8.31.tar.xz
<span style="color:#ff00ff">f7b</span><span style="color:#00ffff">0e9</span><span style="color:#00ff00">594</span><span style="color:#00ff00">673</span><span style="color:#00ffff">7ce</span><span style="color:#ff0000">907</span><span style="color:#ff00ff">aac</span><span style="color:#ff0000">935</span><span style="color:#ff0000">a12</span><span style="color:#00ffff">bdf</span><span style="color:#ff0000">72</span> tmp/coreutils-8.32.tar.gz
<span style="color:#00ff00">022</span><span style="color:#00ff00">042</span><span style="color:#00ff00">695</span><span style="color:#ff00ff">b7d</span><span style="color:#00ffff">5bc</span><span style="color:#ff00ff">f1a</span><span style="color:#ff0000">935</span><span style="color:#00ffff">59a</span><span style="color:#ff0000">973</span><span style="color:#00ff00">5e6</span><span style="color:#00ff00">68</span> tmp/coreutils-8.32.tar.xz
 
Some 8-bit colors with groups of six.
 
$ md5sum tmp/coreutils-* | checksumcolor -n 6 -e
<span style="color:#af00d7">ab06d6</span><span style="color:#af5f87">894975</span><span style="color:#af5fff">8971fe</span><span style="color:#af5faf">744db6</span><span style="color:#af5f00">6b5728</span><span style="color:#af5f00">16</span> tmp/coreutils-8.30.tar.xz
<span style="color:#af00af">0009a2</span><span style="color:#afd7d7">24d8e2</span><span style="color:#afd7ff">88e8ec</span><span style="color:#af5fff">406ef0</span><span style="color:#af0087">161f92</span><span style="color:#af5f00">93</span> tmp/coreutils-8.31.tar.xz
<span style="color:#afafd7">f7b0e9</span><span style="color:#af5f5f">594673</span><span style="color:#afd700">7ce907</span><span style="color:#afd75f">aac935</span><span style="color:#af00d7">a12bdf</span><span style="color:#af0000">72</span> tmp/coreutils-8.32.tar.gz
<span style="color:#af005f">022042</span><span style="color:#af5f87">695b7d</span><span style="color:#afd700">5bcf1a</span><span style="color:#af5f87">93559a</span><span style="color:#af5fd7">9735e6</span><span style="color:#af8700">68</span> tmp/coreutils-8.32.tar.xz
 
SHA256 with 8-bit colors and groups of five.
 
$ sha256sum tmp/coreutils-* | checksumcolor -n 5 -e --html
<span style="color:#af875f">e831b</span><span style="color:#afaf87">3a860</span><span style="color:#af005f">91496</span><span style="color:#afd7af">cdba7</span><span style="color:#af005f">20411</span><span style="color:#af8787">f9748</span><span style="color:#afff87">de815</span><span style="color:#af8787">07798</span><span style="color:#af5f00">f6130</span><span style="color:#afd7ff">adeae</span><span style="color:#af8787">f872d</span><span style="color:#af005f">206e1</span><span style="color:#af005f">b057</span> tmp/coreutils-8.30.tar.xz
<span style="color:#afff87">ff7a9</span><span style="color:#af8700">c918e</span><span style="color:#afd7ff">dce6b</span><span style="color:#afff5f">4f4b2</span><span style="color:#af005f">725e3</span><span style="color:#af87af">f9b37</span><span style="color:#af00d7">b0c4d</span><span style="color:#af875f">19353</span><span style="color:#afd7af">1cac4</span><span style="color:#afaf5f">9a48b</span><span style="color:#af5fd7">56c4d</span><span style="color:#afd75f">0d3a9</span><span style="color:#af87ff">e9fd</span> tmp/coreutils-8.31.tar.xz
<span style="color:#af5faf">d5ab0</span><span style="color:#af5f5f">7435a</span><span style="color:#af5f00">74058</span><span style="color:#afaf5f">ab69a</span><span style="color:#af0000">2007e</span><span style="color:#af5f87">838be</span><span style="color:#afff5f">4f6a9</span><span style="color:#afaf5f">0b563</span><span style="color:#afd787">5d812</span><span style="color:#af00ff">c2e26</span><span style="color:#af8700">671e3</span><span style="color:#af8700">972fc</span><span style="color:#af00af">a1b8</span> tmp/coreutils-8.32.tar.gz
<span style="color:#af5f5f">4458d</span><span style="color:#afd7ff">8de78</span><span style="color:#af87d7">49df4</span><span style="color:#afd7d7">4ccab</span><span style="color:#af5fff">15e16</span><span style="color:#af005f">b1548</span><span style="color:#af0087">b2852</span><span style="color:#af5fd7">24dbb</span><span style="color:#af5fff">a5f08</span><span style="color:#afafd7">fac07</span><span style="color:#afd700">0c1c0</span><span style="color:#af00af">e0bcc</span><span style="color:#afd7ff">4cfa</span> tmp/coreutils-8.32.tar.xz
 
No colors when piping or redirecting from checksumcolor
 
$ md5sum tmp/coreutils-* | checksumcolor | head
=={{header|Perl 6}}==
ab06d68949758971fe744db66b572816 tmp/coreutils-8.30.tar.xz
0009a224d8e288e8ec406ef0161f9293 tmp/coreutils-8.31.tar.xz
f7b0e95946737ce907aac935a12bdf72 tmp/coreutils-8.32.tar.gz
022042695b7d5bcf1a93559a9735e668 tmp/coreutils-8.32.tar.xz
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2019.03}}
To determine the colors, rather than breaking the md5sum into groups of 3 characters, (which leaves two lonely characters at the end), I elected to replicate the first 5 characters onto the end, then for each character, used it and the 5 characters following as a true-color index. I also added an option to output as HTML code for ease of pasting in here.
 
<syntaxhighlight lang="raku" perl6line>unit sub MAIN ($mode = 'ANSI');
 
if $*OUT.t or $mode eq 'HTML' { # if OUT is a terminal or if in HTML $module
Line 268 ⟶ 634:
.say while $_ = get();
}
</syntaxhighlight>
</lang>
 
Can't really show the ANSI output directly so show the HTML output. Essentially identical.
 
{{out}}
<pre>md5sum *.p6 | perl6raku checksum-color.p6 HTML > checksum-color.html</pre>
yields:
 
Line 286 ⟶ 652:
<br>
</div>
=={{header|Ring}}==
<syntaxhighlight lang="ring">
load "consolecolors.ring"
 
str = "#0123456789ABCDEF"
color = [CC_FG_BLACK,CC_FG_DARK_RED,CC_FG_DARK_GREEN,CC_FG_DARK_YELLOW, CC_FG_DARK_BLUE, CC_FG_DARK_MAGENTA,
CC_FG_DARK_CYAN,CC_FG_GRAY,CC_FG_DARK_GRAY,CC_FG_RED,CC_FG_GREEN,CC_FG_YELLOW,CC_FG_BLUE,
CC_FG_MAGENTA,CC_FG_CYAN,CC_FG_WHITE,CC_BG_DARK_RED ]
 
for n = 1 to len(str)
cc_print(color[n] | CC_FG_WHITE,str[n])
next
</syntaxhighlight>
{{out}}
<pre>
Same as the Phix entry.
</pre>
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var ansi = frequire("Term::ANSIColor")
 
func colorhash(hash) {
Line 301 ⟶ 683:
say line
}
}</langsyntaxhighlight>
{{out}}
<div style="background-color:black; color:white; width:600px; font-size:100%; font-family: Monaco, monospace;">
Line 314 ⟶ 696:
<font color="#D7AFAF">b5</font><font color="#AF5FAF">85</font><font color="#D7FFD7">c2</font><font color="#5FD7FF">51</font><font color="#5FAF00">46</font><font color="#121212">e9</font><font color="#5FD75F">4d</font><font color="#767676">f3</font><font color="#87D700">70</font><font color="#303030">ec</font><font color="#5FAF87">48</font><font color="#5FAF00">46</font><font color="#8787FF">69</font><font color="#00005F">11</font><font color="#FFAFAF">d9</font><font color="#D78787">ae</font> pell.sf
</div>
=={{header|Wren}}==
{{trans|OCaml}}
{{libheader|Wren-dynamic}}
{{libheader|Wren-fmt}}
{{libheader|Wren-ioutil}}
{{libheader|Wren-pattern}}
Translated via the Go entry. Currently, Wren-cli has no way to run external utilities. The md5sum's have therefore been pre-computed and saved to a file.
<syntaxhighlight lang="wren">import "./dynamic" for Tuple
import "./fmt" for Conv, Fmt
import "./ioutil" for FileUtil, Stdout
import "./pattern" for Pattern
 
var Color = Tuple.create("Color", ["r", "g", "b"])
 
var ColorEx = Tuple.create("ColorEx", ["color", "code"])
 
var colors = [
ColorEx.new(Color.new(15, 0, 0), "31"),
ColorEx.new(Color.new( 0, 15, 0), "32"),
ColorEx.new(Color.new(15, 15, 0), "33"),
ColorEx.new(Color.new( 0, 0, 15), "34"),
ColorEx.new(Color.new(15, 0, 15), "35"),
ColorEx.new(Color.new( 0, 15, 15), "36")
]
 
var squareDist = Fn.new { |c1, c2|
var xd = c2.r - c1.r
var yd = c2.g - c1.g
var zd = c2.b - c1.b
return xd*xd + yd*yd + zd*zd
}
 
var printColor = Fn.new { |s|
var n = s.count
var k = 0
for (i in 0...(n/3).floor){
var j = i * 3
var c1 = s[j]
var c2 = s[j+1]
var c3 = s[j+2]
k = j + 3
var r = Conv.atoi(c1, 16)
var g = Conv.atoi(c2, 16)
var b = Conv.atoi(c3, 16)
var rgb = Color.new(r, g, b)
var m = 676
var colorCode = ""
for (cex in colors) {
var sqd = squareDist.call(cex.color, rgb)
if (sqd < m) {
colorCode = cex.code
m = sqd
}
}
Fmt.write("\e[$s;1m$s$s$s\e[00m", colorCode, c1, c2, c3)
}
var j = k
while (j < n) {
var c = s[j]
Fmt.write("\e[0;1m$s\e[00m", c)
j = j + 1
}
Stdout.flush()
}
 
var i = " \t"
var p = Pattern.new("[+1/w][+1/i+1/z]", Pattern.whole, i)
 
var colorChecksum = Fn.new { |fileName|
for (line in FileUtil.readLines(fileName)) {
if (!line) return
if (p.isMatch(line)) {
var m = p.findAll(line)[0]
var s1 = m.capsText[0]
var s2 = m.capsText[1]
printColor.call(s1)
System.print(s2)
} else {
System.print(line)
}
}
}
 
colorChecksum.call("md5sums.txt")</syntaxhighlight>
 
{{out}}
<pre>
Same as the OCaml entry
</pre>
 
=={{header|zkl}}==
{{trans|OCaml}}
<langsyntaxhighlight lang="zkl">var [const] colorRGBs=T(T(15, 0, 0), T(0 ,15, 0), T(15, 15, 0),
T( 0, 0, 15), T(15, 0, 15), T( 0, 15, 15) ),
colorTxt =T("31","32","33","34","35","36"); // esc[<ab>m
Line 331 ⟶ 802:
})
.append("\e[0m",chksum[k,*]); // reset color, rest of check sum
}</langsyntaxhighlight>
Fake "md5sum coreutils-* | zkl checksumcolor" for testing
<langsyntaxhighlight lang="zkl">re,lines := RegExp("([A-Fa-f0-9]+)([ \t]+.+)"),
#<<<
"ab20d840e13adfebf2b6936a2dab071b coreutils-8.29.tar.gz
Line 346 ⟶ 817:
println(colorize(chksum),txt);
}
}</langsyntaxhighlight>
{{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)
<langsyntaxhighlight lang="zkl">re:=RegExp("([A-Fa-f0-9]+)([ \t]+.+)");
foreach line in (File.stdin){
if(re.search(line)){
Line 357 ⟶ 828:
println(colorize(chksum),txt);
} else print(line);
}</langsyntaxhighlight>
9,476

edits