Skip to content
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

No color #205

Merged
merged 2 commits into from
Jun 26, 2023
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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$"
```
Expand Down Expand Up @@ -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/).

<p align="center">
<img src="https://github.com/solidiquis/erdtree/blob/master/assets/colorization.png?raw=true" alt="failed to load picture" />
</p>
Expand Down
2 changes: 1 addition & 1 deletion example/.erdtree.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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$"
10 changes: 10 additions & 0 deletions src/context/color.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
6 changes: 5 additions & 1 deletion src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 let Some(Some(var)) = color::NO_COLOR.get() {
return !var.is_empty();
}

match self.color {
Coloring::Auto if !self.stdout_is_tty => true,
Coloring::None => true,
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down