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

Commitf1074b9

Browse files
committed
Start re-adding styling information
1 parent4cbb80a commitf1074b9

File tree

10 files changed

+149
-58
lines changed

10 files changed

+149
-58
lines changed

‎Cargo.toml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ edition = "2018"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10+
ansi_term = {version ="0.12",optional =true }
1011

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

15+
[features]
16+
default = []
17+
color = ["ansi_term"]
18+
1419
[[bench]]
1520
name ="simple"
16-
harness =false
21+
harness =false

‎benches/simple.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use criterion::Criterion;
66

77
use std::fmt::Write;
88

9+
use annotate_snippets::DisplayList;
910
use annotate_snippets::{Annotation,AnnotationType,SourceAnnotation};
1011
use annotate_snippets::{Slice,Snippet};
11-
use annotate_snippets::DisplayList;
1212

1313
constSOURCE:&'staticstr =r#") -> Option<String> {
1414
for ann in annotations {

‎src/annotation.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub struct Annotation<'s> {
77

88
#[derive(Debug,Clone)]
99
pubenumAnnotationType{
10+
None,
1011
Error,
1112
Warning,
1213
Info,

‎src/display_list/annotation.rs‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1+
usecrate::annotation::AnnotationType;
2+
usecrate::styles::Stylesheet;
13
use std::fmt;
24

35
#[derive(Debug,Clone)]
46
pubstructAnnotation<'d>{
5-
pubannotation_type:DisplayAnnotationType,
7+
pubannotation_type:AnnotationType,
68
pubid:Option<&'dstr>,
79
publabel:&'dstr,
810
}
911

10-
impl<'d> fmt::DisplayforAnnotation<'d>{
11-
fnfmt(&self,f:&mut fmt::Formatter<'_>) -> fmt::Result{
12-
f.write_str(self.label)
12+
impl<'d>Annotation<'d>{
13+
pubfnfmt_with_style(
14+
&self,
15+
f:&mut fmt::Formatter<'_>,
16+
style:&implStylesheet,
17+
) -> fmt::Result{
18+
style.format(f,&self.annotation_type,self.label)
19+
// f.write_str(self.label)
1320
}
1421
}
1522

16-
#[derive(Debug,Clone)]
17-
pubenumDisplayAnnotationType{
18-
None,
19-
Error,
20-
Warning,
21-
Info,
22-
Note,
23-
Help,
24-
}
25-
26-
impl fmt::DisplayforDisplayAnnotationType{
23+
impl fmt::DisplayforAnnotationType{
2724
fnfmt(&self,f:&mut fmt::Formatter<'_>) -> fmt::Result{
2825
matchself{
2926
Self::None =>Ok(()),

‎src/display_list/line.rs‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::fmt;
2-
use std::fmt::Display;
32
use std::fmt::Write;
43

5-
usesuper::annotation::{Annotation,DisplayAnnotationType};
4+
usesuper::annotation::Annotation;
5+
usecrate::annotation::AnnotationType;
6+
usecrate::styles::Stylesheet;
67

78
#[derive(Debug,Clone)]
89
pubenumDisplayLine<'d>{
@@ -15,9 +16,10 @@ pub enum DisplayLine<'d> {
1516
}
1617

1718
impl<'d>DisplayLine<'d>{
18-
pubfnfmt(
19+
pubfnfmt_with_style(
1920
&self,
2021
f:&mut fmt::Formatter<'_>,
22+
style:&implStylesheet,
2123
lineno_max:Option<usize>,
2224
inline_marks_width:usize,
2325
) -> fmt::Result{
@@ -38,9 +40,10 @@ impl<'d> DisplayLine<'d> {
3840
for markin inline_marks{
3941
write!(f,"{}", mark)?;
4042
}
41-
writeln!(f,"{}", line)
43+
line.fmt_with_style(f, style)?;
44+
f.write_char('\n')
4245
}
43-
Self::Raw(dl) => dl.fmt(f, lineno_max),
46+
Self::Raw(dl) => dl.fmt_with_style(f, style, lineno_max),
4447
}
4548
}
4649
}
@@ -57,8 +60,8 @@ pub enum DisplaySourceLine<'d> {
5760
Empty,
5861
}
5962

60-
impl<'d>fmt::DisplayforDisplaySourceLine<'d>{
61-
fnfmt(&self,f:&mut fmt::Formatter<'_>) -> fmt::Result{
63+
impl<'d>DisplaySourceLine<'d>{
64+
fnfmt_with_style(&self,f:&mut fmt::Formatter<'_>,style:&implStylesheet) -> fmt::Result{
6265
matchself{
6366
Self::Content{ text} =>{
6467
f.write_char(' ')?;
@@ -76,7 +79,7 @@ impl<'d> fmt::Display for DisplaySourceLine<'d> {
7679
write!(f,"{:->1$}","", end - start)?;
7780
}
7881
f.write_char(' ')?;
79-
annotation.fmt(f)
82+
annotation.fmt_with_style(f, style)
8083
}
8184
Self::Empty =>Ok(()),
8285
}
@@ -97,7 +100,12 @@ pub enum DisplayRawLine<'d> {
97100
}
98101

99102
impl<'d>DisplayRawLine<'d>{
100-
fnfmt(&self,f:&mut fmt::Formatter<'_>,lineno_max:usize) -> fmt::Result{
103+
fnfmt_with_style(
104+
&self,
105+
f:&mut fmt::Formatter<'_>,
106+
style:&implStylesheet,
107+
lineno_max:usize,
108+
) -> fmt::Result{
101109
matchself{
102110
Self::Origin{ path, pos} =>{
103111
write!(f,"{:>1$}","", lineno_max)?;
@@ -108,7 +116,7 @@ impl<'d> DisplayRawLine<'d> {
108116
f.write_char('\n')
109117
}
110118
Self::Annotation{ annotation, ..} =>{
111-
annotation.annotation_type.fmt(f)?;
119+
style.format(f,&annotation.annotation_type,&annotation.annotation_type)?;
112120
ifletSome(id) = annotation.id{
113121
write!(f,"[{}]", id)?;
114122
}
@@ -121,7 +129,7 @@ impl<'d> DisplayRawLine<'d> {
121129
#[derive(Debug,Clone)]
122130
pubstructDisplayMark{
123131
pubmark_type:DisplayMarkType,
124-
pubannotation_type:DisplayAnnotationType,
132+
pubannotation_type:AnnotationType,
125133
}
126134

127135
impl fmt::DisplayforDisplayMark{

‎src/display_list/list.rs‎

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
usesuper::annotation::{Annotation,DisplayAnnotationType};
1+
usesuper::annotation::Annotation;
22
usesuper::line::{DisplayLine,DisplayMark,DisplayMarkType,DisplayRawLine,DisplaySourceLine};
3+
usecrate::annotation::AnnotationType;
4+
usecrate::styles::{get_stylesheet,Stylesheet};
35
usecrate::{Slice,Snippet,SourceAnnotation};
46
use std::cmp;
57
use std::fmt;
@@ -9,6 +11,34 @@ pub struct DisplayList<'d> {
911
pubbody:Vec<DisplayLine<'d>>,
1012
}
1113

14+
impl<'d>DisplayList<'d>{
15+
pubfnfmt_with_style(
16+
&self,
17+
f:&mut fmt::Formatter<'_>,
18+
style:&implStylesheet,
19+
) -> fmt::Result{
20+
let lineno_max =self.body.iter().rev().find_map(|line|{
21+
ifletDisplayLine::Source{
22+
lineno:Some(lineno),
23+
..
24+
} = line
25+
{
26+
Some(digits(*lineno))
27+
}else{
28+
None
29+
}
30+
});
31+
let inline_marks_width =self.body.iter().fold(0, |max, line|match line{
32+
DisplayLine::Source{ inline_marks, ..} => cmp::max(inline_marks.len(), max),
33+
_ => max,
34+
});
35+
for linein&self.body{
36+
line.fmt_with_style(f, style, lineno_max, inline_marks_width)?
37+
}
38+
Ok(())
39+
}
40+
}
41+
1242
fnget_header_pos(slice:&Slice) ->(Option<usize>,Option<usize>){
1343
let line = slice.line_start;
1444
(line,None)
@@ -19,10 +49,10 @@ impl<'d> From<&Snippet<'d>> for DisplayList<'d> {
1949
letmut body =vec![];
2050

2151
ifletSome(annotation) =&snippet.title{
22-
let label = annotation.label.clone().unwrap_or_default();
52+
let label = annotation.label.unwrap_or_default();
2353
body.push(DisplayLine::Raw(DisplayRawLine::Annotation{
2454
annotation:Annotation{
25-
annotation_type:DisplayAnnotationType::Error,
55+
annotation_type:AnnotationType::Error,
2656
id: annotation.id,
2757
label:&label,
2858
},
@@ -79,15 +109,15 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
79109
// Annotation starts in this line
80110
inline_marks.push(DisplayMark{
81111
mark_type:DisplayMarkType::AnnotationStart,
82-
annotation_type:DisplayAnnotationType::Error,
112+
annotation_type:AnnotationType::Error,
83113
});
84114
true
85115
}elseif ann.range.0 < line_start_pos && ann.range.1 > line_start_pos + line_length
86116
{
87117
// Annotation goes through this line
88118
inline_marks.push(DisplayMark{
89119
mark_type:DisplayMarkType::AnnotationThrough,
90-
annotation_type:DisplayAnnotationType::Error,
120+
annotation_type:AnnotationType::Error,
91121
});
92122
true
93123
}elseif ann.range.0 < line_start_pos
@@ -97,7 +127,7 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
97127
// Annotation ends on this line
98128
inline_marks.push(DisplayMark{
99129
mark_type:DisplayMarkType::AnnotationThrough,
100-
annotation_type:DisplayAnnotationType::Error,
130+
annotation_type:AnnotationType::Error,
101131
});
102132
current_annotations.push(*ann);
103133
false
@@ -120,7 +150,7 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
120150
let inline_marks =if ann.range.0 < line_start_pos{
121151
vec![DisplayMark{
122152
mark_type:DisplayMarkType::AnnotationThrough,
123-
annotation_type:DisplayAnnotationType::Error,
153+
annotation_type:AnnotationType::Error,
124154
}]
125155
}else{
126156
vec![]
@@ -130,7 +160,7 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
130160
inline_marks,
131161
line:DisplaySourceLine::Annotation{
132162
annotation:Annotation{
133-
annotation_type:DisplayAnnotationType::Error,
163+
annotation_type:AnnotationType::Error,
134164
id:None,
135165
label: ann.label,
136166
},
@@ -152,36 +182,19 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
152182
}
153183
}
154184

155-
fndigits(n:&usize) ->usize{
156-
letmut n = n.clone();
185+
fndigits(n:usize) ->usize{
186+
letmut n = n;
157187
letmut sum =0;
158188
while n !=0{
159-
n= n /10;
189+
n/=10;
160190
sum +=1;
161191
}
162192
sum
163193
}
164194

165195
impl<'d> fmt::DisplayforDisplayList<'d>{
166196
fnfmt(&self,f:&mut fmt::Formatter<'_>) -> fmt::Result{
167-
let lineno_max =self.body.iter().rev().find_map(|line|{
168-
ifletDisplayLine::Source{
169-
lineno:Some(lineno),
170-
..
171-
} = line
172-
{
173-
Some(digits(lineno))
174-
}else{
175-
None
176-
}
177-
});
178-
let inline_marks_width =self.body.iter().fold(0, |max, line|match line{
179-
DisplayLine::Source{ inline_marks, ..} => cmp::max(inline_marks.len(), max),
180-
_ => max,
181-
});
182-
for linein&self.body{
183-
line.fmt(f, lineno_max, inline_marks_width)?
184-
}
185-
Ok(())
197+
let style =get_stylesheet();
198+
self.fmt_with_style(f,&style)
186199
}
187200
}

‎src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub mod annotation;
22
mod display_list;
33
pubmod slice;
44
pubmod snippet;
5+
pubmod styles;
56

67
pubuse annotation::{Annotation,AnnotationType,SourceAnnotation};
78
pubuse display_list::DisplayList;

‎src/styles/color.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
usesuper::Stylesheet;
2+
usecrate::AnnotationType;
3+
use std::fmt;
4+
use std::fmt::Display;
5+
6+
use ansi_term::Colour::{Red,Yellow};
7+
8+
#[derive(Default)]
9+
pubstructStylesheetColor{}
10+
11+
implStylesheetforStylesheetColor{
12+
fnformat(
13+
&self,
14+
f:&mut fmt::Formatter,
15+
annotation_type:&AnnotationType,
16+
value:implDisplay,
17+
) -> fmt::Result{
18+
match annotation_type{
19+
AnnotationType::Error =>write!(f,"{}",Red.paint(value.to_string()).to_string()),
20+
AnnotationType::Warning =>write!(f,"{}",Yellow.paint(value.to_string()).to_string()),
21+
_ =>write!(f,"{}", value),
22+
}
23+
}
24+
}

‎src/styles/mod.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
usecrate::AnnotationType;
2+
use std::fmt;
3+
use std::fmt::Display;
4+
5+
#[cfg(feature ="ansi_term")]
6+
pubmod color;
7+
pubmod plain;
8+
9+
pubtraitStylesheet{
10+
fnformat(
11+
&self,
12+
f:&mut fmt::Formatter,
13+
annotation_type:&AnnotationType,
14+
value:implDisplay,
15+
) -> fmt::Result;
16+
}
17+
18+
pubfnget_stylesheet() ->implStylesheet{
19+
#[cfg(feature ="ansi_term")]
20+
return color::StylesheetColor::default();
21+
22+
#[cfg(not(feature ="ansi_term"))]
23+
return plain::StylesheetPlain::default();
24+
}

‎src/styles/plain.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
usesuper::Stylesheet;
2+
usecrate::AnnotationType;
3+
use std::fmt;
4+
use std::fmt::Display;
5+
6+
#[derive(Default)]
7+
pubstructStylesheetPlain{}
8+
9+
implStylesheetforStylesheetPlain{
10+
fnformat(
11+
&self,
12+
f:&mut fmt::Formatter,
13+
_annotation_type:&AnnotationType,
14+
value:implDisplay,
15+
) -> fmt::Result{
16+
write!(f,"{}", value)
17+
}
18+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp