Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ffd68e9

Browse files
committed
refactor!: Switch to anstyle for color
BREAKING CHANGE: This removes `color` feature
1 parent ca5ebc9 commit ffd68e9

File tree

5 files changed

+109
-89
lines changed

5 files changed

+109
-89
lines changed

‎Cargo.lock

Lines changed: 1 addition & 10 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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,19 @@ keywords = ["code", "analysis", "ascii", "errors", "debug"]
1414
maintenance = { status = "actively-developed" }
1515

1616
[dependencies]
17+
anstyle = "1.0.4"
1718
unicode-width = "0.1.11"
18-
yansi-term = { version = "0.1.2", optional = true }
1919

2020
[dev-dependencies]
2121
criterion = "0.5.1"
2222
difference = "2.0.0"
2323
glob = "0.3.1"
2424
serde = { version = "1.0.192", features = ["derive"] }
2525
toml = "0.5.11"
26-
yansi-term = "0.1.2"
2726

2827
[[bench]]
2928
name = "simple"
3029
harness = false
3130

3231
[features]
3332
default = []
34-
color = ["yansi-term"]

‎src/display_list/mod.rs

Lines changed: 73 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ use crate::snippet;
3535
use std::cmp::{max, min};
3636
use std::fmt::{Display, Write};
3737
use std::{cmp, fmt};
38-
use yansi_term::Style;
38+
39+
use anstyle::Style;
3940

4041
use crate::renderer::StyleSheet;
4142

@@ -204,7 +205,15 @@ impl<'a> DisplayList<'a> {
204205
for fragment in label {
205206
match fragment.style {
206207
DisplayTextStyle::Regular => fragment.content.fmt(f)?,
207-
DisplayTextStyle::Emphasis => emphasis_style.paint(fragment.content).fmt(f)?,
208+
DisplayTextStyle::Emphasis => {
209+
write!(
210+
f,
211+
"{}{}{}",
212+
emphasis_style.render(),
213+
fragment.content,
214+
emphasis_style.render_reset()
215+
)?;
216+
}
208217
}
209218
}
210219
Ok(())
@@ -231,26 +240,21 @@ impl<'a> DisplayList<'a> {
231240
if formatted_len == 0 {
232241
self.format_label(&annotation.label, f)
233242
} else {
234-
color
235-
.paint_fn(Box::new(|f: &mut fmt::Formatter<'_>| {
236-
Self::format_annotation_type(&annotation.annotation_type, f)?;
237-
if let Some(id) = &annotation.id {
238-
f.write_char('[')?;
239-
f.write_str(id)?;
240-
f.write_char(']')?;
241-
}
242-
Ok(())
243-
}))
244-
.fmt(f)?;
243+
write!(f, "{}", color.render())?;
244+
Self::format_annotation_type(&annotation.annotation_type, f)?;
245+
if let Some(id) = &annotation.id {
246+
f.write_char('[')?;
247+
f.write_str(id)?;
248+
f.write_char(']')?;
249+
}
250+
write!(f, "{}", color.render_reset())?;
245251

246252
if !is_annotation_empty(annotation) {
247253
if in_source {
248-
color
249-
.paint_fn(Box::new(|f: &mut fmt::Formatter<'_>| {
250-
f.write_str(": ")?;
251-
self.format_label(&annotation.label, f)
252-
}))
253-
.fmt(f)?;
254+
write!(f, "{}", color.render())?;
255+
f.write_str(": ")?;
256+
self.format_label(&annotation.label, f)?;
257+
write!(f, "{}", color.render_reset())?;
254258
} else {
255259
f.write_str(": ")?;
256260
self.format_label(&annotation.label, f)?;
@@ -361,25 +365,21 @@ impl<'a> DisplayList<'a> {
361365
_ => range.0,
362366
};
363367

364-
color
365-
.paint_fn(|f| {
366-
format_repeat_char(indent_char, indent_length + 1, f)?;
367-
format_repeat_char(mark, range.1 - indent_length, f)
368-
})
369-
.fmt(f)?;
368+
write!(f, "{}", color.render())?;
369+
format_repeat_char(indent_char, indent_length + 1, f)?;
370+
format_repeat_char(mark, range.1 - indent_length, f)?;
371+
write!(f, "{}", color.render_reset())?;
370372

371373
if !is_annotation_empty(annotation) {
372374
f.write_char(' ')?;
373-
color
374-
.paint_fn(|f| {
375-
self.format_annotation(
376-
annotation,
377-
annotation_part == &DisplayAnnotationPart::LabelContinuation,
378-
true,
379-
f,
380-
)
381-
})
382-
.fmt(f)?;
375+
write!(f, "{}", color.render())?;
376+
self.format_annotation(
377+
annotation,
378+
annotation_part == &DisplayAnnotationPart::LabelContinuation,
379+
true,
380+
f,
381+
)?;
382+
write!(f, "{}", color.render_reset())?;
383383
}
384384

385385
Ok(())
@@ -408,7 +408,13 @@ impl<'a> DisplayList<'a> {
408408

409409
if let Some((col, row)) = pos {
410410
format_repeat_char(' ', lineno_width, f)?;
411-
lineno_color.paint(header_sigil).fmt(f)?;
411+
write!(
412+
f,
413+
"{}{}{}",
414+
lineno_color.render(),
415+
header_sigil,
416+
lineno_color.render_reset()
417+
)?;
412418
f.write_char(' ')?;
413419
path.fmt(f)?;
414420
f.write_char(':')?;
@@ -417,7 +423,13 @@ impl<'a> DisplayList<'a> {
417423
row.fmt(f)
418424
} else {
419425
format_repeat_char(' ', lineno_width, f)?;
420-
lineno_color.paint(header_sigil).fmt(f)?;
426+
write!(
427+
f,
428+
"{}{}{}",
429+
lineno_color.render(),
430+
header_sigil,
431+
lineno_color.render_reset()
432+
)?;
421433
f.write_char(' ')?;
422434
path.fmt(f)
423435
}
@@ -434,7 +446,12 @@ impl<'a> DisplayList<'a> {
434446
let lineno_color = self.stylesheet.line_no();
435447
format_repeat_char(' ', lineno_width, f)?;
436448
f.write_char(' ')?;
437-
lineno_color.paint("=").fmt(f)?;
449+
write!(
450+
f,
451+
"{}={}",
452+
lineno_color.render(),
453+
lineno_color.render_reset()
454+
)?;
438455
f.write_char(' ')?;
439456
}
440457
}
@@ -459,22 +476,18 @@ impl<'a> DisplayList<'a> {
459476
} => {
460477
let lineno_color = self.stylesheet.line_no();
461478
if self.anonymized_line_numbers && lineno.is_some() {
462-
lineno_color
463-
.paint_fn(Box::new(|f: &mut fmt::Formatter<'_>| {
464-
f.write_str(Self::ANONYMIZED_LINE_NUM)?;
465-
f.write_str(" |")
466-
}))
467-
.fmt(f)?;
479+
write!(f, "{}", lineno_color.render())?;
480+
f.write_str(Self::ANONYMIZED_LINE_NUM)?;
481+
f.write_str(" |")?;
482+
write!(f, "{}", lineno_color.render_reset())?;
468483
} else {
469-
lineno_color
470-
.paint_fn(Box::new(|f: &mut fmt::Formatter<'_>| {
471-
match lineno {
472-
Some(n) => write!(f, "{:>width$}", n, width = lineno_width),
473-
None => format_repeat_char(' ', lineno_width, f),
474-
}?;
475-
f.write_str(" |")
476-
}))
477-
.fmt(f)?;
484+
write!(f, "{}", lineno_color.render())?;
485+
match lineno {
486+
Some(n) => write!(f, "{:>width$}", n, width = lineno_width),
487+
None => format_repeat_char(' ', lineno_width, f),
488+
}?;
489+
f.write_str(" |")?;
490+
write!(f, "{}", lineno_color.render_reset())?;
478491
}
479492
if *line != DisplaySourceLine::Empty {
480493
if !inline_marks.is_empty() || 0 < inline_marks_width {
@@ -508,14 +521,13 @@ impl<'a> DisplayList<'a> {
508521
) -> fmt::Result {
509522
format_repeat_char(' ', inline_marks_width - inline_marks.len(), f)?;
510523
for mark in inline_marks {
511-
self.get_annotation_style(&mark.annotation_type)
512-
.paint_fn(Box::new(|f: &mut fmt::Formatter<'_>| {
513-
f.write_char(match mark.mark_type {
514-
DisplayMarkType::AnnotationThrough => '|',
515-
DisplayMarkType::AnnotationStart => '/',
516-
})
517-
}))
518-
.fmt(f)?;
524+
let annotation_style = self.get_annotation_style(&mark.annotation_type);
525+
write!(f, "{}", annotation_style.render())?;
526+
f.write_char(match mark.mark_type {
527+
DisplayMarkType::AnnotationThrough => '|',
528+
DisplayMarkType::AnnotationStart => '/',
529+
})?;
530+
write!(f, "{}", annotation_style.render_reset())?;
519531
}
520532
Ok(())
521533
}

‎src/renderer/mod.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use crate::display_list::{DisplayList, Margin};
22
use crate::snippet::Snippet;
3+
use anstyle::{AnsiColor, Effects, Style};
34
use std::fmt::Display;
4-
use yansi_term::Color::Fixed;
5-
use yansi_term::Style;
65

76
#[derive(Clone)]
87
pub struct Renderer {
@@ -36,13 +35,13 @@ impl Renderer {
3635
anonymized_line_numbers: false,
3736
margin: None,
3837
stylesheet: StyleSheet {
39-
error: Fixed(9).bold(),
40-
warning: Fixed(11).bold(),
41-
info: Fixed(12).bold(),
42-
note: Style::new().bold(),
43-
help: Fixed(14).bold(),
44-
line_no: Fixed(12).bold(),
45-
emphasis: Style::new().bold(),
38+
error: AnsiColor::BrightRed.on_default().effects(Effects::BOLD),
39+
warning: AnsiColor::BrightYellow.on_default().effects(Effects::BOLD),
40+
info: AnsiColor::BrightBlue.on_default().effects(Effects::BOLD),
41+
note: Style::new().effects(Effects::BOLD),
42+
help: AnsiColor::BrightCyan.on_default().effects(Effects::BOLD),
43+
line_no: AnsiColor::BrightBlue.on_default().effects(Effects::BOLD),
44+
emphasis: Style::new().effects(Effects::BOLD),
4645
none: Style::new(),
4746
},
4847
}

‎tests/diff/mod.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use anstyle::{AnsiColor, Color, Style};
12
use difference::{Changeset, Difference};
2-
use yansi_term::Color::{Black, Green, Red};
33

4+
const GREEN: Style = AnsiColor::Green.on_default();
45
pub fn get_diff(left: &str, right: &str) -> String {
56
let mut output = String::new();
67

@@ -14,28 +15,47 @@ pub fn get_diff(left: &str, right: &str) -> String {
1415
Difference::Add(ref x) => {
1516
match diffs[i - 1] {
1617
Difference::Rem(ref y) => {
17-
output += &format!("{}", Green.paint("+"));
18+
output += &format!("{}+{}", GREEN.render(),GREEN.render_reset());
1819
let Changeset { diffs, .. } = Changeset::new(y, x, " ");
1920
for c in diffs {
2021
match c {
2122
Difference::Same(ref z) => {
22-
output += &format!("{} ", Green.paint(z.as_str()));
23+
output += &format!(
24+
"{}{}{} ",
25+
GREEN.render(),
26+
z.as_str(),
27+
GREEN.render_reset()
28+
);
2329
}
2430
Difference::Add(ref z) => {
25-
output += &format!("{} ", Black.on(Green).paint(z.as_str()));
31+
let black_on_green = Style::new()
32+
.bg_color(Some(Color::Ansi(AnsiColor::Green)))
33+
.fg_color(Some(Color::Ansi(AnsiColor::Black)));
34+
output += &format!(
35+
"{}{}{} ",
36+
black_on_green.render(),
37+
z.as_str(),
38+
black_on_green.render_reset()
39+
);
2640
}
2741
_ => (),
2842
}
2943
}
3044
output += "\n";
3145
}
3246
_ => {
33-
output += &format!("+{}\n", Green.paint(x.as_str()));
47+
output += &format!(
48+
"+{}{}{}\n",
49+
GREEN.render(),
50+
x.as_str(),
51+
GREEN.render_reset()
52+
);
3453
}
3554
};
3655
}
3756
Difference::Rem(ref x) => {
38-
output += &format!("-{}\n", Red.paint(x.as_str()));
57+
let red = AnsiColor::Red.on_default();
58+
output += &format!("-{}{}{}\n", red.render(), x.as_str(), red.render_reset());
3959
}
4060
}
4161
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /