clif-util: only print color escape sequences in verbose mode

`clif-util wasm ...` has functionality to print extra information in color when verbose mode (`-v`) is specified. Previously, the ANSI color escape sequences were printed regardless of whether `-v` was used so that users that captured output of this command would have to remove escape sequences from their capture files. With this change, `clif-util wasm ...` will only print the ANSI color escape sequences when `-v` is used.
This commit is contained in:
Andrew Brown
2020-08-03 10:20:49 -07:00
parent 65eaca35dd
commit 39937c60af

View File

@@ -21,20 +21,33 @@ use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use term; use term;
/// For verbose printing: only print if the `$x` expression is true.
macro_rules! vprintln { macro_rules! vprintln {
($x: expr, $($tts:tt)*) => { ($x: expr, $($tts:tt)*) => {
if $x { if $x {
println!($($tts)*); println!($($tts)*);
} }
} };
} }
/// For verbose printing: prints in color if the `$x` expression is true.
macro_rules! vprint { macro_rules! vcprintln {
($x: expr, $($tts:tt)*) => { ($x: expr, $term: ident, $color: expr, $($tts:tt)*) => {
if $x { if $x {
print!($($tts)*); let _ = $term.fg($color);
println!($($tts)*);
let _ = $term.reset();
} }
} };
}
/// For verbose printing: prints in color (without an appended newline) if the `$x` expression is true.
macro_rules! vcprint {
($x: expr, $term: ident, $color: expr, $($tts:tt)*) => {
if $x {
let _ = $term.fg($color);
print!($($tts)*);
let _ = $term.reset();
}
};
} }
pub fn run( pub fn run(
@@ -85,13 +98,14 @@ fn handle_module(
fisa: FlagsOrIsa, fisa: FlagsOrIsa,
) -> Result<(), String> { ) -> Result<(), String> {
let mut terminal = term::stdout().unwrap(); let mut terminal = term::stdout().unwrap();
let _ = terminal.fg(term::color::YELLOW); vcprint!(flag_verbose, terminal, term::color::YELLOW, "Handling: ");
vprint!(flag_verbose, "Handling: ");
let _ = terminal.reset();
vprintln!(flag_verbose, "\"{}\"", name); vprintln!(flag_verbose, "\"{}\"", name);
let _ = terminal.fg(term::color::MAGENTA); vcprint!(
vprint!(flag_verbose, "Translating... "); flag_verbose,
let _ = terminal.reset(); terminal,
term::color::MAGENTA,
"Translating... "
);
let module_binary = if path.to_str() == Some("-") { let module_binary = if path.to_str() == Some("-") {
let stdin = std::io::stdin(); let stdin = std::io::stdin();
@@ -121,9 +135,7 @@ fn handle_module(
DummyEnvironment::new(isa.frontend_config(), ReturnMode::NormalReturns, debug_info); DummyEnvironment::new(isa.frontend_config(), ReturnMode::NormalReturns, debug_info);
translate_module(&module_binary, &mut dummy_environ).map_err(|e| e.to_string())?; translate_module(&module_binary, &mut dummy_environ).map_err(|e| e.to_string())?;
let _ = terminal.fg(term::color::GREEN); vcprintln!(flag_verbose, terminal, term::color::GREEN, "ok");
vprintln!(flag_verbose, "ok");
let _ = terminal.reset();
if flag_just_decode { if flag_just_decode {
if !flag_print { if !flag_print {
@@ -153,13 +165,16 @@ fn handle_module(
return Ok(()); return Ok(());
} }
let _ = terminal.fg(term::color::MAGENTA);
if flag_check_translation { if flag_check_translation {
vprint!(flag_verbose, "Checking... "); vcprint!(flag_verbose, terminal, term::color::MAGENTA, "Checking... ");
} else { } else {
vprint!(flag_verbose, "Compiling... "); vcprint!(
flag_verbose,
terminal,
term::color::MAGENTA,
"Compiling... "
);
} }
let _ = terminal.reset();
if flag_print_size { if flag_print_size {
vprintln!(flag_verbose, ""); vprintln!(flag_verbose, "");
@@ -263,8 +278,6 @@ fn handle_module(
println!("{}", timing::take_current()); println!("{}", timing::take_current());
} }
let _ = terminal.fg(term::color::GREEN); vcprintln!(flag_verbose, terminal, term::color::GREEN, "ok");
vprintln!(flag_verbose, "ok");
let _ = terminal.reset();
Ok(()) Ok(())
} }