From 390241eb056ca49b15220ca342110968a3ccc608 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 1 May 2015 19:16:08 +0200 Subject: [PATCH 1/4] Beginning of beautifuler rustc --explain --- src/librustc_driver/lib.rs | 137 ++++++++++++++++++++++++++++++++++++- 1 file changed, 136 insertions(+), 1 deletion(-) diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 8dff0f1d3f41a..c1002905a0d6d 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -262,6 +262,135 @@ pub trait CompilerCalls<'a> { fn build_controller(&mut self, &Session) -> CompileController<'a>; } +enum Color { + Black = 30, + Red = 31, + Green = 32, + Yellow = 33, + Blue = 34, + Magenta = 35, + Cyan = 36, + Grey = 37, + White = 49 +} + +enum Style { + NoStyle = 0, + Bold = 1 +} + +fn get_color(color: Color, style: Style, text: &str) -> String { + format!("\x1b[{};1m{}\x1b[0m", color as i32, /*style as i32, */text) +} + +fn parse_input(input: &str) -> String { + let lines : Vec<&str> = input.split('\n').collect(); + let mut out = String::new(); + + for line in lines { + let words : Vec<&str> = line.split(' ').collect(); + let mut it = 0; + + while it < words.len() { + let word : &str = words[it]; + + if word.starts_with("#") { + out.push_str(&get_color(Color::White, Style::NoStyle, word)); + it += 1; + continue; + } + match word { + "pub" | "const" | "static" | "crate" | "extern" => { + out.push_str(&get_color(Color::Red, Style::NoStyle, word)); + } + "fn" | "struct" | "mod" | "type" | "enum" | "let" | "match" | "trait" => { + out.push_str(&get_color(Color::Red, Style::NoStyle, word)); + out.push_str(&get_color(Color::Red, Style::NoStyle, " ")); + it += 1; + if it < words.len() { + out.push_str(&get_color(Color::Blue, Style::NoStyle, words[it])); + } + } + _ => { + if word.find(' ').is_some() { + let funcs : Vec<&str> = word.split('.').collect(); + + match funcs[funcs.len() - 1].find('(') { + Some(_) => { + let mut i = 0; + + if funcs.len() > 1 { + while i < funcs.len() - 2 { + out.push_str(&get_color(Color::Blue, Style::NoStyle, funcs[i])); + out.push('.'); + i += 1; + } + if i < funcs.len() { + let func_name : Vec<&str> = funcs[i].split('(').collect(); + out.push_str(&get_color(Color::Blue, Style::NoStyle, func_name[0])); + i = 1; + + while i < func_name.len() { + out.push('('); + out.push_str(func_name[i]); + i += 1; + } + } + } else { + out.push_str(funcs[0]); + } + } + None => { + out.push_str(word); + } + } + } else { + let func_name : Vec<&str> = word.split('(').collect(); + + + if func_name.len() > 1 { + out.push_str(&get_color(Color::Blue, Style::NoStyle, func_name[0])); + let mut i = 1; + + while i < func_name.len() { + out.push('('); + out.push_str(func_name[i]); + i += 1; + } + } else { + out.push_str(word); + } + } + } + } + it += 1; + if it < words.len() { + out.push(' '); + } + } + out.push('\n'); + } + out +} + +fn beautiful_error_printing(splits: &[&str]) -> String { + let mut i = 1; + let mut s = String::new(); + + while i < splits.len() { + s.push_str(splits[i - 1]); + //s.push_str(&format!("\x1b[{}m", GREEN)); + s.push_str(&parse_input(splits[i])); + //s.push_str(splits[i]); + //s.push_str(&format!("\x1b[{}m", NORMAL)); + i += 2; + } + if i - 1 < splits.len() { + s.push_str(splits[i - 1]) + } + s +} + // CompilerCalls instance for a regular rustc build. #[derive(Copy, Clone)] pub struct RustcDefaultCalls; @@ -276,7 +405,13 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls { match descriptions.find_description(&code[..]) { Some(ref description) => { // Slice off the leading newline and print. - print!("{}", &description[1..]); + let tmp_print : Vec<&str> = (&description[1..]).split("```\n").collect(); + + if tmp_print.len() < 2 { + print!("{}", tmp_print[0]); + } else { + print!("{}", beautiful_error_printing(&tmp_print)); + } } None => { early_error(&format!("no extended information for {}", code)); From 1ddfd15f74ab67dc5f6470e28d9bfbb40e6fa021 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 2 May 2015 00:28:49 +0200 Subject: [PATCH 2/4] Few updates --- src/librustc_driver/lib.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index c1002905a0d6d..17f99b7c7eb26 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -286,19 +286,20 @@ fn get_color(color: Color, style: Style, text: &str) -> String { fn parse_input(input: &str) -> String { let lines : Vec<&str> = input.split('\n').collect(); let mut out = String::new(); + let mut total = lines.len(); for line in lines { + if line.starts_with("#") { + out.push_str(&get_color(Color::White, Style::NoStyle, line)); + out.push('\n'); + continue; + } let words : Vec<&str> = line.split(' ').collect(); let mut it = 0; while it < words.len() { let word : &str = words[it]; - if word.starts_with("#") { - out.push_str(&get_color(Color::White, Style::NoStyle, word)); - it += 1; - continue; - } match word { "pub" | "const" | "static" | "crate" | "extern" => { out.push_str(&get_color(Color::Red, Style::NoStyle, word)); @@ -368,7 +369,10 @@ fn parse_input(input: &str) -> String { out.push(' '); } } - out.push('\n'); + total -= 1; + if total > 1 { + out.push('\n'); + } } out } From 914523203b5f9f3877d96ad1ea1edd866ef87512 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 2 May 2015 01:27:55 +0200 Subject: [PATCH 3/4] Fix the make tidy command --- src/librustc_driver/lib.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 17f99b7c7eb26..25572f0cee050 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -322,13 +322,15 @@ fn parse_input(input: &str) -> String { if funcs.len() > 1 { while i < funcs.len() - 2 { - out.push_str(&get_color(Color::Blue, Style::NoStyle, funcs[i])); + out.push_str(&get_color(Color::Blue, Style::NoStyle, + funcs[i])); out.push('.'); i += 1; } if i < funcs.len() { let func_name : Vec<&str> = funcs[i].split('(').collect(); - out.push_str(&get_color(Color::Blue, Style::NoStyle, func_name[0])); + out.push_str(&get_color(Color::Blue, Style::NoStyle, + func_name[0])); i = 1; while i < func_name.len() { @@ -347,7 +349,6 @@ fn parse_input(input: &str) -> String { } } else { let func_name : Vec<&str> = word.split('(').collect(); - if func_name.len() > 1 { out.push_str(&get_color(Color::Blue, Style::NoStyle, func_name[0])); From 02d875d057bd3cfd83db01b93d9cfdf4e5e631d9 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 2 May 2015 17:32:12 +0200 Subject: [PATCH 4/4] Create explain.rs file, use libterm --- src/librustc_driver/explain.rs | 134 ++++++++++++++++++++++++++++++++ src/librustc_driver/lib.rs | 137 +-------------------------------- 2 files changed, 136 insertions(+), 135 deletions(-) create mode 100644 src/librustc_driver/explain.rs diff --git a/src/librustc_driver/explain.rs b/src/librustc_driver/explain.rs new file mode 100644 index 0000000000000..8faf9c9f2f2ef --- /dev/null +++ b/src/librustc_driver/explain.rs @@ -0,0 +1,134 @@ +// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +extern crate term; + +fn parse_input(input: &str) { + let lines : Vec<&str> = input.split('\n').collect(); + //let mut out = String::new(); + let mut total = lines.len(); + let mut t = term::stdout().unwrap(); + + for line in lines { + if line.starts_with("#") { + t.attr(term::attr::Bold).unwrap(); + t.fg(term::color::WHITE).unwrap(); + (writeln!(t, "{}", line)).unwrap(); + t.reset().unwrap(); + continue; + } + let words : Vec<&str> = line.split(' ').collect(); + let mut it = 0; + + while it < words.len() { + let word : &str = words[it]; + + match word { + "pub" | "const" | "static" | "crate" | "extern" => { + t.attr(term::attr::Bold).unwrap(); + t.fg(term::color::RED).unwrap(); + (write!(t, "{}", word)).unwrap(); + t.reset().unwrap(); + } + "fn" | "struct" | "mod" | "type" | "enum" | "let" | "match" | "trait" => { + t.attr(term::attr::Bold).unwrap(); + t.fg(term::color::RED).unwrap(); + (write!(t, "{} ", word)).unwrap(); + it += 1; + t.fg(term::color::BLUE).unwrap(); + if it < words.len() { + (write!(t, "{}", words[it])).unwrap(); + } + t.reset().unwrap(); + } + _ => { + if word.find(' ').is_some() { + let funcs : Vec<&str> = word.split('.').collect(); + + match funcs[funcs.len() - 1].find('(') { + Some(_) => { + let mut i = 0; + + if funcs.len() > 1 { + while i < funcs.len() - 2 { + t.attr(term::attr::Bold).unwrap(); + t.fg(term::color::BLUE).unwrap(); + (write!(t, "{}.", funcs[i])).unwrap(); + t.reset().unwrap(); + i += 1; + } + if i < funcs.len() { + let func_name : Vec<&str> = funcs[i].split('(').collect(); + t.attr(term::attr::Bold).unwrap(); + t.fg(term::color::BLUE).unwrap(); + (write!(t, "{}.", func_name[0])).unwrap(); + t.reset().unwrap(); + i = 1; + + while i < func_name.len() { + (write!(t, "({}", func_name[i])).unwrap(); + i += 1; + } + } + } else { + (write!(t, "{}", funcs[0])).unwrap(); + } + } + None => { + (write!(t, "{}", word)).unwrap(); + } + } + } else { + let func_name : Vec<&str> = word.split('(').collect(); + + if func_name.len() > 1 { + t.attr(term::attr::Bold).unwrap(); + t.fg(term::color::BLUE).unwrap(); + (write!(t, "{}", func_name[0])).unwrap(); + t.reset().unwrap(); + let mut i = 1; + + while i < func_name.len() { + (write!(t, "({}", func_name[i])).unwrap(); + i += 1; + } + } else { + (write!(t, "{}", word)).unwrap(); + } + } + } + } + it += 1; + if it < words.len() { + (write!(t, " ")).unwrap(); + } + } + total -= 1; + if total > 1 { + (writeln!(t, "")).unwrap(); + } + } +} + +pub fn beautiful_error_printing(splits: &[&str]) { + let mut i = 1; + + while i < splits.len() { + print!("{}", splits[i - 1]); + parse_input(splits[i]); + i += 2; + if i < splits.len() { + println!(""); + } + } + if i - 1 < splits.len() { + print!("{}", splits[i - 1]); + } +} \ No newline at end of file diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 25572f0cee050..71f0a9b08a0a1 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -89,6 +89,7 @@ pub mod test; pub mod driver; pub mod pretty; +pub mod explain; const BUG_REPORT_URL: &'static str = @@ -262,140 +263,6 @@ pub trait CompilerCalls<'a> { fn build_controller(&mut self, &Session) -> CompileController<'a>; } -enum Color { - Black = 30, - Red = 31, - Green = 32, - Yellow = 33, - Blue = 34, - Magenta = 35, - Cyan = 36, - Grey = 37, - White = 49 -} - -enum Style { - NoStyle = 0, - Bold = 1 -} - -fn get_color(color: Color, style: Style, text: &str) -> String { - format!("\x1b[{};1m{}\x1b[0m", color as i32, /*style as i32, */text) -} - -fn parse_input(input: &str) -> String { - let lines : Vec<&str> = input.split('\n').collect(); - let mut out = String::new(); - let mut total = lines.len(); - - for line in lines { - if line.starts_with("#") { - out.push_str(&get_color(Color::White, Style::NoStyle, line)); - out.push('\n'); - continue; - } - let words : Vec<&str> = line.split(' ').collect(); - let mut it = 0; - - while it < words.len() { - let word : &str = words[it]; - - match word { - "pub" | "const" | "static" | "crate" | "extern" => { - out.push_str(&get_color(Color::Red, Style::NoStyle, word)); - } - "fn" | "struct" | "mod" | "type" | "enum" | "let" | "match" | "trait" => { - out.push_str(&get_color(Color::Red, Style::NoStyle, word)); - out.push_str(&get_color(Color::Red, Style::NoStyle, " ")); - it += 1; - if it < words.len() { - out.push_str(&get_color(Color::Blue, Style::NoStyle, words[it])); - } - } - _ => { - if word.find(' ').is_some() { - let funcs : Vec<&str> = word.split('.').collect(); - - match funcs[funcs.len() - 1].find('(') { - Some(_) => { - let mut i = 0; - - if funcs.len() > 1 { - while i < funcs.len() - 2 { - out.push_str(&get_color(Color::Blue, Style::NoStyle, - funcs[i])); - out.push('.'); - i += 1; - } - if i < funcs.len() { - let func_name : Vec<&str> = funcs[i].split('(').collect(); - out.push_str(&get_color(Color::Blue, Style::NoStyle, - func_name[0])); - i = 1; - - while i < func_name.len() { - out.push('('); - out.push_str(func_name[i]); - i += 1; - } - } - } else { - out.push_str(funcs[0]); - } - } - None => { - out.push_str(word); - } - } - } else { - let func_name : Vec<&str> = word.split('(').collect(); - - if func_name.len() > 1 { - out.push_str(&get_color(Color::Blue, Style::NoStyle, func_name[0])); - let mut i = 1; - - while i < func_name.len() { - out.push('('); - out.push_str(func_name[i]); - i += 1; - } - } else { - out.push_str(word); - } - } - } - } - it += 1; - if it < words.len() { - out.push(' '); - } - } - total -= 1; - if total > 1 { - out.push('\n'); - } - } - out -} - -fn beautiful_error_printing(splits: &[&str]) -> String { - let mut i = 1; - let mut s = String::new(); - - while i < splits.len() { - s.push_str(splits[i - 1]); - //s.push_str(&format!("\x1b[{}m", GREEN)); - s.push_str(&parse_input(splits[i])); - //s.push_str(splits[i]); - //s.push_str(&format!("\x1b[{}m", NORMAL)); - i += 2; - } - if i - 1 < splits.len() { - s.push_str(splits[i - 1]) - } - s -} - // CompilerCalls instance for a regular rustc build. #[derive(Copy, Clone)] pub struct RustcDefaultCalls; @@ -415,7 +282,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls { if tmp_print.len() < 2 { print!("{}", tmp_print[0]); } else { - print!("{}", beautiful_error_printing(&tmp_print)); + explain::beautiful_error_printing(&tmp_print) } } None => {