Movatterモバイル変換


[0]ホーム

URL:


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

Commit3b921bc

Browse files
committed
Add renderer and styles
1 parentc56386c commit3b921bc

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::StyleasColorStyle;
3+
#[cfg(feature ="termcolor")]
4+
use annotate_snippets::renderers::ascii_default::styles::color2::StyleasColorStyle;
5+
#[cfg(all(not(feature ="ansi_term"), not(feature ="termcolor")))]
6+
use annotate_snippets::renderers::ascii_default::styles::plain::StyleasPlainStyle;
7+
use annotate_snippets::renderers::ascii_default::RendererasAsciiRenderer;
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+
letmut 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
ifletSome(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+
pubmod annotation;
2+
pubmod line;
3+
pubmod list;
44

55
pubuse list::DisplayList;

‎src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pubmod annotation;
22
mod display_list;
3+
pubmod renderers;
34
pubmod slice;
45
pubmod snippet;
56
pubmod 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+
pubmod styles;
2+
3+
usesuper::RendererasRendererTrait;
4+
usecrate::display_list::line::DisplayLine;
5+
usecrate::display_list::line::DisplayRawLine;
6+
usecrate::DisplayList;
7+
use std::io::Write;
8+
use std::marker::PhantomData;
9+
use styles::StyleasStyleTrait;
10+
11+
pubstructRenderer<S:StyleTrait>{
12+
style:PhantomData<S>,
13+
}
14+
15+
impl<S:StyleTrait>Renderer<S>{
16+
pubfnnew() ->Self{
17+
Renderer{style:PhantomData}
18+
}
19+
20+
pubfnfmt(&self,w:&mutimplWrite,dl:&DisplayList) -> std::io::Result<()>{
21+
for linein&dl.body{
22+
self.fmt_line(w, line)?;
23+
}
24+
Ok(())
25+
}
26+
27+
fnfmt_line(&self,w:&mutimplWrite,line:&DisplayLine) -> std::io::Result<()>{
28+
match line{
29+
DisplayLine::Raw(l) =>self.fmt_raw_line(w, l),
30+
_ =>Ok(()),
31+
}
32+
}
33+
34+
fnfmt_raw_line(
35+
&self,
36+
w:&mutimpl 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>RendererTraitforRenderer<S>{
56+
fnfmt(&self,w:&mutimplWrite,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::StyleasAnsiTermStyle;
2+
3+
usesuper::StyleasStyleTrait;
4+
5+
use std::fmt;
6+
7+
pubstructStyle{}
8+
9+
implStyleTraitforStyle{
10+
fnfmt(w:&mutdyn 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+
usesuper::StyleasStyleTrait;
4+
5+
use std::fmt;
6+
use std::io::Write;
7+
8+
pubstructStyle{}
9+
10+
implStyleTraitforStyle{
11+
fnfmt(w:&mutdyn std::io::Write,pattern:impl fmt::Display) -> std::io::Result<()>{
12+
letmut 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+
pubmod color;
3+
#[cfg(feature ="termcolor")]
4+
pubmod color2;
5+
pubmod plain;
6+
7+
use std::fmt;
8+
9+
pubtraitStyle{
10+
fnfmt(w:&mutdyn 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+
usesuper::StyleasStyleTrait;
2+
3+
use std::fmt;
4+
5+
pubstructStyle{}
6+
7+
implStyleTraitforStyle{
8+
fnfmt(w:&mutdyn std::io::Write,pattern:impl fmt::Display) -> std::io::Result<()>{
9+
write!(w,"{}", pattern)
10+
}
11+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp