Skip to content

Commit 76ace66

Browse files
MisterDArgrinberg
authored andcommitted
feature(mdx): add colors to console logs
If Dune should ouput ANSI colors to stderr, so can mdx. Signed-off-by: Antonin Décimo <[email protected]>
1 parent 7e12e09 commit 76ace66

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Unreleased
1313

1414
- Fix preprocessing with `staged_pps` (#6748, fixes #6644, @rgrinberg)
1515

16+
- Use colored output with MDX when Dune colors are enabled.
17+
(#6462, @MisterDA)
18+
1619
- Make `dune describe workspace` return consistent dependencies for
1720
executables and for libraries. By default, compile-time dependencies
1821
towards PPX-rewriters are from now not taken into account (but

src/dune_rules/mdx.ml

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ open Import
22

33
let mdx_version_required = "1.6.0"
44

5+
let color_always : _ Command.Args.t Lazy.t =
6+
lazy
7+
(if Lazy.force Ansi_color.stderr_supports_color then
8+
S [ A "--color=always" ]
9+
else S [])
10+
511
module Files = struct
612
type t =
713
{ src : Path.Build.t
@@ -63,10 +69,12 @@ module Deps = struct
6369
mdx_version_required
6470
]
6571

66-
let rule ~dir ~mdx_prog files =
67-
Command.run ~dir:(Path.build dir) mdx_prog
68-
[ A "deps"; Dep (Path.build files.Files.src) ]
69-
~stdout_to:files.Files.deps
72+
let rule ~dir ~mdx_prog (files : Files.t) =
73+
Command.run ~dir:(Path.build dir) mdx_prog ~stdout_to:files.deps
74+
[ Command.Args.A "deps"
75+
; Lazy.force color_always
76+
; Dep (Path.build files.Files.src)
77+
]
7078

7179
let path_escapes_dir str =
7280
try
@@ -305,8 +313,8 @@ let gen_rules_for_single_file stanza ~sctx ~dir ~expander ~mdx_prog
305313
in
306314
let mdx_generic_deps = Bindings.to_list stanza.deps in
307315
let executable, command_line =
308-
(*The old mdx stanza calls the [ocaml-mdx] executable, new ones the
309-
generated executable *)
316+
(* The old mdx stanza calls the [ocaml-mdx] executable, new ones the
317+
generated executable *)
310318
let open Command.Args in
311319
match mdx_prog_gen with
312320
| Some prog -> (Ok (Path.build prog), [ Dep (Path.build files.src) ])
@@ -315,8 +323,13 @@ let gen_rules_for_single_file stanza ~sctx ~dir ~expander ~mdx_prog
315323
List.concat_map stanza.preludes ~f:(Prelude.to_args ~dir)
316324
in
317325
( mdx_prog
318-
, [ A "test" ] @ prelude_args
319-
@ [ A "-o"; Target files.corrected; Dep (Path.build files.src) ] )
326+
, [ A "test"
327+
; S prelude_args
328+
; Lazy.force color_always
329+
; A "-o"
330+
; Target files.corrected
331+
; Dep (Path.build files.src)
332+
] )
320333
in
321334
let deps, sandbox =
322335
Dep_conf_eval.unnamed ~expander (mdx_package_deps @ mdx_generic_deps)
@@ -365,13 +378,13 @@ let mdx_prog_gen t ~sctx ~dir ~scope ~expander ~mdx_prog =
365378
in
366379
S args
367380
in
368-
let prelude_args =
369-
Command.Args.S (List.concat_map t.preludes ~f:(Prelude.to_args ~dir))
370-
in
381+
let open Command.Args in
382+
let prelude_args = S (List.concat_map t.preludes ~f:(Prelude.to_args ~dir)) in
371383
(* We call mdx to generate the testing executable source *)
372384
let action =
373385
Command.run ~dir:(Path.build dir) mdx_prog ~stdout_to:file
374-
[ A "dune-gen"; prelude_args; Resolve.Memo.args directory_args ]
386+
([ A "dune-gen"; prelude_args; Resolve.Memo.args directory_args ]
387+
@ Lazy.force color_always)
375388
in
376389
let open Memo.O in
377390
let* () = Super_context.add_rule sctx ~loc ~dir action in

0 commit comments

Comments
 (0)