Skip to content

Commit a3abba6

Browse files
committed
Make api-diff output colored
Signed-off-by: Nathan Rebours <nathan.p.rebours@gmail.com>
1 parent d4419bf commit a3abba6

File tree

7 files changed

+39
-5
lines changed

7 files changed

+39
-5
lines changed

api-watch.opam

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ depends: [
1313
"ppx_deriving"
1414
"logs"
1515
"containers"
16+
"fmt"
1617
"cmdliner" {>= "1.1.0"}
1718
"diffutils"
1819
"odoc" {with-doc}

bin/api_diff.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ let run (`Main_module main_module) (`Ref_cmi reference) (`Current_cmi current) =
3737
| None -> Ok 0
3838
| Some diff ->
3939
let text_diff = Api_watch.Text_diff.from_diff diff in
40-
Api_watch.Text_diff.pp Format.std_formatter text_diff;
40+
Api_watch.Text_diff.With_colors.pp Format.std_formatter text_diff;
4141
Ok 1
4242

4343
let named f = Cmdliner.Term.(app (const f))
@@ -81,5 +81,6 @@ let info =
8181
let term = Cmdliner.Term.(const run $ main_module $ ref_cmi $ current_cmi)
8282

8383
let () =
84+
Fmt_tty.setup_std_outputs ();
8485
let exit_code = Cmdliner.Cmd.eval_result' (Cmdliner.Cmd.v info term) in
8586
exit exit_code

bin/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
(executable
22
(name api_diff)
33
(public_name api-diff)
4-
(libraries api-watch cmdliner compiler-libs.common))
4+
(libraries api-watch cmdliner compiler-libs.common fmt.tty))

dune-project

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
ppx_deriving
2121
logs
2222
containers
23+
fmt
2324
(cmdliner (>= 1.1.0))
2425
diffutils))

lib/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
(public_name api-watch)
44
(preprocess
55
(pps ppx_deriving.std))
6-
(libraries compiler-libs.common diffutils unix containers))
6+
(libraries compiler-libs.common diffutils unix containers fmt))

lib/text_diff.ml

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,35 @@ let from_diff (diff : Diff.module_) : Diffutils.Diff.t String_map.t =
8888
in
8989
process_module_diff diff.mname diff String_map.empty
9090

91-
let pp fmt t =
91+
let pp_diff fmt diff = Diffutils.Diff.pp Diffutils.Diff.git_printer fmt diff
92+
93+
let gen_pp pp_diff fmt t =
9294
let print_module_diff module_path diff =
9395
Format.fprintf fmt "diff module %s:\n" module_path;
94-
Diffutils.Diff.pp Diffutils.Diff.git_printer Format.std_formatter diff;
96+
pp_diff fmt diff;
9597
Format.fprintf fmt "\n"
9698
in
9799
String_map.iter print_module_diff t
100+
101+
let pp fmt t = gen_pp pp_diff fmt t
102+
103+
module With_colors = struct
104+
let pp_l fmt ~color ~prefix ~line =
105+
Format.fprintf fmt "%a%a\n"
106+
Fmt.(styled color string)
107+
prefix
108+
Fmt.(styled color string)
109+
line
110+
111+
let pp_add fmt line = pp_l fmt ~color:`Green ~prefix:"+" ~line
112+
let pp_remove fmt line = pp_l fmt ~color:`Red ~prefix:"-" ~line
113+
let pp_keep fmt line = Format.fprintf fmt " %s\n" line
114+
115+
let printer =
116+
Diffutils.Diff.printer ~same:pp_keep ~diff:(fun fmt { orig; new_ } ->
117+
List.iter (pp_remove fmt) orig;
118+
List.iter (pp_add fmt) new_)
119+
120+
let pp_diff fmt diff = Diffutils.Diff.pp printer fmt diff
121+
let pp fmt t = gen_pp pp_diff fmt t
122+
end

lib/text_diff.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,9 @@ val from_diff : Diff.module_ -> Diffutils.Diff.t String_map.t
2424

2525
val pp : Format.formatter -> t -> unit
2626
(** Pretty-print the text diff in a human readable, git diff like format. *)
27+
28+
module With_colors : sig
29+
val pp : Format.formatter -> t -> unit
30+
(** Same as regular [pp] but prints added lines in green and removed lines
31+
in red. *)
32+
end

0 commit comments

Comments
 (0)