8000 Support `CLICOLOR` and `CLICOLOR_FORCE` to manage ANSI colors by MisterDA · Pull Request #6340 · ocaml/dune · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Support CLICOLOR and CLICOLOR_FORCE to manage ANSI colors #6340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Unreleased
----------

- Support `CLICOLOR` and `CLICOLOR_FORCE` to enable/disable/force ANSI
colors. (#6340, fixes #6323, @MisterDA).

8000 - Allow `Byte_complete` binaries to be installable (#4873, @AltGr, @rgrinberg)

- Revive `$ dune external-lib-deps` under `$ dune describe external-lib-deps`.
Expand Down
6 changes: 3 additions & 3 deletions bin/build_cmd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ let runtest_info =
]
]
in
Cmd.info "runtest" ~doc ~man
Cmd.info "runtest" ~doc ~man ~envs:Common.envs

let runtest_term =
let name_ = Arg.info [] ~docv:"DIR" in
Expand Down Expand Up @@ -184,7 +184,7 @@ let build =
in
run_build_command ~common ~config ~request
in
Cmd.v (Cmd.info "build" ~doc ~man) term
Cmd.v (Cmd.info "build" ~doc ~man ~envs:Common.envs) term

let fmt =
let doc = "Format source code." in
Expand All @@ -209,4 +209,4 @@ let fmt =
in
run_build_command ~common ~config ~request
in
Cmd.v (Cmd.info "fmt" ~doc ~man) term
Cmd.v (Cmd.info "fmt" ~doc ~man ~envs:Common.envs) term
15 changes: 15 additions & 0 deletions bin/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,21 @@ let term_with_default_root_is_cwd = term ~default_root_is_cwd:true

let term = term ~default_root_is_cwd:false

let envs =
Cmd.Env.
[ info
~doc:
"If different than $(b,0), ANSI colors are supported and should be \
used when the program isn’t piped. If equal to $(b,0), don’t output \
ANSI color escape codes"
"CLICOLOR"
; info
~doc:
"If different than $(b,0), ANSI colors should be enabled no matter \
what."
"CLICOLOR_FORCE"
]

let config_from_config_file = Options_implied_by_dash_p.config_term

let context_arg ~doc =
Expand Down
2 changes: 2 additions & 0 deletions bin/common.mli
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ val term : t Cmdliner.Term.t

val term_with_default_root_is_cwd : t Cmdliner.Term.t

val envs : Cmdliner.Cmd.Env.info list

(** Set whether Dune should print the "Entering directory '<dir>'" message *)
val set_print_directory : t -> bool -> t

Expand Down
2 changes: 1 addition & 1 deletion bin/help.ml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ let man =
; Common.footer
]

let info = Cmd.info "help" ~doc ~man
let info = Cmd.info "help" ~doc ~man ~envs:Common.envs

let term =
Term.ret
Expand Down
2 changes: 1 addition & 1 deletion bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let common_commands_synopsis =

let info =
let doc = "composable build system for OCaml" in
Cmd.info "dune" ~doc
Cmd.info "dune" ~doc ~envs:Common.envs
~version:
(match Build_info.V1.version () with
| None -> "n/a"
Expand Down
26 changes: 18 additions & 8 deletions otherlibs/stdune/ansi_color.ml
< 5587 td id="diff-99cf0218f7d1ed2aa6092f57892c13a299cddbd5ed21da729870d956eb011b32L134" data-line-number="134" class="blob-num blob-num-deletion js-linkable-line-number">
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,28 @@ module Style = struct
Printf.sprintf "\027[%sm" (String.concat l ~sep:";")
end

let term_supports_color =
lazy
(match Stdlib.Sys.getenv "TERM" with
let supports_color fd =
let is_smart =
match Stdlib.Sys.getenv "TERM" with
| exception Not_found -> false
| "dumb" -> false
| _ -> true)
| _ -> true
and clicolor =
match Stdlib.Sys.getenv "CLICOLOR" with
| exception Not_found -> true
| "0" -> false
| _ -> true
and clicolor_force =
match Stdlib.Sys.getenv "CLICOLOR_FORCE" with
| exception Not_found -> false
| "0" -> false
| _ -> true
in
(is_smart && Unix.isatty fd && clicolor) || clicolor_force

let stdout_supports_color =
lazy (Lazy.force term_supports_color && Unix.isatty Unix.stdout)
let stdout_supports_color = lazy (supports_color Unix.stdout)

let stderr_supports_color =
lazy (Lazy.force term_supports_color && Unix.isatty Unix.stderr)
let stderr_supports_color = lazy (supports_color Unix.stderr)

let rec tag_handler current_styles ppf styles pp =
Format.pp_print_as ppf 0 (Style.escape_sequence_no_reset styles);
Expand Down
0