Skip to content

Commit

Permalink
clif-util: add option for controlling terminal colors
Browse files Browse the repository at this point in the history
On @fitzgen's suggestion, this change adds a `--color` option for controlling whether the `clif-util` output prints with ANSI color escape sequences. Only `clif-util wasm ...` currently uses this new option. The option has three variants:
 - `--color auto`, the default, prints colors if the terminal supports them
 - `--color always` prints colors always
 - `--color never` never prints colors
  • Loading branch information
abrown committed Aug 3, 2020
1 parent 39937c6 commit ef122a7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 16 deletions.
37 changes: 31 additions & 6 deletions cranelift/src/clif-util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
)
)]

use clap::{App, Arg, SubCommand};
use clap::{arg_enum, App, Arg, SubCommand};
use cranelift_codegen::dbg::LOG_FILENAME_PREFIX;
use cranelift_codegen::VERSION;
use std::io::{self, Write};
Expand Down Expand Up @@ -62,6 +62,26 @@ fn add_verbose_flag<'a>() -> clap::Arg<'a, 'a> {
Arg::with_name("verbose").short("v").help("Be more verbose")
}

arg_enum! {
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum UseTerminalColor {
Auto,
Never,
Always
}
}

fn add_color<'a>() -> clap::Arg<'a, 'a> {
Arg::with_name("color")
.long("color")
.possible_values(&UseTerminalColor::variants())
.takes_value(true)
.multiple(false)
.default_value("auto")
.case_insensitive(true)
.help("Use colors in output")
}

fn add_time_flag<'a>() -> clap::Arg<'a, 'a> {
Arg::with_name("time-passes")
.short("T")
Expand Down Expand Up @@ -120,6 +140,12 @@ fn add_check_translation_flag<'a>() -> clap::Arg<'a, 'a> {
.help("Just checks the correctness of Cranelift IR translated from WebAssembly")
}

fn add_value_ranges<'a>() -> clap::Arg<'a, 'a> {
Arg::with_name("value-ranges")
.long("value-ranges")
.help("Display values ranges and their locations")
}

/// Returns a vector of clap value options and changes these options into a vector of strings
fn get_vec(argument_vec: Option<clap::Values>) -> Vec<String> {
let mut ret_vec: Vec<String> = Vec::new();
Expand Down Expand Up @@ -201,11 +227,9 @@ fn main() {
)
.subcommand(add_wasm_or_compile("compile"))
.subcommand(
add_wasm_or_compile("wasm").arg(
Arg::with_name("value-ranges")
.long("value-ranges")
.help("Display values ranges and their locations"),
),
add_wasm_or_compile("wasm")
.arg(add_value_ranges())
.arg(add_color()),
)
.subcommand(
SubCommand::with_name("pass")
Expand Down Expand Up @@ -306,6 +330,7 @@ fn main() {

wasm::run(
get_vec(rest_cmd.values_of("file")),
rest_cmd.value_of("color").unwrap().parse().unwrap(),
rest_cmd.is_present("verbose"),
rest_cmd.is_present("just-decode"),
rest_cmd.is_present("check-translation"),
Expand Down
48 changes: 38 additions & 10 deletions cranelift/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use crate::disasm::{print_all, PrintRelocs, PrintStackmaps, PrintTraps};
use crate::utils::parse_sets_and_triple;
use crate::UseTerminalColor;
use cranelift_codegen::ir::DisplayFunctionAnnotations;
use cranelift_codegen::print_errors::{pretty_error, pretty_verifier_error};
use cranelift_codegen::settings::FlagsOrIsa;
Expand All @@ -31,27 +32,36 @@ macro_rules! vprintln {
}
/// For verbose printing: prints in color if the `$x` expression is true.
macro_rules! vcprintln {
($x: expr, $term: ident, $color: expr, $($tts:tt)*) => {
($x: expr, $use_color: expr, $term: ident, $color: expr, $($tts:tt)*) => {
if $x {
let _ = $term.fg($color);
if $use_color {
let _ = $term.fg($color);
}
println!($($tts)*);
let _ = $term.reset();
if $use_color {
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)*) => {
($x: expr, $use_color: expr, $term: ident, $color: expr, $($tts:tt)*) => {
if $x {
let _ = $term.fg($color);
if $use_color {
let _ = $term.fg($color);
}
print!($($tts)*);
let _ = $term.reset();
if $use_color {
let _ = $term.reset();
}
}
};
}

pub fn run(
files: Vec<String>,
use_terminal_color: UseTerminalColor,
flag_verbose: bool,
flag_just_decode: bool,
flag_check_translation: bool,
Expand All @@ -68,6 +78,7 @@ pub fn run(
let path = Path::new(&filename);
let name = String::from(path.as_os_str().to_string_lossy());
handle_module(
use_terminal_color,
flag_verbose,
flag_just_decode,
flag_check_translation,
Expand All @@ -85,6 +96,7 @@ pub fn run(
}

fn handle_module(
use_terminal_color: UseTerminalColor,
flag_verbose: bool,
flag_just_decode: bool,
flag_check_translation: bool,
Expand All @@ -98,10 +110,19 @@ fn handle_module(
fisa: FlagsOrIsa,
) -> Result<(), String> {
let mut terminal = term::stdout().unwrap();
vcprint!(flag_verbose, terminal, term::color::YELLOW, "Handling: ");
let use_color = terminal.supports_color() && use_terminal_color == UseTerminalColor::Auto
|| use_terminal_color == UseTerminalColor::Always;
vcprint!(
flag_verbose,
use_color,
terminal,
term::color::YELLOW,
"Handling: "
);
vprintln!(flag_verbose, "\"{}\"", name);
vcprint!(
flag_verbose,
use_color,
terminal,
term::color::MAGENTA,
"Translating... "
Expand Down Expand Up @@ -135,7 +156,7 @@ fn handle_module(
DummyEnvironment::new(isa.frontend_config(), ReturnMode::NormalReturns, debug_info);
translate_module(&module_binary, &mut dummy_environ).map_err(|e| e.to_string())?;

vcprintln!(flag_verbose, terminal, term::color::GREEN, "ok");
vcprintln!(flag_verbose, use_color, terminal, term::color::GREEN, "ok");

if flag_just_decode {
if !flag_print {
Expand Down Expand Up @@ -166,10 +187,17 @@ fn handle_module(
}

if flag_check_translation {
vcprint!(flag_verbose, terminal, term::color::MAGENTA, "Checking... ");
vcprint!(
flag_verbose,
use_color,
terminal,
term::color::MAGENTA,
"Checking... "
);
} else {
vcprint!(
flag_verbose,
use_color,
terminal,
term::color::MAGENTA,
"Compiling... "
Expand Down Expand Up @@ -278,6 +306,6 @@ fn handle_module(
println!("{}", timing::take_current());
}

vcprintln!(flag_verbose, terminal, term::color::GREEN, "ok");
vcprintln!(flag_verbose, use_color, terminal, term::color::GREEN, "ok");
Ok(())
}

0 comments on commit ef122a7

Please sign in to comment.