1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-26 08:25:50 +03:00

termwiz: support NO_COLOR environment variable (#5020)

* termwiz: support NO_COLOR environment variable

* style: update formatting

* refactor: use capabilities for enabling no-color
This commit is contained in:
Orhun Parmaksız 2024-05-05 02:01:47 +03:00 committed by GitHub
parent 67d4ba9f76
commit 1375e79a21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 16 deletions

View File

@ -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`
@ -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
}
}
@ -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.

View File

@ -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)) => {
@ -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)) => {

View File

@ -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};
@ -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,
}
}
}
@ -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
@ -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
}