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 dfd4e87

Browse files
committed
fix!: Switch to anstyle for color
BREAKING CHANGE: This removes `color` feature
1 parent 4affdfb commit dfd4e87

File tree

6 files changed

+109
-91
lines changed

6 files changed

+109
-91
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: 72 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +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;
3938

40-
use crate::renderer::stylesheet::Stylesheet;
39+
use crate::renderer::{stylesheet::Stylesheet,Style};
4140

4241
/// List of lines to be displayed.
4342
pub struct DisplayList<'a> {
@@ -204,7 +203,15 @@ impl<'a> DisplayList<'a> {
204203
for fragment in label {
205204
match fragment.style {
206205
DisplayTextStyle::Regular => fragment.content.fmt(f)?,
207-
DisplayTextStyle::Emphasis => emphasis_style.paint(fragment.content).fmt(f)?,
206+
DisplayTextStyle::Emphasis => {
207+
write!(
208+
f,
209+
"{}{}{}",
210+
emphasis_style.render(),
211+
fragment.content,
212+
emphasis_style.render_reset()
213+
)?;
214+
}
208215
}
209216
}
210217
Ok(())
@@ -231,26 +238,21 @@ impl<'a> DisplayList<'a> {
231238
if formatted_len == 0 {
232239
self.format_label(&annotation.label, f)
233240
} 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)?;
241+
write!(f, "{}", color.render())?;
242+
Self::format_annotation_type(&annotation.annotation_type, f)?;
243+
if let Some(id) = &annotation.id {
244+
f.write_char('[')?;
245+
f.write_str(id)?;
246+
f.write_char(']')?;
247+
}
248+
write!(f, "{}", color.render_reset())?;
245249

246250
if !is_annotation_empty(annotation) {
247251
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)?;
252+
write!(f, "{}", color.render())?;
253+
f.write_str(": ")?;
254+
self.format_label(&annotation.label, f)?;
255+
write!(f, "{}", color.render_reset())?;
254256
} else {
255257
f.write_str(": ")?;
256258
self.format_label(&annotation.label, f)?;
@@ -361,25 +363,21 @@ impl<'a> DisplayList<'a> {
361363
_ => range.0,
362364
};
363365

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)?;
366+
write!(f, "{}", color.render())?;
367+
format_repeat_char(indent_char, indent_length + 1, f)?;
368+
format_repeat_char(mark, range.1 - indent_length, f)?;
369+
write!(f, "{}", color.render_reset())?;
370370

371371
if !is_annotation_empty(annotation) {
372372
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)?;
373+
write!(f, "{}", color.render())?;
374+
self.format_annotation(
375+
annotation,
376+
annotation_part == &DisplayAnnotationPart::LabelContinuation,
377+
true,
378+
f,
379+
)?;
380+
write!(f, "{}", color.render_reset())?;
383381
}
384382

385383
Ok(())
@@ -408,7 +406,13 @@ impl<'a> DisplayList<'a> {
408406

409407
if let Some((col, row)) = pos {
410408
format_repeat_char(' ', lineno_width, f)?;
411-
lineno_color.paint(header_sigil).fmt(f)?;
409+
write!(
410+
f,
411+
"{}{}{}",
412+
lineno_color.render(),
413+
header_sigil,
414+
lineno_color.render_reset()
415+
)?;
412416
f.write_char(' ')?;
413417
path.fmt(f)?;
414418
f.write_char(':')?;
@@ -417,7 +421,13 @@ impl<'a> DisplayList<'a> {
417421
row.fmt(f)
418422
} else {
419423
format_repeat_char(' ', lineno_width, f)?;
420-
lineno_color.paint(header_sigil).fmt(f)?;
424+
write!(
425+
f,
426+
"{}{}{}",
427+
lineno_color.render(),
428+
header_sigil,
429+
lineno_color.render_reset()
430+
)?;
421431
f.write_char(' ')?;
422432
path.fmt(f)
423433
}
@@ -434,7 +444,12 @@ impl<'a> DisplayList<'a> {
434444
let lineno_color = self.stylesheet.line_no();
435445
format_repeat_char(' ', lineno_width, f)?;
436446
f.write_char(' ')?;
437-
lineno_color.paint("=").fmt(f)?;
447+
write!(
448+
f,
449+
"{}={}",
450+
lineno_color.render(),
451+
lineno_color.render_reset()
452+
)?;
438453
f.write_char(' ')?;
439454
}
440455
}
@@ -459,22 +474,18 @@ impl<'a> DisplayList<'a> {
459474
} => {
460475
let lineno_color = self.stylesheet.line_no();
461476
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)?;
477+
write!(f, "{}", lineno_color.render())?;
478+
f.write_str(Self::ANONYMIZED_LINE_NUM)?;
479+
f.write_str(" |")?;
480+
write!(f, "{}", lineno_color.render_reset())?;
468481
} 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)?;
482+
write!(f, "{}", lineno_color.render())?;
483+
match lineno {
484+
Some(n) => write!(f, "{:>width$}", n, width = lineno_width),
485+
None => format_repeat_char(' ', lineno_width, f),
486+
}?;
487+
f.write_str(" |")?;
488+
write!(f, "{}", lineno_color.render_reset())?;
478489
}
479490
if *line != DisplaySourceLine::Empty {
480491
if !inline_marks.is_empty() || 0 < inline_marks_width {
@@ -508,14 +519,13 @@ impl<'a> DisplayList<'a> {
508519
) -> fmt::Result {
509520
format_repeat_char(' ', inline_marks_width - inline_marks.len(), f)?;
510521
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)?;
522+
let annotation_style = self.get_annotation_style(&mark.annotation_type);
523+
write!(f, "{}", annotation_style.render())?;
524+
f.write_char(match mark.mark_type {
525+
DisplayMarkType::AnnotationThrough => '|',
526+
DisplayMarkType::AnnotationStart => '/',
527+
})?;
528+
write!(f, "{}", annotation_style.render_reset())?;
519529
}
520530
Ok(())
521531
}

‎src/renderer/mod.rs‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ pub mod stylesheet;
22

33
use crate::display_list::{DisplayList, Margin};
44
use crate::snippet::Snippet;
5+
pub use anstyle::*;
56
use std::fmt::Display;
67
use stylesheet::Stylesheet;
7-
use yansi_term::Color::Fixed;
8-
use yansi_term::Style;
98

109
#[derive(Clone)]
1110
pub struct Renderer {
@@ -28,13 +27,13 @@ impl Renderer {
2827
pub fn styled() -> Self {
2928
Self {
3029
stylesheet: Stylesheet {
31-
error: Fixed(9).bold(),
32-
warning: Fixed(11).bold(),
33-
info: Fixed(12).bold(),
34-
note: Style::new().bold(),
35-
help: Fixed(14).bold(),
36-
line_no: Fixed(12).bold(),
37-
emphasis: Style::new().bold(),
30+
error: AnsiColor::BrightRed.on_default().effects(Effects::BOLD),
31+
warning: AnsiColor::BrightYellow.on_default().effects(Effects::BOLD),
32+
info: AnsiColor::BrightBlue.on_default().effects(Effects::BOLD),
33+
note: Style::new().effects(Effects::BOLD),
34+
help: AnsiColor::BrightCyan.on_default().effects(Effects::BOLD),
35+
line_no: AnsiColor::BrightBlue.on_default().effects(Effects::BOLD),
36+
emphasis: Style::new().effects(Effects::BOLD),
3837
none: Style::new(),
3938
},
4039
..Self::plain()

‎src/renderer/stylesheet.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use yansi_term::Style;
1+
use anstyle::Style;
22

33
#[derive(Clone, Copy, Debug)]
44
pub struct Stylesheet {

‎tests/diff/mod.rs‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use annotate_snippets::renderer::{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 によって変換されたページ (->オリジナル) /