summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Nguyen <benjamin.van.nguyen@gmail.com>2023-06-25 23:29:03 -0700
committerBenjamin Nguyen <benjamin.van.nguyen@gmail.com>2023-06-25 23:29:03 -0700
commit910d9d3cbbac1d2dbe596c647b16e1f658ac3c9f (patch)
tree64245a411869eb1ecfc57a21ffc6c4df34a49fb0
parent9fbeaa944708fca708afd9bac63ae807e9234b36 (diff)
add support for NO_COLOR
-rw-r--r--README.md2
-rw-r--r--example/.erdtree.toml2
-rw-r--r--src/context/color.rs10
-rw-r--r--src/context/mod.rs6
-rw-r--r--src/main.rs1
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<Option<OsString>> = 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<dyn Error>> {
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);