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

Commit9b04c67

Browse files
committed
Example works with ansi_term
1 parent3a1bbc5 commit9b04c67

File tree

7 files changed

+139
-51
lines changed

7 files changed

+139
-51
lines changed

‎src/display_list/line.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
usesuper::annotation::Annotation;
22
usecrate::annotation::AnnotationType;
3+
use std::ops::Range;
34

45
#[derive(Debug,Clone)]
56
pubenumDisplayLine<'d>{
@@ -18,7 +19,7 @@ pub enum DisplaySourceLine<'d> {
1819
},
1920
Annotation{
2021
annotation:Annotation<'d>,
21-
range:(usize,usize),
22+
range:Range<usize>,
2223
},
2324
Empty,
2425
}

‎src/display_list/list.rs‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
usesuper::annotation::Annotation;
22
usesuper::line::{DisplayLine,DisplayMark,DisplayMarkType,DisplayRawLine,DisplaySourceLine};
3-
usecrate::annotation::AnnotationType;
43
usecrate::{Slice,Snippet,SourceAnnotation};
54

65
#[derive(Debug,Clone)]
@@ -21,7 +20,7 @@ impl<'d> From<&Snippet<'d>> for DisplayList<'d> {
2120
let label = annotation.label.unwrap_or_default();
2221
body.push(DisplayLine::Raw(DisplayRawLine::Annotation{
2322
annotation:Annotation{
24-
annotation_type:AnnotationType::Error,
23+
annotation_type:annotation.annotation_type.clone(),
2524
id: annotation.id,
2625
label:&label,
2726
},
@@ -62,7 +61,7 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
6261

6362
letmut i = slice.line_start.unwrap_or(1);
6463
for linein slice.source.lines(){
65-
let line_range = line_start_pos..(line_start_pos + line.chars().count());
64+
let line_range = line_start_pos..(line_start_pos + line.chars().count() +1);
6665

6766
letmut current_annotations =vec![];
6867
letmut inline_marks =vec![];
@@ -78,23 +77,21 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
7877
// Annotation starts in this line
7978
inline_marks.push(DisplayMark{
8079
mark_type:DisplayMarkType::AnnotationStart,
81-
annotation_type:AnnotationType::Error,
80+
annotation_type:ann.annotation_type.clone(),
8281
});
8382
true
8483
}elseif ann.range.start < line_range.start && ann.range.end > line_range.end{
8584
// Annotation goes through this line
8685
inline_marks.push(DisplayMark{
8786
mark_type:DisplayMarkType::AnnotationThrough,
88-
annotation_type:AnnotationType::Error,
87+
annotation_type:ann.annotation_type.clone(),
8988
});
9089
true
91-
}elseif line_range.contains(&ann.range.end)
92-
&& !line_range.contains(&ann.range.start)
93-
{
90+
}elseif line_range.contains(&ann.range.end){
9491
// Annotation ends on this line
9592
inline_marks.push(DisplayMark{
9693
mark_type:DisplayMarkType::AnnotationThrough,
97-
annotation_type:AnnotationType::Error,
94+
annotation_type:ann.annotation_type.clone(),
9895
});
9996
current_annotations.push(*ann);
10097
false
@@ -117,7 +114,7 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
117114
let inline_marks =if ann.range.start < line_start_pos{
118115
vec![DisplayMark{
119116
mark_type:DisplayMarkType::AnnotationThrough,
120-
annotation_type:AnnotationType::Error,
117+
annotation_type:ann.annotation_type.clone(),
121118
}]
122119
}else{
123120
vec![]
@@ -127,11 +124,11 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
127124
inline_marks,
128125
line:DisplaySourceLine::Annotation{
129126
annotation:Annotation{
130-
annotation_type:AnnotationType::Error,
127+
annotation_type:ann.annotation_type.clone(),
131128
id:None,
132129
label: ann.label,
133130
},
134-
range:(start,ann.range.end - line_start_pos),
131+
range: start..(ann.range.end - line_start_pos),
135132
},
136133
});
137134
}

‎src/renderers/ascii_default/mod.rs‎

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
pubmod styles;
22

3+
#[cfg(feature ="ansi_term")]
4+
usecrate::renderers::ascii_default::styles::color::Style;
5+
#[cfg(feature ="termcolor")]
6+
usecrate::renderers::ascii_default::styles::color2::Style;
7+
#[cfg(all(not(feature ="ansi_term"), not(feature ="termcolor")))]
8+
usecrate::renderers::ascii_default::styles::plain::Style;
9+
310
usesuper::RendererasRendererTrait;
411
usecrate::annotation::AnnotationType;
5-
usecrate::display_list::annotation::Annotation;
612
usecrate::display_list::line::DisplayLine;
713
usecrate::display_list::line::DisplayMark;
814
usecrate::display_list::line::DisplayMarkType;
@@ -13,6 +19,7 @@ use std::cmp;
1319
use std::io::Write;
1420
use std::marker::PhantomData;
1521
use styles::StyleasStyleTrait;
22+
use styles::StyleType;
1623

1724
fndigits(n:usize) ->usize{
1825
letmut n = n;
@@ -28,6 +35,10 @@ pub struct Renderer<S: StyleTrait> {
2835
style:PhantomData<S>,
2936
}
3037

38+
pubfnget_renderer() ->implRendererTrait{
39+
Renderer::<Style>::new()
40+
}
41+
3142
impl<S:StyleTrait>Renderer<S>{
3243
pubfnnew() ->Self{
3344
Renderer{style:PhantomData}
@@ -70,11 +81,15 @@ impl<S: StyleTrait> Renderer<S> {
7081
line,
7182
} =>{
7283
ifletSome(lineno) = lineno{
73-
write!(w,"{:>1$}", lineno, lineno_max)?;
84+
S::fmt(
85+
w,
86+
format_args!("{:>1$}", lineno, lineno_max),
87+
&[StyleType::LineNo,StyleType::Bold],
88+
)?;
7489
}else{
7590
write!(w,"{:>1$}","", lineno_max)?;
7691
}
77-
write!(w," | ")?;
92+
S::fmt(w," | ",&[StyleType::LineNo,StyleType::Bold])?;
7893
write!(w,"{:>1$}","", inline_marks_width - inline_marks.len())?;
7994
for markin inline_marks{
8095
self.fmt_display_mark(w, mark)?;
@@ -93,18 +108,17 @@ impl<S: StyleTrait> Renderer<S> {
93108
) -> std::io::Result<()>{
94109
match line{
95110
DisplaySourceLine::Content{ text} =>write!(w," {}", text),
96-
DisplaySourceLine::Annotation{
97-
annotation,
98-
range:(start, end),
99-
} =>{
100-
let indent =if start ==&0{0}else{ start +1};
111+
DisplaySourceLine::Annotation{ annotation, range} =>{
112+
let(_, style) =self.get_annotation_type_style(&annotation.annotation_type);
113+
let styles =[StyleType::Bold, style];
114+
let indent =if range.start ==0{0}else{ range.start +1};
101115
write!(w,"{:>1$}","", indent)?;
102-
if start ==&0{
103-
write!(w,"{:_>1$}","^",end - start+1)?;
116+
ifrange.start ==0{
117+
S::fmt(w,format_args!("{:_>1$}","^",range.len()+1),&styles)?;
104118
}else{
105-
write!(w,"{:->1$}","",end - start)?;
119+
S::fmt(w,format_args!("{:->1$}","",range.len()),&styles)?;
106120
}
107-
write!(w," {}",annotation.label)
121+
S::fmt(w, annotation.label,&styles)
108122
}
109123
DisplaySourceLine::Empty =>Ok(()),
110124
}
@@ -118,35 +132,41 @@ impl<S: StyleTrait> Renderer<S> {
118132
) -> std::io::Result<()>{
119133
match line{
120134
DisplayRawLine::Origin{ path, pos} =>{
121-
S::fmt(w,format_args!("{:>1$}","", lineno_max))?;
122-
S::fmt(w,format_args!("--> {}", path))?;
135+
write!(w,"{:>1$}","", lineno_max)?;
136+
S::fmt(w,"-->",&[StyleType::Bold,StyleType::LineNo])?;
137+
write!(w," {}", path)?;
123138
ifletSome(line) = pos.0{
124-
S::fmt(w,format_args!(":{}", line))?;
139+
write!(w,":{}", line)?;
125140
}
126141
write!(w,"\n")
127142
}
128143
DisplayRawLine::Annotation{ annotation, ..} =>{
129-
self.fmt_annotation(w, annotation)?;
144+
let(desc, style) =self.get_annotation_type_style(&annotation.annotation_type);
145+
let s =[StyleType::Bold, style];
146+
S::fmt(w, desc,&s)?;
130147
ifletSome(id) = annotation.id{
131-
write!(w,"[{}]", id)?;
148+
S::fmt(w,format_args!("[{}]", id),&s)?;
132149
}
133-
writeln!(w,": {}", annotation.label)
150+
S::fmt(
151+
w,
152+
format_args!(": {}\n", annotation.label),
153+
&[StyleType::Bold],
154+
)
134155
}
135156
}
136157
}
137158

138-
fnfmt_annotation(
159+
fnget_annotation_type_style(
139160
&self,
140-
w:&mutimpl std::io::Write,
141-
annotation:&Annotation,
142-
) -> std::io::Result<()>{
143-
match annotation.annotation_type{
144-
AnnotationType::None =>Ok(()),
145-
AnnotationType::Error =>write!(w,"error"),
146-
AnnotationType::Warning =>write!(w,"warning"),
147-
AnnotationType::Info =>write!(w,"info"),
148-
AnnotationType::Note =>write!(w,"note"),
149-
AnnotationType::Help =>write!(w,"help"),
161+
annotation_type:&AnnotationType,
162+
) ->(&'staticstr,StyleType){
163+
match annotation_type{
164+
AnnotationType::Error =>("error",StyleType::Error),
165+
AnnotationType::Warning =>("warning",StyleType::Warning),
166+
AnnotationType::Info =>("info",StyleType::Info),
167+
AnnotationType::Note =>("note",StyleType::Note),
168+
AnnotationType::Help =>("help",StyleType::Help),
169+
AnnotationType::None =>("",StyleType::None),
150170
}
151171
}
152172

@@ -155,10 +175,12 @@ impl<S: StyleTrait> Renderer<S> {
155175
w:&mutimpl std::io::Write,
156176
display_mark:&DisplayMark,
157177
) -> std::io::Result<()>{
158-
match display_mark.mark_type{
159-
DisplayMarkType::AnnotationStart =>write!(w,"/"),
160-
DisplayMarkType::AnnotationThrough =>write!(w,"|"),
161-
}
178+
let(_, style) =self.get_annotation_type_style(&display_mark.annotation_type);
179+
let ch =match display_mark.mark_type{
180+
DisplayMarkType::AnnotationStart =>'/',
181+
DisplayMarkType::AnnotationThrough =>'|',
182+
};
183+
S::fmt(w, ch,&[style])
162184
}
163185
}
164186

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,56 @@
1+
use ansi_term::Color::Fixed;
12
use ansi_term::StyleasAnsiTermStyle;
23

34
usesuper::StyleasStyleTrait;
5+
usesuper::StyleType;
46

57
use std::fmt;
68

79
pubstructStyle{}
810

911
implStyleTraitforStyle{
10-
fnfmt(w:&mutdyn std::io::Write,pattern:impl fmt::Display) -> std::io::Result<()>{
11-
let style =AnsiTermStyle::new().bold();
12+
fnfmt(
13+
w:&mutdyn std::io::Write,
14+
pattern:impl fmt::Display,
15+
styles:&[StyleType],
16+
) -> std::io::Result<()>{
17+
letmut style =AnsiTermStyle::new();
18+
for style_typein styles{
19+
match style_type{
20+
StyleType::Bold =>{
21+
style = style.bold();
22+
}
23+
StyleType::Error => style = style.fg(Fixed(9)),
24+
StyleType::Warning => style = style.fg(Fixed(11)),
25+
StyleType::Info => style = style.fg(Fixed(12)),
26+
StyleType::Note =>{}
27+
StyleType::Help => style = style.fg(Fixed(14)),
28+
StyleType::LineNo => style = style.fg(Fixed(12)),
29+
StyleType::None =>{}
30+
}
31+
}
1232
write!(w,"{}", style.paint(pattern.to_string()))
1333
}
1434
}
35+
36+
//-impl Stylesheet for AnsiTermStylesheet {
37+
//- fn get_style(&self, class: StyleClass) -> Box<dyn Style> {
38+
//- let ansi_term_style = match class {
39+
//- StyleClass::Error => Fixed(9).bold(),
40+
//- StyleClass::Warning => Fixed(11).bold(),
41+
//- StyleClass::Info => Fixed(12).bold(),
42+
//- StyleClass::Note => AnsiTermStyle::new().bold(),
43+
//- StyleClass::Help => Fixed(14).bold(),
44+
//-
45+
//- StyleClass::LineNo => Fixed(12).bold(),
46+
//-
47+
//-
48+
//- StyleClass::Emphasis => AnsiTermStyle::new().bold(),
49+
//-
50+
//- StyleClass::None => AnsiTermStyle::new(),
51+
//- };
52+
//- Box::new(AnsiTermStyleWrapper {
53+
//- style: ansi_term_style,
54+
//- })
55+
//- }
56+
//-}

‎src/renderers/ascii_default/styles/color2.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
use termcolor::{Ansi,ColorSpec,WriteColor};
22

33
usesuper::StyleasStyleTrait;
4+
usesuper::StyleType;
45

56
use std::fmt;
67
use std::io::Write;
78

89
pubstructStyle{}
910

1011
implStyleTraitforStyle{
11-
fnfmt(w:&mutdyn std::io::Write,pattern:impl fmt::Display) -> std::io::Result<()>{
12+
fnfmt(
13+
w:&mutdyn std::io::Write,
14+
pattern:impl fmt::Display,
15+
styles:&[StyleType],
16+
) -> std::io::Result<()>{
1217
letmut ansi =Ansi::new(w);
1318
ansi.set_color(ColorSpec::new().set_bold(true)).unwrap();
1419
write!(ansi,"{}", pattern)?;

‎src/renderers/ascii_default/styles/mod.rs‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,21 @@ pub mod plain;
77
use std::fmt;
88

99
pubtraitStyle{
10-
fnfmt(w:&mutdyn std::io::Write,pattern:impl fmt::Display) -> std::io::Result<()>;
10+
fnfmt(
11+
w:&mutdyn std::io::Write,
12+
pattern:impl fmt::Display,
13+
styles:&[StyleType],
14+
) -> std::io::Result<()>;
15+
}
16+
17+
#[derive(Debug)]
18+
pubenumStyleType{
19+
Bold,
20+
Error,
21+
Warning,
22+
Info,
23+
Note,
24+
Help,
25+
LineNo,
26+
None,
1127
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
usesuper::StyleasStyleTrait;
2+
usesuper::StyleType;
23

34
use std::fmt;
45

56
pubstructStyle{}
67

78
implStyleTraitforStyle{
8-
fnfmt(w:&mutdyn std::io::Write,pattern:impl fmt::Display) -> std::io::Result<()>{
9+
fnfmt(
10+
w:&mutdyn std::io::Write,
11+
pattern:impl fmt::Display,
12+
_styles:&[StyleType],
13+
) -> std::io::Result<()>{
914
write!(w,"{}", pattern)
1015
}
1116
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp