summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNanoTech2016-12-06 21:55:47 -0600
committerNanoTech2017-03-10 23:48:28 -0600
commite8da1dd3007a392e9594eca72177265f356cabeb (patch)
treee8f038960cb8b4da4f69f67465a7560f1d71e62a
parent3d9dfaeadf85c31295b2b14ac25bd66ddc774177 (diff)
Colored error messages
Diffstat (limited to '')
-rw-r--r--Cargo.lock10
-rw-r--r--Cargo.toml1
-rw-r--r--src/main.rs47
3 files changed, 49 insertions, 9 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 9cc468a..7c142e5 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6,6 +6,7 @@ dependencies = [
"lalrpop 0.12.4 (registry+https://github.com/rust-lang/crates.io-index)",
"lalrpop-util 0.12.4 (registry+https://github.com/rust-lang/crates.io-index)",
"nom 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "term-painter 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -210,6 +211,14 @@ dependencies = [
]
[[package]]
+name = "term-painter"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "term 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "thread-id"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -273,6 +282,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum rustc-serialize 0.3.21 (registry+https://github.com/rust-lang/crates.io-index)" = "bff9fc1c79f2dec76b253273d07682e94a978bd8f132ded071188122b2af9818"
"checksum strsim 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "67f84c44fbb2f91db7fef94554e6b2ac05909c9c0b0bc23bb98d3a1aebfe7f7c"
"checksum term 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "3deff8a2b3b6607d6d7cc32ac25c0b33709453ca9cceac006caac51e963cf94a"
+"checksum term-painter 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ab900bf2f05175932b13d4fc12f8ff09ef777715b04998791ab2c930841e496b"
"checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03"
"checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5"
"checksum unicode-xid 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f69506a2561962651710609304bbb961fa3da598c812f877975a82e48ee144f9"
diff --git a/Cargo.toml b/Cargo.toml
index 8a5ad81..ea1250e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -11,3 +11,4 @@ lalrpop = "0.12.4"
bytecount = "0.1.4"
lalrpop-util = "0.12.4"
nom = "2.0.0"
+term-painter = "0.2.3"
diff --git a/src/main.rs b/src/main.rs
index 0abcea8..deb6ac3 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,6 +2,7 @@ extern crate bytecount;
extern crate lalrpop_util;
#[macro_use]
extern crate nom;
+extern crate term_painter;
mod core;
pub use core::*;
@@ -16,18 +17,46 @@ fn print_error(message: &str, source: &str, start: usize, end: usize) {
let line_number = bytecount::count(source[..start].as_bytes(), '\n' as u8);
let line_start = source[..start].rfind('\n').map(|i| i + 1).unwrap_or(0);
let line_end = source[end..].find('\n').unwrap_or(0) + end;
- let context = &source[line_start..line_end];
+ let context_prefix = &source[line_start..start];
+ let context_highlighted = &source[start..end];
+ let context_suffix = &source[end..line_end];
- println!("error: {}", message);
- println!(" --> (stdin):{}:0", line_number);
let line_number_str = line_number.to_string();
let line_number_width = line_number_str.len();
- println!("{:w$} |", "", w = line_number_width);
- println!("{} | {}", line_number_str, context);
- println!("{:w$} | {:so$}{:^>ew$}", "", "", "",
- w = line_number_width,
- so = source[line_start..start].chars().count(),
- ew = ::std::cmp::max(1, source[start..end].chars().count()));
+
+ use term_painter::ToStyle;
+ let err_style = term_painter::Color::Red;
+ let bold = term_painter::Attr::Bold;
+
+ bold.with(|| {
+ err_style.with(|| {
+ print!("error: ");
+ });
+ println!("{}", message);
+ });
+ bold.with(|| {
+ print!(" -->");
+ });
+ println!(" {}:{}:0", "(stdin)", line_number);
+ bold.with(|| {
+ println!("{:w$} |", "", w = line_number_width);
+ print!("{} |", line_number_str);
+ });
+ print!(" {}", context_prefix);
+ bold.with(|| {
+ err_style.with(|| {
+ print!("{}", context_highlighted);
+ });
+ });
+ println!("{}", context_suffix);
+ bold.with(|| {
+ print!("{:w$} |", "", w = line_number_width);
+ err_style.with(|| {
+ println!(" {:so$}{:^>ew$}", "", "",
+ so = source[line_start..start].chars().count(),
+ ew = ::std::cmp::max(1, source[start..end].chars().count()));
+ });
+ });
}
fn main() {