Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ab0a303

Browse files
committedOct 16, 2019
termcolor example works
1 parent 9b04c67 commit ab0a303

File tree

4 files changed

+59
-40
lines changed

4 files changed

+59
-40
lines changed
 

‎src/renderers/ascii_default/mod.rs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,31 @@ impl<S: StyleTrait> Renderer<S> {
8080
inline_marks,
8181
line,
8282
} => {
83+
let style = &[StyleType::LineNo];
8384
if let Some(lineno) = lineno {
8485
S::fmt(
8586
w,
86-
format_args!("{:>1$}", lineno, lineno_max),
87-
&[StyleType::LineNo, StyleType::Bold],
87+
format_args!("{:>width$} | ", lineno, width = lineno_max),
88+
style,
8889
)?;
8990
} else {
90-
write!(w, "{:>1$}", "", lineno_max)?;
91+
S::fmt(
92+
w,
93+
format_args!("{:>width$} | ", "", width = lineno_max),
94+
style,
95+
)?;
9196
}
92-
S::fmt(w, " | ", &[StyleType::LineNo, StyleType::Bold])?;
93-
write!(w, "{:>1$}", "", inline_marks_width - inline_marks.len())?;
97+
write!(
98+
w,
99+
"{:>width$}",
100+
"",
101+
width = inline_marks_width - inline_marks.len()
102+
)?;
94103
for mark in inline_marks {
95104
self.fmt_display_mark(w, mark)?;
96105
}
97106
self.fmt_source_line(w, line)?;
98-
write!(w, "\n")
107+
writeln!(w)
99108
}
100109
DisplayLine::Raw(l) => self.fmt_raw_line(w, l, lineno_max),
101110
}
@@ -110,15 +119,27 @@ impl<S: StyleTrait> Renderer<S> {
110119
DisplaySourceLine::Content { text } => write!(w, " {}", text),
111120
DisplaySourceLine::Annotation { annotation, range } => {
112121
let (_, style) = self.get_annotation_type_style(&annotation.annotation_type);
113-
let styles = [StyleType::Bold, style];
122+
let styles = [StyleType::Emphasis, style];
114123
let indent = if range.start == 0 { 0 } else { range.start + 1 };
115124
write!(w, "{:>1$}", "", indent)?;
116125
if range.start == 0 {
117-
S::fmt(w, format_args!("{:_>1$} ", "^", range.len() + 1), &styles)?;
126+
S::fmt(
127+
w,
128+
format_args!(
129+
"{:_>width$} {}",
130+
"^",
131+
annotation.label,
132+
width = range.len() + 1
133+
),
134+
&styles,
135+
)
118136
} else {
119-
S::fmt(w, format_args!("{:->1$} ", "", range.len()), &styles)?;
137+
S::fmt(
138+
w,
139+
format_args!("{:->width$} {}", "", annotation.label, width = range.len()),
140+
&styles,
141+
)
120142
}
121-
S::fmt(w, annotation.label, &styles)
122143
}
123144
DisplaySourceLine::Empty => Ok(()),
124145
}
@@ -133,24 +154,24 @@ impl<S: StyleTrait> Renderer<S> {
133154
match line {
134155
DisplayRawLine::Origin { path, pos } => {
135156
write!(w, "{:>1$}", "", lineno_max)?;
136-
S::fmt(w, "-->", &[StyleType::Bold, StyleType::LineNo])?;
157+
S::fmt(w, "-->", &[StyleType::Emphasis, StyleType::LineNo])?;
137158
write!(w, " {}", path)?;
138159
if let Some(line) = pos.0 {
139160
write!(w, ":{}", line)?;
140161
}
141-
write!(w, "\n")
162+
writeln!(w)
142163
}
143164
DisplayRawLine::Annotation { annotation, .. } => {
144165
let (desc, style) = self.get_annotation_type_style(&annotation.annotation_type);
145-
let s = [StyleType::Bold, style];
166+
let s = [StyleType::Emphasis, style];
146167
S::fmt(w, desc, &s)?;
147168
if let Some(id) = annotation.id {
148169
S::fmt(w, format_args!("[{}]", id), &s)?;
149170
}
150171
S::fmt(
151172
w,
152173
format_args!(": {}\n", annotation.label),
153-
&[StyleType::Bold],
174+
&[StyleType::Emphasis],
154175
)
155176
}
156177
}

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

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl StyleTrait for Style {
1717
let mut style = AnsiTermStyle::new();
1818
for style_type in styles {
1919
match style_type {
20-
StyleType::Bold => {
20+
StyleType::Emphasis => {
2121
style = style.bold();
2222
}
2323
StyleType::Error => style = style.fg(Fixed(9)),
@@ -32,25 +32,3 @@ impl StyleTrait for Style {
3232
write!(w, "{}", style.paint(pattern.to_string()))
3333
}
3434
}
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: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use termcolor::{Ansi, ColorSpec, WriteColor};
1+
use termcolor::{Ansi, Color, ColorSpec, WriteColor};
22

33
use super::Style as StyleTrait;
44
use super::StyleType;
@@ -14,8 +14,27 @@ impl StyleTrait for Style {
1414
pattern: impl fmt::Display,
1515
styles: &[StyleType],
1616
) -> std::io::Result<()> {
17+
let mut color = ColorSpec::new();
18+
for style_type in styles {
19+
match style_type {
20+
StyleType::Emphasis => {
21+
color.set_bold(true);
22+
}
23+
StyleType::Error => {
24+
color.set_fg(Some(Color::Red));
25+
}
26+
StyleType::Warning => {
27+
color.set_fg(Some(Color::Yellow));
28+
}
29+
StyleType::LineNo => {
30+
color.set_fg(Some(Color::Cyan));
31+
}
32+
_ => {}
33+
}
34+
}
1735
let mut ansi = Ansi::new(w);
18-
ansi.set_color(ColorSpec::new().set_bold(true)).unwrap();
36+
ansi.set_color(&color).unwrap();
37+
//ansi.set_color(ColorSpec::new().set_bold(true)).unwrap();
1938
write!(ansi, "{}", pattern)?;
2039
ansi.reset().unwrap();
2140
Ok(())

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ pub trait Style {
1616

1717
#[derive(Debug)]
1818
pub enum StyleType {
19-
Bold,
19+
Emphasis,
20+
2021
Error,
2122
Warning,
2223
Info,

0 commit comments

Comments
 (0)
Please sign in to comment.