term: Remove `concolor` dependency

Use `anstyle-query` and `is-terminal` instead, which are the replacement
crates that are also used in `snapbox`.
This commit is contained in:
Alexis Sellier 2023-05-10 15:54:28 +02:00
parent d249937a61
commit 8da6363cb3
No known key found for this signature in database
4 changed files with 437 additions and 331 deletions

718
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,8 @@ edition = "2021"
[dependencies]
anyhow = { version = "1" }
concolor = { version = "0", features = ["api_unstable"] }
anstyle-query = { version = "1.0.0" }
is-terminal = { version = "0.4.4" }
inquire = { version = "0.6", default-features = false, features = ["termion", "editor"] }
once_cell = { version = "1.13" }
termion = { version = "2" }

View File

@ -1,10 +1,18 @@
use std::fmt;
use std::sync;
use std::sync::atomic::AtomicBool;
use std::{fmt, io};
use is_terminal::IsTerminal;
use unicode_width::UnicodeWidthStr;
use super::color::Color;
use super::style::{Property, Style};
/// Whether paint styling is enabled or not.
static ENABLED: AtomicBool = AtomicBool::new(true);
/// Whether paint styling should be forced.
static FORCED: AtomicBool = AtomicBool::new(false);
/// A structure encapsulating an item and styling.
#[derive(Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash, Copy, Clone)]
pub struct Paint<T> {
@ -251,7 +259,36 @@ impl<T: fmt::Display> fmt::Display for Paint<T> {
impl Paint<()> {
/// Returns `true` if coloring is enabled and `false` otherwise.
pub fn is_enabled() -> bool {
concolor::get(concolor::Stream::Stdout).ansi_color()
if FORCED.load(sync::atomic::Ordering::SeqCst) {
return true;
}
let clicolor = anstyle_query::clicolor();
let clicolor_enabled = clicolor.unwrap_or(false);
let clicolor_disabled = !clicolor.unwrap_or(true);
let is_terminal = io::stdout().is_terminal();
let is_enabled = ENABLED.load(sync::atomic::Ordering::SeqCst);
is_terminal
&& is_enabled
&& !anstyle_query::no_color()
&& !clicolor_disabled
&& (anstyle_query::term_supports_color() || clicolor_enabled || anstyle_query::is_ci())
|| anstyle_query::clicolor_force()
}
/// Enable paint styling.
pub fn enable() {
ENABLED.store(true, sync::atomic::Ordering::SeqCst);
}
/// Force paint styling.
pub fn force() {
FORCED.store(true, sync::atomic::Ordering::SeqCst);
}
/// Disable paint styling.
pub fn disable() {
ENABLED.store(false, sync::atomic::Ordering::SeqCst);
}
}

View File

@ -10,7 +10,7 @@ static SERIAL: Mutex<()> = Mutex::new(());
fn colors_enabled() {
let _guard = SERIAL.lock();
concolor::set(concolor::ColorChoice::Always);
Paint::force();
assert_eq!(
Paint::new("text/plain").to_string(),
@ -144,7 +144,7 @@ fn colors_enabled() {
fn colors_disabled() {
let _guard = SERIAL.lock();
concolor::set(concolor::ColorChoice::Never);
Paint::disable();
assert_eq!(
Paint::new("text/plain").to_string(),
@ -246,7 +246,7 @@ fn wrapping() {
let inner = || format!("{} b {}", Paint::red("a"), Paint::green("c"));
let inner2 = || format!("0 {} 1", Paint::magenta(&inner()).wrap());
concolor::set(concolor::ColorChoice::Always);
Paint::force();
assert_eq!(
Paint::new("text/plain").wrap().to_string(),