Skip to content

Commit 05b3872

Browse files
epagedjc
authored andcommitted
refactor: Migrate to anstyle for color definitions
1 parent 2bd6356 commit 05b3872

File tree

6 files changed

+31
-14
lines changed

6 files changed

+31
-14
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ test = ["dep:snapbox", "dep:walkdir"]
4242
# Sorted by alphabetic order
4343
[dependencies]
4444
anstyle = "1.0.11"
45+
anstyle-termcolor = "1.1.4"
4546
anyhow = "1.0.69"
4647
cfg-if = "1.0"
4748
chrono = { version = "0.4", default-features = false, features = ["std"] }

src/cli/common.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use std::path::{Path, PathBuf};
77
use std::sync::LazyLock;
88
use std::{cmp, env};
99

10+
use anstyle::AnsiColor;
1011
use anyhow::{Context, Result, anyhow};
1112
use git_testament::{git_testament, render_testament};
12-
use termcolor::Color;
1313
use tracing::{error, info, warn};
1414
use tracing_subscriber::{EnvFilter, Registry, reload::Handle};
1515

@@ -152,10 +152,10 @@ fn show_channel_updates(
152152
) -> Result<()> {
153153
let data = updates.into_iter().map(|(pkg, result)| {
154154
let (banner, color) = match &result {
155-
Ok(UpdateStatus::Installed) => ("installed", Some(Color::Green)),
156-
Ok(UpdateStatus::Updated(_)) => ("updated", Some(Color::Green)),
155+
Ok(UpdateStatus::Installed) => ("installed", Some(AnsiColor::Green)),
156+
Ok(UpdateStatus::Updated(_)) => ("updated", Some(AnsiColor::Green)),
157157
Ok(UpdateStatus::Unchanged) => ("unchanged", None),
158-
Err(_) => ("update failed", Some(Color::Red)),
158+
Err(_) => ("update failed", Some(AnsiColor::Red)),
159159
};
160160

161161
let (previous_version, version) = match &pkg {

src/cli/markdown.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Write Markdown to the terminal
22
use std::io::Write;
33

4+
use anstyle::AnsiColor;
45
use pulldown_cmark::{Event, Tag, TagEnd};
5-
use termcolor::Color;
66

77
use crate::process::{Attr, ColorableTerminal};
88

@@ -151,7 +151,7 @@ impl<'a> LineFormatter<'a> {
151151
self.wrapper.write_line();
152152
}
153153
Tag::Emphasis => {
154-
self.push_attr(Attr::ForegroundColor(Color::Red));
154+
self.push_attr(Attr::ForegroundColor(AnsiColor::Red));
155155
}
156156
Tag::Strong => {}
157157
Tag::Strikethrough => {}

src/cli/self_update.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ use std::process::Command;
4040
use std::str::FromStr;
4141
use std::{fmt, fs};
4242

43+
use anstyle::AnsiColor;
4344
use anyhow::{Context, Result, anyhow};
4445
use cfg_if::cfg_if;
4546
use clap::ValueEnum;
4647
use clap::builder::PossibleValue;
4748
use itertools::Itertools;
4849
use same_file::Handle;
4950
use serde::{Deserialize, Serialize};
50-
use termcolor::Color;
5151
use tracing::{error, info, trace, warn};
5252

5353
use crate::dist::download::DownloadCfg;
@@ -1368,13 +1368,13 @@ pub(crate) async fn check_rustup_update(dl_cfg: &DownloadCfg<'_>) -> anyhow::Res
13681368
write!(t.lock(), "rustup - ")?;
13691369

13701370
Ok(if current_version != available_version {
1371-
let _ = t.fg(Color::Yellow);
1371+
let _ = t.fg(AnsiColor::Yellow);
13721372
write!(t.lock(), "Update available")?;
13731373
let _ = t.reset();
13741374
writeln!(t.lock(), " : {current_version} -> {available_version}")?;
13751375
true
13761376
} else {
1377-
let _ = t.fg(Color::Green);
1377+
let _ = t.fg(AnsiColor::Green);
13781378
write!(t.lock(), "Up to date")?;
13791379
let _ = t.reset();
13801380
writeln!(t.lock(), " : {current_version}")?;

src/process/terminal_source.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use std::{
99
sync::{Arc, Mutex, MutexGuard},
1010
};
1111

12-
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, StandardStreamLock, WriteColor};
12+
use anstyle::AnsiColor;
13+
use anstyle_termcolor::to_termcolor_color;
14+
use termcolor::ColorChoice;
15+
use termcolor::{ColorSpec, StandardStream, StandardStreamLock, WriteColor};
1316

1417
use super::Process;
1518
#[cfg(feature = "test")]
@@ -108,10 +111,10 @@ impl ColorableTerminal {
108111
}
109112
}
110113

111-
pub fn fg(&mut self, color: Color) -> io::Result<()> {
114+
pub fn fg(&mut self, color: AnsiColor) -> io::Result<()> {
112115
match self.inner.lock().unwrap().deref_mut() {
113116
TerminalInner::StandardStream(s, spec) => {
114-
spec.set_fg(Some(color));
117+
spec.set_fg(Some(to_termcolor_color(color.into())));
115118
s.set_color(spec)
116119
}
117120
#[cfg(feature = "test")]
@@ -124,7 +127,9 @@ impl ColorableTerminal {
124127
TerminalInner::StandardStream(s, spec) => {
125128
match attr {
126129
Attr::Bold => spec.set_bold(true),
127-
Attr::ForegroundColor(color) => spec.set_fg(Some(color)),
130+
Attr::ForegroundColor(color) => {
131+
spec.set_fg(Some(to_termcolor_color(color.into())))
132+
}
128133
};
129134
s.set_color(spec)
130135
}
@@ -356,7 +361,7 @@ impl StreamSelector {
356361
#[derive(Copy, Clone, Debug)]
357362
pub enum Attr {
358363
Bold,
359-
ForegroundColor(Color),
364+
ForegroundColor(AnsiColor),
360365
}
361366

362367
#[cfg(test)]

0 commit comments

Comments
 (0)