Skip to content

Commit 4530e08

Browse files
committed
internal: decolorize to prepare for proper colorization
1 parent 69094ae commit 4530e08

6 files changed

Lines changed: 13 additions & 74 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ unescape = "0.1.0"
3333
memmap2 = "0.9.0"
3434
tempfile = "3.8.0"
3535
thiserror = "1.0.50"
36-
ansi_term = "0.12.1"
3736
clap.workspace = true
3837

3938
[dev-dependencies]

src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ pub(crate) mod utils;
88
use std::process;
99

1010
pub(crate) use self::input::{App, Source};
11-
use ansi_term::{Color, Style};
1211
pub(crate) use error::{Error, Result};
1312
use replacer::Replacer;
1413

1514
use clap::Parser;
1615

1716
fn main() {
1817
if let Err(e) = try_main() {
19-
eprintln!("{}: {}", Style::from(Color::Red).bold().paint("error"), e);
18+
eprintln!("{}: {}", "error", e);
2019
process::exit(1);
2120
}
2221
}

src/replacer/mod.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl Replacer {
103103
regex: &regex::bytes::Regex,
104104
limit: usize,
105105
haystack: &'haystack [u8],
106-
use_color: bool,
106+
_use_color: bool,
107107
mut rep: R,
108108
) -> Cow<'haystack, [u8]> {
109109
let mut it = regex.captures_iter(haystack).enumerate().peekable();
@@ -116,17 +116,7 @@ impl Replacer {
116116
// unwrap on 0 is OK because captures only reports matches
117117
let m = cap.get(0).unwrap();
118118
new.extend_from_slice(&haystack[last_match..m.start()]);
119-
if use_color {
120-
new.extend_from_slice(
121-
ansi_term::Color::Blue.prefix().to_string().as_bytes(),
122-
);
123-
}
124119
rep.replace_append(&cap, &mut new);
125-
if use_color {
126-
new.extend_from_slice(
127-
ansi_term::Color::Blue.suffix().to_string().as_bytes(),
128-
);
129-
}
130120
last_match = m.end();
131121
if limit > 0 && i >= limit - 1 {
132122
break;

src/replacer/validate.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::{error::Error, fmt, str::CharIndices};
22

3-
use ansi_term::{Color, Style};
4-
53
#[derive(Debug)]
64
pub struct InvalidReplaceCapture {
75
original_replace: String,
@@ -53,21 +51,19 @@ impl fmt::Display for InvalidReplaceCapture {
5351
// Build up the error to show the user
5452
let mut formatted = String::new();
5553
let mut arrows_start = Span::start_at(0);
56-
let special = Style::new().bold();
57-
let error = Style::from(Color::Red).bold();
5854
for (byte_index, c) in original_replace.char_indices() {
5955
let (prefix, suffix, text) = match SpecialChar::new(c) {
6056
Some(c) => {
61-
(Some(special.prefix()), Some(special.suffix()), c.render())
57+
(Some("" /* special prefix */), Some("" /* special suffix */), c.render())
6258
}
6359
None => {
6460
let (prefix, suffix) = if byte_index == invalid_ident.start
6561
{
66-
(Some(error.prefix()), None)
62+
(Some("" /* error prefix */), None)
6763
} else if byte_index
6864
== invalid_ident.end.checked_sub(1).unwrap()
6965
{
70-
(None, Some(error.suffix()))
66+
(None, Some("" /* error suffix */))
7167
} else {
7268
(None, None)
7369
};
@@ -99,20 +95,20 @@ impl fmt::Display for InvalidReplaceCapture {
9995
let mut arrows = " ".repeat(arrows_span.start);
10096
arrows.push_str(&format!(
10197
"{}",
102-
Style::new().bold().paint("^".repeat(arrows_span.len()))
98+
"^".repeat(arrows_span.len())
10399
));
104100

105101
let ident = invalid_ident.slice(original_replace);
106102
let (number, the_rest) = ident.split_at(*num_leading_digits);
107103
let disambiguous = format!("${{{number}}}{the_rest}");
108104
let error_message = format!(
109105
"The numbered capture group `{}` in the replacement text is ambiguous.",
110-
Style::new().bold().paint(format!("${}", number).to_string())
106+
format!("${}", number).to_string()
111107
);
112108
let hint_message = format!(
113109
"{}: Use curly braces to disambiguate it `{}`.",
114-
Style::from(Color::Blue).bold().paint("hint"),
115-
Style::new().bold().paint(disambiguous)
110+
"hint",
111+
disambiguous
116112
);
117113

118114
writeln!(f, "{}", error_message)?;

tests/cli.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,7 @@ mod cli {
8181
sd().args(["-p", "abc\\d+", "", file.path().to_str().unwrap()])
8282
.assert()
8383
.success()
84-
.stdout(format!(
85-
"{}{}def\n",
86-
ansi_term::Color::Blue.prefix(),
87-
ansi_term::Color::Blue.suffix()
88-
));
84+
.stdout("def");
8985

9086
assert_file(file.path(), "abc123def");
9187

@@ -113,13 +109,7 @@ mod cli {
113109

114110
fn bad_replace_helper_plain(replace: &str) -> String {
115111
let stderr = bad_replace_helper_styled(replace);
116-
117-
// TODO: no easy way to toggle off styling yet. Add a `--color <when>`
118-
// flag, and respect things like `$NO_COLOR`. `ansi_term` is
119-
// unmaintained, so we should migrate off of it anyways
120-
console::AnsiCodeIterator::new(&stderr)
121-
.filter_map(|(s, is_ansi)| (!is_ansi).then_some(s))
122-
.collect()
112+
stderr
123113
}
124114

125115
#[test]
@@ -182,7 +172,7 @@ mod cli {
182172

183173
// NOTE: styled terminal output is platform dependent, so convert to a
184174
// common format, in this case HTML, to check
185-
#[test]
175+
//#[test]
186176
fn ambiguous_replace_ensure_styling() {
187177
let styled_stderr = bad_replace_helper_styled("\t$1bad after");
188178
let html_stderr =
@@ -225,10 +215,7 @@ mod cli {
225215
])
226216
.assert()
227217
.success()
228-
.stdout(format!(
229-
"{}\nfoo\nfoo\n",
230-
ansi_term::Color::Blue.paint("bar")
231-
));
218+
.stdout("bar\nfoo\nfoo");
232219

233220
Ok(())
234221
}

0 commit comments

Comments
 (0)