Skip to content

Commit

Permalink
termwiz: support NO_COLOR environment variable (wez#5020)
Browse files Browse the repository at this point in the history
* termwiz: support NO_COLOR environment variable

* style: update formatting

* refactor: use capabilities for enabling no-color
  • Loading branch information
orhun authored and saep committed Jul 14, 2024
1 parent 7b63879 commit 10dfc6c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
20 changes: 18 additions & 2 deletions termwiz/src/caps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ use terminfo::{self, capability as cap};

pub mod probed;

/// Environment variable name indicating that color output should be disabled.
/// See <https://no-color.org>
const NO_COLOR_ENV: &str = "NO_COLOR";

builder! {
/// Use the `ProbeHints` to configure an instance of
/// the `ProbeHints` struct. `ProbeHints` are passed to the `Capabilities`
Expand Down Expand Up @@ -121,12 +125,21 @@ builder! {

impl ProbeHints {
pub fn new_from_env() -> Self {
ProbeHints::default()
let mut probe_hints = ProbeHints::default()
.term(var("TERM").ok())
.colorterm(var("COLORTERM").ok())
.colorterm_bce(var("COLORTERM_BCE").ok())
.term_program(var("TERM_PROGRAM").ok())
.term_program_version(var("TERM_PROGRAM_VERSION").ok())
.term_program_version(var("TERM_PROGRAM_VERSION").ok());

if !std::env::var(NO_COLOR_ENV)
.unwrap_or("".to_string())
.is_empty()
{
probe_hints.color_level = Some(ColorLevel::MonoChrome);
}

probe_hints
}
}

Expand All @@ -146,6 +159,9 @@ pub enum ColorLevel {
/// What we care about here is whether the terminal supports the escape
/// sequence to specify RGB values rather than a palette index.
TrueColor,
/// Describes monochrome (black and white) color support.
/// Enabled via NO_COLOR environment variable.
MonoChrome,
}

/// `Capabilities` holds information about the capabilities of a terminal.
Expand Down
8 changes: 6 additions & 2 deletions termwiz/src/render/terminfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ impl TerminfoRenderer {
None => 0,
};

if attr.foreground() != current_foreground {
if attr.foreground() != current_foreground
&& self.caps.color_level() != ColorLevel::MonoChrome
{
match (has_true_color, attr.foreground()) {
(true, ColorAttribute::TrueColorWithPaletteFallback(tc, _))
| (true, ColorAttribute::TrueColorWithDefaultFallback(tc)) => {
Expand Down Expand Up @@ -183,7 +185,9 @@ impl TerminfoRenderer {
}
}

if attr.background() != current_background {
if attr.background() != current_background
&& self.caps.color_level() != ColorLevel::MonoChrome
{
match (has_true_color, attr.background()) {
(true, ColorAttribute::TrueColorWithPaletteFallback(tc, _))
| (true, ColorAttribute::TrueColorWithDefaultFallback(tc)) => {
Expand Down
31 changes: 19 additions & 12 deletions termwiz/src/render/windows.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! A Renderer for windows consoles

use crate::caps::Capabilities;
use crate::caps::ColorLevel;
use crate::cell::{AttributeChange, CellAttributes, Underline};
use crate::color::{AnsiColor, ColorAttribute};
use crate::surface::{Change, Position};
Expand All @@ -17,12 +18,14 @@ use winapi::um::wincon::{

pub struct WindowsConsoleRenderer {
pending_attr: CellAttributes,
capabilities: Capabilities,
}

impl WindowsConsoleRenderer {
pub fn new(_caps: Capabilities) -> Self {
pub fn new(capabilities: Capabilities) -> Self {
Self {
pending_attr: CellAttributes::default(),
capabilities,
}
}
}
Expand Down Expand Up @@ -69,6 +72,21 @@ fn to_attr_word(attr: &CellAttributes) -> u16 {
};
}

let reverse = if attr.reverse() {
COMMON_LVB_REVERSE_VIDEO
} else {
0
};
let underline = if attr.underline() != Underline::None {
COMMON_LVB_UNDERSCORE
} else {
0
};

if attr.capabilities.color_level() == ColorLevel::MonoChrome {
return reverse | underline;
}

let fg = match attr.foreground() {
ColorAttribute::TrueColorWithDefaultFallback(_) | ColorAttribute::Default => {
FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN
Expand Down Expand Up @@ -98,17 +116,6 @@ fn to_attr_word(attr: &CellAttributes) -> u16 {
),
};

let reverse = if attr.reverse() {
COMMON_LVB_REVERSE_VIDEO
} else {
0
};
let underline = if attr.underline() != Underline::None {
COMMON_LVB_UNDERSCORE
} else {
0
};

bg | fg | reverse | underline
}

Expand Down

0 comments on commit 10dfc6c

Please sign in to comment.