From 910d9d3cbbac1d2dbe596c647b16e1f658ac3c9f Mon Sep 17 00:00:00 2001 From: Benjamin Nguyen Date: Sun, 25 Jun 2023 23:29:03 -0700 Subject: add support for NO_COLOR --- README.md | 2 +- example/.erdtree.toml | 2 +- src/context/color.rs | 10 ++++++++++ src/context/mod.rs | 6 +++++- src/main.rs | 1 + 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f19b320..3ac15fd 100644 --- a/README.md +++ b/README.md @@ -378,7 +378,7 @@ long = true # How many lines of Rust are in this code base? # e.g. `erd --config rs` [rs] -disk-usage = "word" +disk-usage = "line" level = 1 pattern = "\\.rs$" ``` diff --git a/example/.erdtree.toml b/example/.erdtree.toml index a409e1b..1a84959 100644 --- a/example/.erdtree.toml +++ b/example/.erdtree.toml @@ -21,6 +21,6 @@ long = true # How many lines of Rust are in this code base? [rs] -disk-usage = "word" +disk-usage = "line" level = 1 pattern = "\\.rs$" diff --git a/src/context/color.rs b/src/context/color.rs index e9447be..caf39c7 100644 --- a/src/context/color.rs +++ b/src/context/color.rs @@ -1,4 +1,14 @@ use clap::ValueEnum; +use once_cell::sync::OnceCell; +use std::{env, ffi::OsString}; + +pub static NO_COLOR: OnceCell> = OnceCell::new(); + +/// Reads in the `NO_COLOR` environment variable to determine whether or not to display color in +/// the output. +pub fn no_color_env() { + let _ = NO_COLOR.set(env::var_os("NO_COLOR")); +} /// Enum to determine how the output should be colorized. #[derive(Clone, Copy, Debug, ValueEnum, PartialEq, Eq, Default)] diff --git a/src/context/mod.rs b/src/context/mod.rs index 21472ac..9d331d7 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -326,7 +326,11 @@ impl Context { /// the Coloring, and whether or not stdout is connected to a tty. /// /// If Coloring is Force then this will always evaluate to `false`. - pub const fn no_color(&self) -> bool { + pub fn no_color(&self) -> bool { + if color::NO_COLOR.get().is_some_and(Option::is_some) { + return true; + } + match self.color { Coloring::Auto if !self.stdout_is_tty => true, Coloring::None => true, diff --git a/src/main.rs b/src/main.rs index 1fb61e5..9727aad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -79,6 +79,7 @@ fn run() -> Result<(), Box> { return Ok(()); } + context::color::no_color_env(); styles::init(ctx.no_color()); let indicator = (ctx.stdout_is_tty && !ctx.no_progress).then(progress::Indicator::measure); -- cgit v1.2.3 From 907a6e31d64d2010d987cd8f743e6add047f8592 Mon Sep 17 00:00:00 2001 From: Benjamin Nguyen Date: Sun, 25 Jun 2023 23:35:38 -0700 Subject: no color support --- README.md | 2 ++ src/context/mod.rs | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3ac15fd..4493dc2 100644 --- a/README.md +++ b/README.md @@ -773,6 +773,8 @@ If, however, the default behavior doesn't suit your needs you have control over - force: Turn on colorization always ``` +`erdtree` also supports [NO_COLOR](https://no-color.org/). +

failed to load picture

diff --git a/src/context/mod.rs b/src/context/mod.rs index 9d331d7..c7e060a 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -327,8 +327,8 @@ impl Context { /// /// If Coloring is Force then this will always evaluate to `false`. pub fn no_color(&self) -> bool { - if color::NO_COLOR.get().is_some_and(Option::is_some) { - return true; + if let Some(Some(var)) = color::NO_COLOR.get() { + return !var.is_empty(); } match self.color { -- cgit v1.2.3