Skip to content

Commit 390241e

Browse files
Beginning of beautifuler rustc --explain
1 parent 613109d commit 390241e

File tree

1 file changed

+136
-1
lines changed

1 file changed

+136
-1
lines changed

src/librustc_driver/lib.rs

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,135 @@ pub trait CompilerCalls<'a> {
262262
fn build_controller(&mut self, &Session) -> CompileController<'a>;
263263
}
264264

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+
290+
for line in lines {
291+
let words : Vec<&str> = line.split(' ').collect();
292+
let mut it = 0;
293+
294+
while it < words.len() {
295+
let word : &str = words[it];
296+
297+
if word.starts_with("#") {
298+
out.push_str(&get_color(Color::White, Style::NoStyle, word));
299+
it += 1;
300+
continue;
301+
}
302+
match word {
303+
"pub" | "const" | "static" | "crate" | "extern" => {
304+
out.push_str(&get_color(Color::Red, Style::NoStyle, word));
305+
}
306+
"fn" | "struct" | "mod" | "type" | "enum" | "let" | "match" | "trait" => {
307+
out.push_str(&get_color(Color::Red, Style::NoStyle, word));
308+
out.push_str(&get_color(Color::Red, Style::NoStyle, " "));
309+
it += 1;
310+
if it < words.len() {
311+
out.push_str(&get_color(Color::Blue, Style::NoStyle, words[it]));
312+
}
313+
}
314+
_ => {
315+
if word.find(' ').is_some() {
316+
let funcs : Vec<&str> = word.split('.').collect();
317+
318+
match funcs[funcs.len() - 1].find('(') {
319+
Some(_) => {
320+
let mut i = 0;
321+
322+
if funcs.len() > 1 {
323+
while i < funcs.len() - 2 {
324+
out.push_str(&get_color(Color::Blue, Style::NoStyle, funcs[i]));
325+
out.push('.');
326+
i += 1;
327+
}
328+
if i < funcs.len() {
329+
let func_name : Vec<&str> = funcs[i].split('(').collect();
330+
out.push_str(&get_color(Color::Blue, Style::NoStyle, func_name[0]));
331+
i = 1;
332+
333+
while i < func_name.len() {
334+
out.push('(');
335+
out.push_str(func_name[i]);
336+
i += 1;
337+
}
338+
}
339+
} else {
340+
out.push_str(funcs[0]);
341+
}
342+
}
343+
None => {
344+
out.push_str(word);
345+
}
346+
}
347+
} else {
348+
let func_name : Vec<&str> = word.split('(').collect();
349+
350+
351+
if func_name.len() > 1 {
352+
out.push_str(&get_color(Color::Blue, Style::NoStyle, func_name[0]));
353+
let mut i = 1;
354+
355+
while i < func_name.len() {
356+
out.push('(');
357+
out.push_str(func_name[i]);
358+
i += 1;
359+
}
360+
} else {
361+
out.push_str(word);
362+
}
363+
}
364+
}
365+
}
366+
it += 1;
367+
if it < words.len() {
368+
out.push(' ');
369+
}
370+
}
371+
out.push('\n');
372+
}
373+
out
374+
}
375+
376+
fn beautiful_error_printing(splits: &[&str]) -> String {
377+
let mut i = 1;
378+
let mut s = String::new();
379+
380+
while i < splits.len() {
381+
s.push_str(splits[i - 1]);
382+
//s.push_str(&format!("\x1b[{}m", GREEN));
383+
s.push_str(&parse_input(splits[i]));
384+
//s.push_str(splits[i]);
385+
//s.push_str(&format!("\x1b[{}m", NORMAL));
386+
i += 2;
387+
}
388+
if i - 1 < splits.len() {
389+
s.push_str(splits[i - 1])
390+
}
391+
s
392+
}
393+
265394
// CompilerCalls instance for a regular rustc build.
266395
#[derive(Copy, Clone)]
267396
pub struct RustcDefaultCalls;
@@ -276,7 +405,13 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
276405
match descriptions.find_description(&code[..]) {
277406
Some(ref description) => {
278407
// Slice off the leading newline and print.
279-
print!("{}", &description[1..]);
408+
let tmp_print : Vec<&str> = (&description[1..]).split("```\n").collect();
409+
410+
if tmp_print.len() < 2 {
411+
print!("{}", tmp_print[0]);
412+
} else {
413+
print!("{}", beautiful_error_printing(&tmp_print));
414+
}
280415
}
281416
None => {
282417
early_error(&format!("no extended information for {}", code));

0 commit comments

Comments
 (0)