From 1a8ca30f203336ad40b113179a8df2403b6e68ca Mon Sep 17 00:00:00 2001 From: Dmitriy Kovalenko Date: Sat, 24 Aug 2024 12:41:51 +0200 Subject: [PATCH] use spectrum for color output --- .github/workflows/build.yml | 8 +++- bin/Print.ml | 79 ++++++++++++++----------------------- bin/dune | 2 +- dune-project | 1 + odiff.opam | 1 + 5 files changed, 38 insertions(+), 53 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a2e4b5f1..9bb8c2d1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,12 +10,16 @@ jobs: matrix: include: - os: ubuntu-latest + ocaml-compiler: "ocaml-variants.5.2.0+options,ocaml-option-flambda" triplet: "x64-linux" - os: windows-latest + ocaml-compiler: "ocaml-variants.5.2.0+options,ocaml-option-flambda" triplet: "x64-mingw-static" - os: macos-latest + ocaml-compiler: "ocaml-variants.5.2.0+options,ocaml-option-flambda" triplet: "arm64-osx" - os: macos-13 + ocaml-compiler: "ocaml-variants.5.2.0+options,ocaml-option-flambda" triplet: "x64-osx" defaults: run: @@ -63,11 +67,11 @@ jobs: - uses: ocaml/setup-ocaml@v3 with: - ocaml-compiler: "ocaml-variants.5.2.0+options,ocaml-option-flambda" + ocaml-compiler: ${{ matrix.ocaml-compiler }} opam-disable-sandboxing: true dune-cache: false - - run: opam exec -- opam install . --deps-only + - run: opam exec -- opam install . --deps-only --update-invariant - run: opam exec -- dune build --verbose - run: opam exec -- dune exec ODiffBin -- --version diff --git a/bin/Print.ml b/bin/Print.ml index f4992669..1a124d41 100644 --- a/bin/Print.ml +++ b/bin/Print.ml @@ -1,53 +1,32 @@ -(* open Odiff.Diff *) +open Odiff.Diff let printDiffResult makeParsableOutput result = - (* (match (result, makeParsableOutput) with *) - (* | Layout, true -> "" *) - (* | Layout, false -> *) - (* Pastel.createElement *) - (* ~children: *) - (* [ *) - (* (Pastel.createElement ~color:Red ~bold:true ~children:[ "Failure!" ] *) - (* () [@JSX]); *) - (* " Images have different layout.\n"; *) - (* ] *) - (* () [@JSX] *) - (* | Pixel (_output, diffCount, _percentage, _lines), true when diffCount == 0 -> *) - (* "" *) - (* | Pixel (_output, diffCount, _percentage, _lines), false when diffCount == 0 *) - (* -> *) - (* Pastel.createElement *) - (* ~children: *) - (* [ *) - (* (Pastel.createElement ~color:Green ~bold:true *) - (* ~children:[ "Success!" ] () [@JSX]); *) - (* " Images are equal.\n"; *) - (* (Pastel.createElement ~dim:true *) - (* ~children:[ "No diff output created." ] *) - (* () [@JSX]); *) - (* ] *) - (* () [@JSX] *) - (* | Pixel (_output, diffCount, diffPercentage, stack), true *) - (* when not (Stack.is_empty stack) -> *) - (* Int.to_string diffCount ^ ";" *) - (* ^ Float.to_string diffPercentage *) - (* ^ ";" *) - (* ^ (stack *) - (* |> Stack.fold (fun acc line -> (line |> Int.to_string) ^ "," ^ acc) "") *) - (* | Pixel (_output, diffCount, diffPercentage, _), true -> *) - (* Int.to_string diffCount ^ ";" ^ Float.to_string diffPercentage *) - (* | Pixel (_output, diffCount, diffPercentage, _lines), false -> *) - (* Pastel.createElement *) - (* ~children: *) - (* [ *) - (* (Pastel.createElement ~color:Red ~bold:true ~children:[ "Failure!" ] *) - (* () [@JSX]); *) - (* " Images are different.\n"; *) - (* "Different pixels: "; *) - (* (Pastel.createElement ~color:Red ~bold:true *) - (* ~children:[ Printf.sprintf "%i (%f%%)" diffCount diffPercentage ] *) - (* () [@JSX]); *) - (* ] *) - (* () [@JSX]) *) - (* |> Console.log; *) + let reset_ppf = Spectrum.prepare_ppf Format.str_formatter in + (match (result, makeParsableOutput) with + | Layout, true -> () + | Layout, false -> + Spectrum.Simple.printf "@{%s!@} Images have different layout.\n" + "Failure!" + | Pixel (_output, diffCount, diffPercentage, stack), true + when not (Stack.is_empty stack) -> + Int.to_string diffCount ^ ";" + ^ Float.to_string diffPercentage + ^ ";" + ^ (stack + |> Stack.fold (fun acc line -> (line |> Int.to_string) ^ "," ^ acc) "") + |> print_endline + | Pixel (_output, diffCount, diffPercentage, _), true -> + Int.to_string diffCount ^ ";" ^ Float.to_string diffPercentage + |> print_endline + | Pixel (_output, diffCount, _percentage, _lines), false when diffCount == 0 + -> + Spectrum.Simple.printf + "@{Success!@} Images are equal.\n\ + @{No diff output created.@}" + | Pixel (_output, diffCount, diffPercentage, _lines), false -> + Spectrum.Simple.printf + "@{Failure!@} Images are different.\n\ + Different pixels: @{%i (%f%%)@}" diffCount diffPercentage); + + reset_ppf (); result diff --git a/bin/dune b/bin/dune index be8da22d..a9c7b904 100644 --- a/bin/dune +++ b/bin/dune @@ -4,7 +4,7 @@ (package odiff) (flags (:standard -w -27)) - (libraries odiff-core odiff-io cmdliner)) + (libraries odiff-core odiff-io cmdliner spectrum)) (env (dev diff --git a/dune-project b/dune-project index b13970a9..e6eea588 100644 --- a/dune-project +++ b/dune-project @@ -24,6 +24,7 @@ odiff-core odiff-io (cmdliner (= 1.3.0)) + (spectrum (= 0.6.0)) ) ) diff --git a/odiff.opam b/odiff.opam index 2148379f..d845876d 100644 --- a/odiff.opam +++ b/odiff.opam @@ -11,6 +11,7 @@ depends: [ "odiff-core" "odiff-io" "cmdliner" {= "1.3.0"} + "spectrum" {= "0.6.0"} "odoc" {with-doc} ] build: [