Checksumcolor: Difference between revisions

From Rosetta Code
Content added Content deleted
(exemple with colors)
Line 2: Line 2:


Task: The task is to create this command line utility that we can use to pipe the output of the md5sum and shaXsum commands and that colors the checksum part of the output. Take each group of 3 or 6 hexadecimal characters and interpret it as if it was a color code and print it with the closest console color. Print with colors if the output is the terminal or print the input unchanged if the output of the utility is a pipe.
Task: The task is to create this command line utility that we can use to pipe the output of the md5sum and shaXsum commands and that colors the checksum part of the output. Take each group of 3 or 6 hexadecimal characters and interpret it as if it was a color code and print it with the closest console color. Print with colors if the output is the terminal or print the input unchanged if the output of the utility is a pipe.

Exemple:

<span style="color:#AA0">ab2</span><span style="color:#0AA">0d8</span><span style="color:#00A">40e13a</span><span style="color:#0AA">dfe</span><span style="color:#AA0">bf2</span><span style="color:#A0A">b69</span><span style="color:#00A">36a</span><span style="color:#0AA">2da</span><span style="color:#A00">b07</span>1b coreutils-8.29.tar.gz
<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|OCaml}}==
=={{header|OCaml}}==

Revision as of 19:56, 19 April 2019

Context: In December 2013 a patch was proposed to the coreutils list to add a --color option to the commands md5sum and shaXsum to display the checksums in color to make it easier to visually identify similarities in a list of printed checksums. The patch was not accepted for inclusion and instead it was suggested to create a command line utility which can be used to pipe the output of the md5sum and shaXsum commands similar to the utility colordiff.

Task: The task is to create this command line utility that we can use to pipe the output of the md5sum and shaXsum commands and that colors the checksum part of the output. Take each group of 3 or 6 hexadecimal characters and interpret it as if it was a color code and print it with the closest console color. Print with colors if the output is the terminal or print the input unchanged if the output of the utility is a pipe.

Exemple:

ab20d840e13adfebf2b6936a2dab071b  coreutils-8.29.tar.gz
b259b2936bb46009be3f5cc06a12c32d  coreutils-8.30.tar.gz
03cf26420de566c306d340df52f6ccd7  coreutils-8.31.tar.gz


OCaml

<lang ocaml>#load "unix.cma"

  1. load "str.cma"

let colors = [|

 ((15,  0,  0), "31");
 (( 0, 15,  0), "32");
 ((15, 15,  0), "33");
 (( 0,  0, 15), "34");
 ((15,  0, 15), "35");
 (( 0, 15, 15), "36");

|]

let square_dist (r1, g1, b1) (r2, g2, b2) =

 let xd = r2 - r1 in
 let yd = g2 - g1 in
 let zd = b2 - b1 in
 (xd * xd + yd * yd + zd * zd)

let print_color s =

 let n = String.length s in
 let k = ref 0 in
 for i = 0 to pred (n / 3) do
   let j = i * 3 in
   let c1 = s.[j]
   and c2 = s.[j+1]
   and c3 = s.[j+2] in
   k := j+3;
   let rgb =
     int_of_string (Printf.sprintf "0x%c" c1),
     int_of_string (Printf.sprintf "0x%c" c2),
     int_of_string (Printf.sprintf "0x%c" c3)
   in
   let m = ref 676 in
   let color_code = ref "" in
   Array.iteri (fun i (color, code) ->
     let sqd = square_dist color rgb in
     if sqd < !m then begin
       color_code := code;
       m := sqd;
     end
   ) colors;
   Printf.printf "\027[%s;1m%c%c%c\027[00m" !color_code c1 c2 c3;
 done;
 for j = !k to pred n do
   let c = s.[j] in
   Printf.printf "\027[0;1m%c\027[00m" c;
 done

let r = Str.regexp "^\\([A-Fa-f0-9]+\\)\\([ \t]+.+\\)$"

let color_checksum () =

 try while true do
   let line = input_line stdin in
   if Str.string_match r line 0
   then begin
     let s1 = Str.matched_group 1 line in
     let s2 = Str.matched_group 2 line in
     print_color s1;
     print_endline s2;
   end
   else print_endline line
 done with End_of_file -> ()

let cat () =

 try while true do
   let line = input_line stdin in
   print_endline line
 done with End_of_file -> ()

let () =

 if Unix.isatty Unix.stdout
 then color_checksum ()
 else cat ()</lang>
Output:
$ md5sum coreutils-* | ocaml checksumcolor.ml 
ab20d840e13adfebf2b6936a2dab071b  coreutils-8.29.tar.gz
b259b2936bb46009be3f5cc06a12c32d  coreutils-8.30.tar.gz
03cf26420de566c306d340df52f6ccd7  coreutils-8.31.tar.gz

(printed here without the terminal colors)