From 910d9d3cbbac1d2dbe596c647b16e1f658ac3c9f Mon Sep 17 00:00:00 2001 From: Benjamin Nguyen Date: Sun, 25 Jun 2023 23:29:03 -0700 Subject: [PATCH 1/2] 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 f19b3201..3ac15fdc 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 a409e1bb..1a849592 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 e9447beb..caf39c73 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 21472ac4..9d331d7e 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 1fb61e53..9727aad6 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); From 907a6e31d64d2010d987cd8f743e6add047f8592 Mon Sep 17 00:00:00 2001 From: Benjamin Nguyen Date: Sun, 25 Jun 2023 23:35:38 -0700 Subject: [PATCH 2/2] 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 3ac15fdc..4493dc20 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 9d331d7e..c7e060a0 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 {