Skip to content

Commit 02d875d

Browse files
Create explain.rs file, use libterm
1 parent 9145232 commit 02d875d

File tree

2 files changed

+136
-135
lines changed

2 files changed

+136
-135
lines changed

src/librustc_driver/explain.rs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
extern crate term;
12+
13+
fn parse_input(input: &str) {
14+
let lines : Vec<&str> = input.split('\n').collect();
15+
//let mut out = String::new();
16+
let mut total = lines.len();
17+
let mut t = term::stdout().unwrap();
18+
19+
for line in lines {
20+
if line.starts_with("#") {
21+
t.attr(term::attr::Bold).unwrap();
22+
t.fg(term::color::WHITE).unwrap();
23+
(writeln!(t, "{}", line)).unwrap();
24+
t.reset().unwrap();
25+
continue;
26+
}
27+
let words : Vec<&str> = line.split(' ').collect();
28+
let mut it = 0;
29+
30+
while it < words.len() {
31+
let word : &str = words[it];
32+
33+
match word {
34+
"pub" | "const" | "static" | "crate" | "extern" => {
35+
t.attr(term::attr::Bold).unwrap();
36+
t.fg(term::color::RED).unwrap();
37+
(write!(t, "{}", word)).unwrap();
38+
t.reset().unwrap();
39+
}
40+
"fn" | "struct" | "mod" | "type" | "enum" | "let" | "match" | "trait" => {
41+
t.attr(term::attr::Bold).unwrap();
42+
t.fg(term::color::RED).unwrap();
43+
(write!(t, "{} ", word)).unwrap();
44+
it += 1;
45+
t.fg(term::color::BLUE).unwrap();
46+
if it < words.len() {
47+
(write!(t, "{}", words[it])).unwrap();
48+
}
49+
t.reset().unwrap();
50+
}
51+
_ => {
52+
if word.find(' ').is_some() {
53+
let funcs : Vec<&str> = word.split('.').collect();
54+
55+
match funcs[funcs.len() - 1].find('(') {
56+
Some(_) => {
57+
let mut i = 0;
58+
59+
if funcs.len() > 1 {
60+
while i < funcs.len() - 2 {
61+
t.attr(term::attr::Bold).unwrap();
62+
t.fg(term::color::BLUE).unwrap();
63+
(write!(t, "{}.", funcs[i])).unwrap();
64+
t.reset().unwrap();
65+
i += 1;
66+
}
67+
if i < funcs.len() {
68+
let func_name : Vec<&str> = funcs[i].split('(').collect();
69+
t.attr(term::attr::Bold).unwrap();
70+
t.fg(term::color::BLUE).unwrap();
71+
(write!(t, "{}.", func_name[0])).unwrap();
72+
t.reset().unwrap();
73+
i = 1;
74+
75+
while i < func_name.len() {
76+
(write!(t, "({}", func_name[i])).unwrap();
77+
i += 1;
78+
}
79+
}
80+
} else {
81+
(write!(t, "{}", funcs[0])).unwrap();
82+
}
83+
}
84+
None => {
85+
(write!(t, "{}", word)).unwrap();
86+
}
87+
}
88+
} else {
89+
let func_name : Vec<&str> = word.split('(').collect();
90+
91+
if func_name.len() > 1 {
92+
t.attr(term::attr::Bold).unwrap();
93+
t.fg(term::color::BLUE).unwrap();
94+
(write!(t, "{}", func_name[0])).unwrap();
95+
t.reset().unwrap();
96+
let mut i = 1;
97+
98+
while i < func_name.len() {
99+
(write!(t, "({}", func_name[i])).unwrap();
100+
i += 1;
101+
}
102+
} else {
103+
(write!(t, "{}", word)).unwrap();
104+
}
105+
}
106+
}
107+
}
108+
it += 1;
109+
if it < words.len() {
110+
(write!(t, " ")).unwrap();
111+
}
112+
}
113+
total -= 1;
114+
if total > 1 {
115+
(writeln!(t, "")).unwrap();
116+
}
117+
}
118+
}
119+
120+
pub fn beautiful_error_printing(splits: &[&str]) {
121+
let mut i = 1;
122+
123+
while i < splits.len() {
124+
print!("{}", splits[i - 1]);
125+
parse_input(splits[i]);
126+
i += 2;
127+
if i < splits.len() {
128+
println!("");
129+
}
130+
}
131+
if i - 1 < splits.len() {
132+
print!("{}", splits[i - 1]);
133+
}
134+
}

src/librustc_driver/lib.rs

Lines changed: 2 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pub mod test;
8989

9090
pub mod driver;
9191
pub mod pretty;
92+
pub mod explain;
9293

9394

9495
const BUG_REPORT_URL: &'static str =
@@ -262,140 +263,6 @@ pub trait CompilerCalls<'a> {
262263
fn build_controller(&mut self, &Session) -> CompileController<'a>;
263264
}
264265

265-
enum Color {
266-
Black = 30,
267-
Red = 31,
268-
Green = 32,
269-
Yellow = 33,
270-
Blue = 34,
271-
Magenta = 35,
272-
Cyan = 36,
273-
Grey = 37,
274-
White = 49
275-
}
276-
277-
enum Style {
278-
NoStyle = 0,
279-
Bold = 1
280-
}
281-
282-
fn get_color(color: Color, style: Style, text: &str) -> String {
283-
format!("\x1b[{};1m{}\x1b[0m", color as i32, /*style as i32, */text)
284-
}
285-
286-
fn parse_input(input: &str) -> String {
287-
let lines : Vec<&str> = input.split('\n').collect();
288-
let mut out = String::new();
289-
let mut total = lines.len();
290-
291-
for line in lines {
292-
if line.starts_with("#") {
293-
out.push_str(&get_color(Color::White, Style::NoStyle, line));
294-
out.push('\n');
295-
continue;
296-
}
297-
let words : Vec<&str> = line.split(' ').collect();
298-
let mut it = 0;
299-
300-
while it < words.len() {
301-
let word : &str = words[it];
302-
303-
match word {
304-
"pub" | "const" | "static" | "crate" | "extern" => {
305-
out.push_str(&get_color(Color::Red, Style::NoStyle, word));
306-
}
307-
"fn" | "struct" | "mod" | "type" | "enum" | "let" | "match" | "trait" => {
308-
out.push_str(&get_color(Color::Red, Style::NoStyle, word));
309-
out.push_str(&get_color(Color::Red, Style::NoStyle, " "));
310-
it += 1;
311-
if it < words.len() {
312-
out.push_str(&get_color(Color::Blue, Style::NoStyle, words[it]));
313-
}
314-
}
315-
_ => {
316-
if word.find(' ').is_some() {
317-
let funcs : Vec<&str> = word.split('.').collect();
318-
319-
match funcs[funcs.len() - 1].find('(') {
320-
Some(_) => {
321-
let mut i = 0;
322-
323-
if funcs.len() > 1 {
324-
while i < funcs.len() - 2 {
325-
out.push_str(&get_color(Color::Blue, Style::NoStyle,
326-
funcs[i]));
327-
out.push('.');
328-
i += 1;
329-
}
330-
if i < funcs.len() {
331-
let func_name : Vec<&str> = funcs[i].split('(').collect();
332-
out.push_str(&get_color(Color::Blue, Style::NoStyle,
333-
func_name[0]));
334-
i = 1;
335-
336-
while i < func_name.len() {
337-
out.push('(');
338-
out.push_str(func_name[i]);
339-
i += 1;
340-
}
341-
}
342-
} else {
343-
out.push_str(funcs[0]);
344-
}
345-
}
346-
None => {
347-
out.push_str(word);
348-
}
349-
}
350-
} else {
351-
let func_name : Vec<&str> = word.split('(').collect();
352-
353-
if func_name.len() > 1 {
354-
out.push_str(&get_color(Color::Blue, Style::NoStyle, func_name[0]));
355-
let mut i = 1;
356-
357-
while i < func_name.len() {
358-
out.push('(');
359-
out.push_str(func_name[i]);
360-
i += 1;
361-
}
362-
} else {
363-
out.push_str(word);
364-
}
365-
}
366-
}
367-
}
368-
it += 1;
369-
if it < words.len() {
370-
out.push(' ');
371-
}
372-
}
373-
total -= 1;
374-
if total > 1 {
375-
out.push('\n');
376-
}
377-
}
378-
out
379-
}
380-
381-
fn beautiful_error_printing(splits: &[&str]) -> String {
382-
let mut i = 1;
383-
let mut s = String::new();
384-
385-
while i < splits.len() {
386-
s.push_str(splits[i - 1]);
387-
//s.push_str(&format!("\x1b[{}m", GREEN));
388-
s.push_str(&parse_input(splits[i]));
389-
//s.push_str(splits[i]);
390-
//s.push_str(&format!("\x1b[{}m", NORMAL));
391-
i += 2;
392-
}
393-
if i - 1 < splits.len() {
394-
s.push_str(splits[i - 1])
395-
}
396-
s
397-
}
398-
399266
// CompilerCalls instance for a regular rustc build.
400267
#[derive(Copy, Clone)]
401268
pub struct RustcDefaultCalls;
@@ -415,7 +282,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
415282
if tmp_print.len() < 2 {
416283
print!("{}", tmp_print[0]);
417284
} else {
418-
print!("{}", beautiful_error_printing(&tmp_print));
285+
explain::beautiful_error_printing(&tmp_print)
419286
}
420287
}
421288
None => {

0 commit comments

Comments
 (0)