Skip to content

Commit

Permalink
make it easy to print colored diagnostics output
Browse files Browse the repository at this point in the history
Signed-off-by: Jean Mertz <[email protected]>
  • Loading branch information
JeanMertz committed Jan 13, 2021
1 parent 80d4e0b commit cb820f0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/remap-lang/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,19 @@ impl<'a> From<(&'a str, program::Error)> for ProgramError<'a> {

fn fmt_diagnostic(f: &mut fmt::Formatter<'_>, source: &str, diagnostic: Diagnostic) -> fmt::Result {
use codespan_reporting::files::SimpleFile;
use codespan_reporting::term::{self, DisplayStyle};
use codespan_reporting::term;
use std::str::from_utf8;
use termcolor::Buffer;

let file = SimpleFile::new("<source>", source);
let mut config = term::Config::default();
config.display_style = if f.alternate() {
DisplayStyle::Short
let config = term::Config::default();

let mut buffer = if f.alternate() {
Buffer::ansi()
} else {
DisplayStyle::Rich
Buffer::no_color()
};

let mut buffer = Buffer::no_color();
term::emit(&mut buffer, &config, &file, &diagnostic.into()).map_err(|_| fmt::Error)?;

f.write_str(from_utf8(buffer.as_slice()).map_err(|_| fmt::Error)?)
Expand Down
17 changes: 17 additions & 0 deletions lib/remap-tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ use std::fs;
use std::path::PathBuf;

fn main() {
let verbose = std::env::args()
.nth(1)
.map(|s| s == "--verbose")
.unwrap_or_default();

let mut failed_count = 0;
let tests = fs::read_dir("tests").expect("dir exists");

Expand Down Expand Up @@ -31,6 +36,10 @@ fn main() {

if got == want {
println!("{}", Colour::Green.bold().paint("OK"));

if verbose {
println!("{:#}", value);
}
} else {
println!("{} (expectation)", Colour::Red.bold().paint("FAILED"));

Expand All @@ -45,6 +54,10 @@ fn main() {
let got = err.to_string();
if got == want {
println!("{}", Colour::Green.bold().paint("OK"));

if verbose {
println!("{:#}", err);
}
} else {
println!("{} (runtime)", Colour::Red.bold().paint("FAILED"));
println!("{}", err);
Expand All @@ -57,6 +70,10 @@ fn main() {
let want = want.trim().to_owned();
if got == want {
println!("{}", Colour::Green.bold().paint("OK"));

if verbose {
println!("{:#}", err);
}
} else {
println!("{} (compilation)", Colour::Red.bold().paint("FAILED"));

Expand Down

0 comments on commit cb820f0

Please sign in to comment.