From 882fb7560c7041a4d362c047134b84efa4c77dd4 Mon Sep 17 00:00:00 2001 From: David Allsopp Date: Fri, 19 Jan 2018 14:08:01 +0000 Subject: [PATCH] Add OpamConsole.utf8_symbol Move handling of UTF-8 to OpamConsole, instead of having hard-coded UTF-8 sequences arbitrarily in code. Using Uchar from the stdlib requires extlib-compat rather than extlib (UChar.cmi mustn't be compiled on macOS or Windows). Signed-off-by: David Allsopp --- .travis-ci.sh | 4 +++- opam-core.opam | 1 + src/client/opamSolution.ml | 37 ++++++++++++++++----------------- src/client/opamSwitchCommand.ml | 4 ++-- src/core/opamConsole.ml | 23 ++++++++++++++++++++ src/core/opamConsole.mli | 17 +++++++++++++++ src/solver/opamActionGraph.ml | 18 ++++++++++------ src/solver/opamCudf.ml | 3 +-- 8 files changed, 77 insertions(+), 30 deletions(-) diff --git a/.travis-ci.sh b/.travis-ci.sh index f85b8eafd02..8409f36b0a6 100755 --- a/.travis-ci.sh +++ b/.travis-ci.sh @@ -27,7 +27,9 @@ init-bootstrap () { if [ "$OPAM_TEST" = "1" ]; then opam switch create $OPAMBSSWITCH ocaml-system eval $(opam env) - opam install cohttp-lwt-unix ssl cmdliner dose3 opam-file-format re 'jbuilder>=1.0+beta19' 'mccs>=1.1+5' --yes + # extlib is installed, since UChar.cmi causes problems with the search + # order. See also the removal of uChar and uTF8 in src_ext/jbuild-extlib-src + opam install cohttp-lwt-unix ssl cmdliner dose3 opam-file-format re extlib 'jbuilder>=1.0+beta19' 'mccs>=1.1+5' --yes fi rm -f "$OPAMBSROOT"/log/* } diff --git a/opam-core.opam b/opam-core.opam index 5a820f60ec9..5f70d91a2e2 100644 --- a/opam-core.opam +++ b/opam-core.opam @@ -26,4 +26,5 @@ depends: [ "jbuilder" {build & >= "1.0+beta17"} "cppo" {build} ] +conflicts: "extlib-compat" available: ocaml-version >= "4.02.3" diff --git a/src/client/opamSolution.ml b/src/client/opamSolution.ml index 03bbbeeaf9f..1d47d832f86 100644 --- a/src/client/opamSolution.ml +++ b/src/client/opamSolution.ml @@ -656,7 +656,7 @@ let parallel_apply t _action ~requested ?add_roots action_graph = cleanup_artefacts successful; let successful = PackageActionGraph.reduce successful in let failed = PackageActionGraph.reduce (filter_graph failed) in - let print_actions filter color header ?empty actions = + let print_actions filter tint header ?empty actions = let actions = PackageActionGraph.fold_vertex (fun v acc -> if filter v then v::acc else acc) @@ -664,32 +664,31 @@ let parallel_apply t _action ~requested ?add_roots action_graph = in let actions = List.sort PackageAction.compare actions in if actions <> [] then - OpamConsole.msg "%s%s\n%s%s\n" - (OpamConsole.colorise color - (if OpamConsole.utf8 () - then "\xe2\x94\x8c\xe2\x94\x80 " (* U+250C U+2500 *) - else "+- ")) + OpamConsole.(msg "%s%s\n%s%s\n" + (colorise tint + (Printf.sprintf "%s%s " + (utf8_symbol Symbols.box_drawings_light_down_and_right "+") + (utf8_symbol Symbols.box_drawings_light_horizontal "-"))) header (OpamStd.Format.itemize - ~bullet:(OpamConsole.colorise color - (if OpamConsole.utf8 () - then "\xe2\x94\x82 " (* U+2503 *) else "| ")) + ~bullet:(colorise tint + (utf8_symbol Symbols.box_drawings_light_vertical "|" ^ " ")) (fun x -> x) (List.map (String.concat " ") @@ OpamStd.Format.align_table (PackageAction.to_aligned_strings actions))) - (OpamConsole.colorise color - (if OpamConsole.utf8 () - then "\xe2\x94\x94\xe2\x94\x80 " (* U+2514 U+2500 *) - else "+- ")) + (colorise tint + (Printf.sprintf "%s%s " + (utf8_symbol Symbols.box_drawings_light_up_and_right "+") + (utf8_symbol Symbols.box_drawings_light_horizontal "-")))) else match empty with | Some s -> - OpamConsole.msg "%s%s\n" - (OpamConsole.colorise color - (if OpamConsole.utf8 () - then "\xe2\x95\xb6\xe2\x94\x80 " (* U+2576 U+2500 *) - else "- ")) - s + OpamConsole.(msg "%s%s\n" + (colorise tint + (Printf.sprintf "%s%s " + (utf8_symbol Symbols.box_drawings_light_right "-") + (utf8_symbol Symbols.box_drawings_light_horizontal ""))) + s) | None -> () in OpamConsole.msg "\n"; diff --git a/src/client/opamSwitchCommand.ml b/src/client/opamSwitchCommand.ml index dd631553df4..1fa59697529 100644 --- a/src/client/opamSwitchCommand.ml +++ b/src/client/opamSwitchCommand.ml @@ -73,8 +73,8 @@ let list gt ~print_short = let current = Some switch = OpamStateConfig.get_switch_opt () in List.map (if current then OpamConsole.colorise `bold else fun s -> s) - [ if current then if OpamConsole.utf8 () - then "\xe2\x86\x92" (* U+2192 *) else "->" + [ if current then + OpamConsole.(utf8_symbol Symbols.rightwards_arrow "->") else ""; OpamSwitch.to_string switch; OpamStd.List.concat_map "," diff --git a/src/core/opamConsole.ml b/src/core/opamConsole.ml index 29ff9dcf8d8..2d57727ab2f 100644 --- a/src/core/opamConsole.ml +++ b/src/core/opamConsole.ml @@ -56,6 +56,29 @@ let utf8, utf8_extended = | `Always | `Never -> false | `Auto -> Lazy.force auto && OpamStd.Sys.(os () = Darwin)) +module Symbols = struct + let rightwards_arrow = Uchar.of_int 0x2192 + let box_drawings_light_down_and_right = Uchar.of_int 0x250c + let box_drawings_light_horizontal = Uchar.of_int 0x2500 + let box_drawings_light_vertical = Uchar.of_int 0x2502 + let box_drawings_light_up_and_right = Uchar.of_int 0x2514 + let box_drawings_light_right = Uchar.of_int 0x2576 + let circled_division_slash = Uchar.of_int 0x2298 + let asterisk_operator = Uchar.of_int 0x2217 + let north_east_arrow = Uchar.of_int 0x2197 + let south_east_arrow = Uchar.of_int 0x2198 + let clockwise_open_circle_arrow = Uchar.of_int 0x21bb + let greek_small_letter_lambda = Uchar.of_int 0x03bb +end + +let utf8_symbol c s = + if utf8 () then + let b = Buffer.create 4 in + Buffer.add_utf_8_uchar b c; + Buffer.contents b + else + s + let timer () = if debug () then let t = Unix.gettimeofday () in diff --git a/src/core/opamConsole.mli b/src/core/opamConsole.mli index 93741fba760..e312142d81d 100644 --- a/src/core/opamConsole.mli +++ b/src/core/opamConsole.mli @@ -45,6 +45,23 @@ val colorise' : text_style list -> string -> string val acolor : text_style -> unit -> string -> string val acolor_w : int -> text_style -> out_channel -> string -> unit +module Symbols : sig + val rightwards_arrow : OpamCompat.Uchar.t + val box_drawings_light_down_and_right : OpamCompat.Uchar.t + val box_drawings_light_horizontal : OpamCompat.Uchar.t + val box_drawings_light_vertical : OpamCompat.Uchar.t + val box_drawings_light_up_and_right : OpamCompat.Uchar.t + val box_drawings_light_right : OpamCompat.Uchar.t + val circled_division_slash : OpamCompat.Uchar.t + val asterisk_operator : OpamCompat.Uchar.t + val north_east_arrow : OpamCompat.Uchar.t + val south_east_arrow : OpamCompat.Uchar.t + val clockwise_open_circle_arrow : OpamCompat.Uchar.t + val greek_small_letter_lambda : OpamCompat.Uchar.t +end + +val utf8_symbol: OpamCompat.Uchar.t -> string -> string + (** Logging *) (** Timers, only active when debug is on. Returns the time between the diff --git a/src/solver/opamActionGraph.ml b/src/solver/opamActionGraph.ml index 26b1552f953..891fbbaefb6 100644 --- a/src/solver/opamActionGraph.ml +++ b/src/solver/opamActionGraph.ml @@ -31,12 +31,18 @@ let name_of_action = function | `Build _ -> "build" let symbol_of_action = function - | `Remove _ -> "\xe2\x8a\x98 " (* U+2298 *) - | `Install _ -> "\xe2\x88\x97 " (* U+2217 *) - | `Change (`Up,_,_) -> "\xe2\x86\x97 " (* U+2197 *) - | `Change (`Down,_,_) -> "\xe2\x86\x98 " (* U+2198 *) - | `Reinstall _ -> "\xe2\x86\xbb " (* U+21BB *) - | `Build _ -> "\xce\xbb " (* U+03BB *) + | `Remove _ -> + OpamConsole.utf8_symbol OpamConsole.Symbols.circled_division_slash "" + | `Install _ -> + OpamConsole.utf8_symbol OpamConsole.Symbols.asterisk_operator "" + | `Change (`Up,_,_) -> + OpamConsole.utf8_symbol OpamConsole.Symbols.north_east_arrow "" + | `Change (`Down,_,_) -> + OpamConsole.utf8_symbol OpamConsole.Symbols.south_east_arrow "" + | `Reinstall _ -> + OpamConsole.utf8_symbol OpamConsole.Symbols.clockwise_open_circle_arrow "" + | `Build _ -> + OpamConsole.utf8_symbol OpamConsole.Symbols.greek_small_letter_lambda "" let action_strings ?utf8 a = if utf8 = None && (OpamConsole.utf8 ()) || utf8 = Some true diff --git a/src/solver/opamCudf.ml b/src/solver/opamCudf.ml index 98c6ef7f650..38aa17a8de8 100644 --- a/src/solver/opamCudf.ml +++ b/src/solver/opamCudf.ml @@ -220,8 +220,7 @@ let cycle_conflict ~version_map univ cycle = let arrow_concat sl = let arrow = - if OpamConsole.utf8 () then " \xe2\x86\x92 " (* U+2192 *) - else " -> " + OpamConsole.utf8_symbol OpamConsole.Symbols.rightwards_arrow " -> " in String.concat (OpamConsole.colorise `yellow arrow) sl