Switch from term to termcolor.

See https://rustsec.org/advisories/RUSTSEC-2018-0015
This commit is contained in:
Dan Gohman
2021-02-16 11:49:58 -08:00
parent 09b976e1d5
commit d598883f3a
3 changed files with 28 additions and 112 deletions

View File

@@ -21,7 +21,7 @@ use std::io::Read;
use std::path::Path;
use std::path::PathBuf;
use structopt::StructOpt;
use term;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
/// For verbose printing: only print if the `$x` expression is true.
macro_rules! vprintln {
@@ -36,11 +36,11 @@ macro_rules! vcprintln {
($x: expr, $use_color: expr, $term: ident, $color: expr, $($tts:tt)*) => {
if $x {
if $use_color {
let _ = $term.fg($color);
$term.set_color(ColorSpec::new().set_fg(Some($color)))?;
}
println!($($tts)*);
if $use_color {
let _ = $term.reset();
$term.reset()?;
}
}
};
@@ -50,11 +50,11 @@ macro_rules! vcprint {
($x: expr, $use_color: expr, $term: ident, $color: expr, $($tts:tt)*) => {
if $x {
if $use_color {
let _ = $term.fg($color);
$term.set_color(ColorSpec::new().set_fg(Some($color)))?;
}
print!($($tts)*);
if $use_color {
let _ = $term.reset();
$term.reset()?;
}
}
};
@@ -113,25 +113,25 @@ pub struct Options {
/// Use colors in output? [options: auto/never/always; default: auto]
#[structopt(long("color"), default_value("auto"))]
color: Color,
color: ColorOpt,
}
#[derive(PartialEq, Eq)]
enum Color {
enum ColorOpt {
Auto,
Never,
Always,
}
impl std::str::FromStr for Color {
impl std::str::FromStr for ColorOpt {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.to_lowercase();
match s.as_str() {
"auto" => Ok(Color::Auto),
"never" => Ok(Color::Never),
"always" => Ok(Color::Always),
"auto" => Ok(ColorOpt::Auto),
"never" => Ok(ColorOpt::Never),
"always" => Ok(ColorOpt::Always),
_ => Err(format!("expected auto/never/always, found: {}", s)),
}
}
@@ -149,14 +149,19 @@ pub fn run(options: &Options) -> Result<()> {
}
fn handle_module(options: &Options, path: &Path, name: &str, fisa: FlagsOrIsa) -> Result<()> {
let mut terminal = term::stdout().unwrap();
let use_color =
terminal.supports_color() && options.color == Color::Auto || options.color == Color::Always;
let color_choice = match options.color {
ColorOpt::Auto => ColorChoice::Auto,
ColorOpt::Always => ColorChoice::Always,
ColorOpt::Never => ColorChoice::Never,
};
let mut terminal = StandardStream::stdout(color_choice);
let use_color = terminal.supports_color() && options.color == ColorOpt::Auto
|| options.color == ColorOpt::Always;
vcprint!(
options.verbose,
use_color,
terminal,
term::color::YELLOW,
Color::Yellow,
"Handling: "
);
vprintln!(options.verbose, "\"{}\"", name);
@@ -164,7 +169,7 @@ fn handle_module(options: &Options, path: &Path, name: &str, fisa: FlagsOrIsa) -
options.verbose,
use_color,
terminal,
term::color::MAGENTA,
Color::Magenta,
"Translating... "
);
@@ -192,13 +197,7 @@ fn handle_module(options: &Options, path: &Path, name: &str, fisa: FlagsOrIsa) -
DummyEnvironment::new(isa.frontend_config(), ReturnMode::NormalReturns, debug_info);
translate_module(&module_binary, &mut dummy_environ)?;
vcprintln!(
options.verbose,
use_color,
terminal,
term::color::GREEN,
"ok"
);
vcprintln!(options.verbose, use_color, terminal, Color::Green, "ok");
if options.just_decode {
if !options.print {
@@ -233,7 +232,7 @@ fn handle_module(options: &Options, path: &Path, name: &str, fisa: FlagsOrIsa) -
options.verbose,
use_color,
terminal,
term::color::MAGENTA,
Color::Magenta,
"Checking... "
);
} else {
@@ -241,7 +240,7 @@ fn handle_module(options: &Options, path: &Path, name: &str, fisa: FlagsOrIsa) -
options.verbose,
use_color,
terminal,
term::color::MAGENTA,
Color::Magenta,
"Compiling... "
);
}
@@ -351,12 +350,6 @@ fn handle_module(options: &Options, path: &Path, name: &str, fisa: FlagsOrIsa) -
println!("{}", timing::take_current());
}
vcprintln!(
options.verbose,
use_color,
terminal,
term::color::GREEN,
"ok"
);
vcprintln!(options.verbose, use_color, terminal, Color::Green, "ok");
Ok(())
}