Skip to content

Commit 6804393

Browse files
Beginning of beautifuler rustc --explain
1 parent 65f8899 commit 6804393

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
@@ -263,6 +263,135 @@ pub trait CompilerCalls<'a> {
263263
fn build_controller(&mut self, &Session) -> CompileController<'a>;
264264
}
265265

266+
enum Color {
267+
Black = 30,
268+
Red = 31,
269+
Green = 32,
270+
Yellow = 33,
271+
Blue = 34,
272+
Magenta = 35,
273+
Cyan = 36,
274+
Grey = 37,
275+
White = 49
276+
}
277+
278+
enum Style {
279+
NoStyle = 0,
280+
Bold = 1
281+
}
282+
283+
fn get_color(color: Color, style: Style, text: &str) -> String {
284+
format!("\x1b[{};1m{}\x1b[0m", color as i32, /*style as i32, */text)
285+
}
286+
287+
fn parse_input(input: &str) -> String {
288+
let lines : Vec<&str> = input.split('\n').collect();
289+
let mut out = String::new();
290+
291+
for line in lines {
292+
let words : Vec<&str> = line.split(' ').collect();
293+
let mut it = 0;
294+
295+
while it < words.len() {
296+
let word : &str = words[it];
297+
298+
if word.starts_with("#") {
299+
out.push_str(&get_color(Color::White, Style::NoStyle, word));
300+
it += 1;
301+
continue;
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, funcs[i]));
326+
out.push('.');
327+
i += 1;
328+
}
329+
if i < funcs.len() {
330+
let func_name : Vec<&str> = funcs[i].split('(').collect();
331+
out.push_str(&get_color(Color::Blue, Style::NoStyle, func_name[0]));
332+
i = 1;
333+
334+
while i < func_name.len() {
335+
out.push('(');
336+
out.push_str(func_name[i]);
337+
i += 1;
338+
}
339+
}
340+
} else {
341+
out.push_str(funcs[0]);
342+
}
343+
}
344+
None => {
345+
out.push_str(word);
346+
}
347+
}
348+
} else {
349+
let func_name : Vec<&str> = word.split('(').collect();
350+
351+
352+
if func_name.len() > 1 {
353+
out.push_str(&get_color(Color::Blue, Style::NoStyle, func_name[0]));
354+
let mut i = 1;
355+
356+
while i < func_name.len() {
357+
out.push('(');
358+
out.push_str(func_name[i]);
359+
i += 1;
360+
}
361+
} else {
362+
out.push_str(word);
363+
}
364+
}
365+
}
366+
}
367+
it += 1;
368+
if it < words.len() {
369+
out.push(' ');
370+
}
371+
}
372+
out.push('\n');
373+
}
374+
out
375+
}
376+
377+
fn beautiful_error_printing(splits: &[&str]) -> String {
378+
let mut i = 1;
379+
let mut s = String::new();
380+
381+
while i < splits.len() {
382+
s.push_str(splits[i - 1]);
383+
//s.push_str(&format!("\x1b[{}m", GREEN));
384+
s.push_str(&parse_input(splits[i]));
385+
//s.push_str(splits[i]);
386+
//s.push_str(&format!("\x1b[{}m", NORMAL));
387+
i += 2;
388+
}
389+
if i - 1 < splits.len() {
390+
s.push_str(splits[i - 1])
391+
}
392+
s
393+
}
394+
266395
// CompilerCalls instance for a regular rustc build.
267396
#[derive(Copy, Clone)]
268397
pub struct RustcDefaultCalls;
@@ -277,7 +406,13 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
277406
match descriptions.find_description(&code[..]) {
278407
Some(ref description) => {
279408
// Slice off the leading newline and print.
280-
print!("{}", &description[1..]);
409+
let tmp_print : Vec<&str> = (&description[1..]).split("```\n").collect();
410+
411+
if tmp_print.len() < 2 {
412+
print!("{}", tmp_print[0]);
413+
} else {
414+
print!("{}", beautiful_error_printing(&tmp_print));
415+
}
281416
}
282417
None => {
283418
early_error(&format!("no extended information for {}", code));

0 commit comments

Comments
 (0)