8000 Add renderer and styles · BurntSushi/annotate-snippets-rs@3b921bc · GitHub
[go: up one dir, main page]

Skip to content

Commit 3b921bc

Browse files
committed
Add renderer and styles
1 parent c56386c commit 3b921bc

File tree

14 files changed

+153
-13
lines changed

14 files changed

+153
-13
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ edition = "2018"
88

99
[dependencies]
1010
ansi_term = { version = "0.12", optional = true }
11+
termcolor = { version = "1", optional = true }
1112

1213
[dev-dependencies]
1314
criterion = "0.3"
1415

1516
[features]
1617
default = []
1718
color = ["ansi_term"]
19+
color2 = ["termcolor"]
1820

1921
[[bench]]
2022
name = "simple"

examples/format.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
#[cfg(feature = "ansi_term")]
2+
use annotate_snippets::renderers::ascii_default::styles::color::Style as ColorStyle;
3+
#[cfg(feature = "termcolor")]
4+
use annotate_snippets::renderers::ascii_default::styles::color2::Style as ColorStyle;
5+
#[cfg(all(not(feature = "ansi_term"), not(feature = "termcolor")))]
6+
use annotate_snippets::renderers::ascii_default::styles::plain::Style as PlainStyle;
7+
use annotate_snippets::renderers::ascii_default::Renderer as AsciiRenderer;
8+
use annotate_snippets::DisplayList;
19
use annotate_snippets::{Annotation, AnnotationType, SourceAnnotation};
210
use annotate_snippets::{Slice, Snippet};
311

@@ -50,5 +58,15 @@ fn main() {
5058
],
5159
}],
5260
};
53-
println!("{}", snippet);
61+
let dl = DisplayList::from(&snippet);
62+
63+
#[cfg(all(not(feature = "ansi_term"), not(feature = "termcolor")))]
64+
let r = AsciiRenderer::<PlainStyle>::new();
65+
#[cfg(feature = "ansi_term")]
66+
let r = AsciiRenderer::<ColorStyle>::new();
67+
#[cfg(feature = "termcolor")]
68+
let r = AsciiRenderer::<ColorStyle>::new();
69+
let mut s: Vec<u8> = Vec::new();
70+
r.fmt(&mut s, &dl).unwrap();
71+
println!("{}", std::str::from_utf8(&s).unwrap());
5472
}

src/display_list/line.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ impl<'d> DisplayRawLine<'d> {
118118
Self::Annotation { annotation, .. } => {
119119
style.format(
120120
f,
121+
format_args!("{}", annotation.annotation_type),
121122
&[
122123
StyleClass::TitleLineAnnotationType,
123124
StyleClass::AnnotationTypeError,
124125
],
125-
&annotation.annotation_type,
126126
)?;
127127
if let Some(id) = annotation.id {
128128
write!(f, "[{}]", id)?;

src/display_list/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
mod annotation;
2-
mod line;
3-
mod list;
1+
pub mod annotation;
2+
pub mod line;
3+
pub mod list;
44

55
pub use list::DisplayList;

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod annotation;
22
mod display_list;
3+
pub mod renderers;
34
pub mod slice;
45
pub mod snippet;
56
pub mod styles;

src/renderers/ascii_default/mod.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
pub mod styles;
2+
3+
use super::Renderer as RendererTrait;
4+
use crate::display_list::line::DisplayLine;
5+
use crate::display_list::line::DisplayRawLine;
6+
use crate::DisplayList;
7+
use std::io::Write;
8+
use std::marker::PhantomData;
9+
use styles::Style as StyleTrait;
10+
11+
pub struct Renderer<S: StyleTrait> {
12+
style: PhantomData<S>,
13+
}
14+
15+
impl<S: StyleTrait> Renderer<S> {
16+
pub fn new() -> Self {
17+
Renderer { style: PhantomData }
18+
}
19+
20+
pub fn fmt(&self, w: &mut impl Write, dl: &DisplayList) -> std::io::Result<()> {
21+
for line in &dl.body {
22+
self.fmt_line(w, line)?;
23+
}
24+
Ok(())
25+
}
26+
27+
fn fmt_line(&self, w: &mut impl Write, line: &DisplayLine) -> std::io::Result<()> {
28+
match line {
29+
DisplayLine::Raw(l) => self.fmt_raw_line(w, l),
30+
_ => Ok(()),
31+
}
32+
}
33+
34+
fn fmt_raw_line(
35+
&self,
36+
w: &mut impl std::io::Write,
37+
line: &DisplayRawLine,
38+
) -> std::io::Result<()> {
39+
match line {
40+
DisplayRawLine::Origin { path, .. } => {
41+
let _lineno_max = 1;
42+
S::fmt(w, path)
43+
//write!(w, "{:>1$}", "", lineno_max)?;
44+
//write!(w, "--> {}", path)?;
45+
//if let Some(line) = pos.0 {
46+
//write!(w, ":{}", line)?;
47+
//}
48+
//w.write_char('\n')
49+
}
50+
_ => Ok(()),
51+
}
52+
}
53+
}
54+
55+
impl<S: StyleTrait> RendererTrait for Renderer<S> {
56+
fn fmt(&self, w: &mut impl Write, dl: &DisplayList) -> std::io::Result<()> {
57+
Renderer::fmt(self, w, dl)
58+
}
59+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use ansi_term::Style as AnsiTermStyle;
2+
3+
use super::Style as StyleTrait;
4+
5+
use std::fmt;
6+
7+
pub struct Style {}
8+
9+
impl StyleTrait for Style {
10+
fn fmt(w: &mut dyn fmt::Write, pattern: impl fmt::Display) -> fmt::Result<()> {
11+
let style = AnsiTermStyle::new().bold();
12+
write!(w, "{}", style.paint(pattern.to_string()))
13+
}
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use termcolor::{Ansi, ColorSpec, WriteColor};
2+
3+
use super::Style as StyleTrait;
4+
5+
use std::fmt;
6+
use std::io::Write;
7+
8+
pub struct Style {}
9+
10+
impl StyleTrait for Style {
11+
fn fmt(w: &mut dyn std::io::Write, pattern: impl fmt::Display) -> std::io::Result<()> {
12+
let mut ansi = Ansi::new(w);
13+
ansi.set_color(ColorSpec::new().set_bold(true)).unwrap();
14+
write!(ansi, "{}", pattern)?;
15+
ansi.reset().unwrap();
16+
Ok(())
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#[cfg(feature = "ansi_term")]
2+
pub mod color;
3+
#[cfg(feature = "termcolor")]
4+
pub mod color2;
5+
pub mod plain;
6+
7+
use std::fmt;
8+
9+
pub trait Style {
10+
fn fmt(w: &mut dyn std::io::Write, pattern: impl fmt::Display) -> std::io::Result<()>;
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use super::Style as StyleTrait;
2+
3+
use std::fmt;
4+
5+
pub struct Style {}
6+
7+
impl StyleTrait for Style {
8+
fn fmt(w: &mut dyn std::io::Write, pattern: impl fmt::Display) -> std::io::Result<()> {
9+
write!(w, "{}", pattern)
10+
}
11+
}

0 commit comments

Comments
 (0)
0