From a93b29007f1b231fc2a5904d21e1fed354ff9e21 Mon Sep 17 00:00:00 2001 From: Tomohito Nakayama Date: Mon, 16 Sep 2019 03:48:25 +0900 Subject: Rename ansi.rs to colors.rs (#2956) --- cli/ansi.rs | 90 ------------------------------------------------ cli/colors.rs | 89 +++++++++++++++++++++++++++++++++++++++++++++++ cli/deno_error.rs | 2 +- cli/diagnostics.rs | 9 ++--- cli/fmt_errors.rs | 24 ++++++------- cli/integration_tests.rs | 2 +- cli/main.rs | 20 +++++------ cli/ops/os.rs | 4 +-- 8 files changed, 120 insertions(+), 120 deletions(-) delete mode 100644 cli/ansi.rs create mode 100644 cli/colors.rs (limited to 'cli') diff --git a/cli/ansi.rs b/cli/ansi.rs deleted file mode 100644 index ab2347bcd..000000000 --- a/cli/ansi.rs +++ /dev/null @@ -1,90 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -// TODO(ry) Rename this file to colors.rs -// TODO(ry) Replace ansi_term with termcolor. -use ansi_term::Color::Black; -use ansi_term::Color::Fixed; -use ansi_term::Color::Red; -use ansi_term::Color::White; -use ansi_term::Style; -use regex::Regex; -use std::env; -use std::fmt; - -lazy_static! { - // STRIP_ANSI_RE and strip_ansi_codes are lifted from the "console" crate. - // Copyright 2017 Armin Ronacher . MIT License. - static ref STRIP_ANSI_RE: Regex = Regex::new( - r"[\x1b\x9b][\[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-PRZcf-nqry=><]" - ).unwrap(); - static ref NO_COLOR: bool = { - env::var_os("NO_COLOR").is_some() - }; -} - -/// Helper function to strip ansi codes. -#[cfg(test)] -pub fn strip_ansi_codes(s: &str) -> std::borrow::Cow { - STRIP_ANSI_RE.replace_all(s, "") -} - -pub fn use_color() -> bool { - !(*NO_COLOR) -} - -pub fn red_bold(s: String) -> impl fmt::Display { - let mut style = Style::new(); - if use_color() { - style = style.bold().fg(Red); - } - style.paint(s) -} - -pub fn italic_bold(s: String) -> impl fmt::Display { - let mut style = Style::new(); - if use_color() { - style = style.italic().bold(); - } - style.paint(s) -} - -pub fn black_on_white(s: String) -> impl fmt::Display { - let mut style = Style::new(); - if use_color() { - style = style.on(White).fg(Black); - } - style.paint(s) -} - -pub fn yellow(s: String) -> impl fmt::Display { - let mut style = Style::new(); - if use_color() { - // matches TypeScript's ForegroundColorEscapeSequences.Yellow - style = style.fg(Fixed(11)); - } - style.paint(s) -} - -pub fn cyan(s: String) -> impl fmt::Display { - let mut style = Style::new(); - if use_color() { - // matches TypeScript's ForegroundColorEscapeSequences.Cyan - style = style.fg(Fixed(14)); - } - style.paint(s) -} - -pub fn red(s: String) -> impl fmt::Display { - let mut style = Style::new(); - if use_color() { - style = style.fg(Red); - } - style.paint(s) -} - -pub fn bold(s: String) -> impl fmt::Display { - let mut style = Style::new(); - if use_color() { - style = style.bold(); - } - style.paint(s) -} diff --git a/cli/colors.rs b/cli/colors.rs new file mode 100644 index 000000000..7ca42e2f5 --- /dev/null +++ b/cli/colors.rs @@ -0,0 +1,89 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +// TODO(ry) Replace ansi_term with termcolor. +use ansi_term::Color::Black; +use ansi_term::Color::Fixed; +use ansi_term::Color::Red; +use ansi_term::Color::White; +use ansi_term::Style; +use regex::Regex; +use std::env; +use std::fmt; + +lazy_static! { + // STRIP_ANSI_RE and strip_ansi_codes are lifted from the "console" crate. + // Copyright 2017 Armin Ronacher . MIT License. + static ref STRIP_ANSI_RE: Regex = Regex::new( + r"[\x1b\x9b][\[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-PRZcf-nqry=><]" + ).unwrap(); + static ref NO_COLOR: bool = { + env::var_os("NO_COLOR").is_some() + }; +} + +/// Helper function to strip ansi codes. +#[cfg(test)] +pub fn strip_ansi_codes(s: &str) -> std::borrow::Cow { + STRIP_ANSI_RE.replace_all(s, "") +} + +pub fn use_color() -> bool { + !(*NO_COLOR) +} + +pub fn red_bold(s: String) -> impl fmt::Display { + let mut style = Style::new(); + if use_color() { + style = style.bold().fg(Red); + } + style.paint(s) +} + +pub fn italic_bold(s: String) -> impl fmt::Display { + let mut style = Style::new(); + if use_color() { + style = style.italic().bold(); + } + style.paint(s) +} + +pub fn black_on_white(s: String) -> impl fmt::Display { + let mut style = Style::new(); + if use_color() { + style = style.on(White).fg(Black); + } + style.paint(s) +} + +pub fn yellow(s: String) -> impl fmt::Display { + let mut style = Style::new(); + if use_color() { + // matches TypeScript's ForegroundColorEscapeSequences.Yellow + style = style.fg(Fixed(11)); + } + style.paint(s) +} + +pub fn cyan(s: String) -> impl fmt::Display { + let mut style = Style::new(); + if use_color() { + // matches TypeScript's ForegroundColorEscapeSequences.Cyan + style = style.fg(Fixed(14)); + } + style.paint(s) +} + +pub fn red(s: String) -> impl fmt::Display { + let mut style = Style::new(); + if use_color() { + style = style.fg(Red); + } + style.paint(s) +} + +pub fn bold(s: String) -> impl fmt::Display { + let mut style = Style::new(); + if use_color() { + style = style.bold(); + } + style.paint(s) +} diff --git a/cli/deno_error.rs b/cli/deno_error.rs index 71e5ac806..2066d9c52 100644 --- a/cli/deno_error.rs +++ b/cli/deno_error.rs @@ -299,7 +299,7 @@ impl GetErrorKind for dyn AnyError { #[cfg(test)] mod tests { use super::*; - use crate::ansi::strip_ansi_codes; + use crate::colors::strip_ansi_codes; use crate::diagnostics::Diagnostic; use crate::diagnostics::DiagnosticCategory; use crate::diagnostics::DiagnosticItem; diff --git a/cli/diagnostics.rs b/cli/diagnostics.rs index 14f5ba2c4..1e7c2b5eb 100644 --- a/cli/diagnostics.rs +++ b/cli/diagnostics.rs @@ -1,7 +1,7 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. //! This module encodes TypeScript errors (diagnostics) into Rust structs and //! contains code for printing them to the console. -use crate::ansi; +use crate::colors; use crate::fmt_errors::format_maybe_source_line; use crate::fmt_errors::format_maybe_source_name; use crate::fmt_errors::DisplayFormatter; @@ -183,7 +183,7 @@ impl DisplayFormatter for DiagnosticItem { fn format_category_and_code(&self) -> String { let category = match self.category { DiagnosticCategory::Error => { - format!("{}", ansi::red_bold("error".to_string())) + format!("{}", colors::red_bold("error".to_string())) } DiagnosticCategory::Warning => "warn".to_string(), DiagnosticCategory::Debug => "debug".to_string(), @@ -191,7 +191,8 @@ impl DisplayFormatter for DiagnosticItem { _ => "".to_string(), }; - let code = ansi::bold(format!(" TS{}", self.code.to_string())).to_string(); + let code = + colors::bold(format!(" TS{}", self.code.to_string())).to_string(); format!("{}{}: ", category, code) } @@ -340,7 +341,7 @@ impl From for DiagnosticCategory { #[cfg(test)] mod tests { use super::*; - use crate::ansi::strip_ansi_codes; + use crate::colors::strip_ansi_codes; fn diagnostic1() -> Diagnostic { Diagnostic { diff --git a/cli/fmt_errors.rs b/cli/fmt_errors.rs index 60b4e3356..84fcf5b43 100644 --- a/cli/fmt_errors.rs +++ b/cli/fmt_errors.rs @@ -1,6 +1,6 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. //! This mod provides DenoError to unify errors across Deno. -use crate::ansi; +use crate::colors; use crate::source_maps::apply_source_map; use crate::source_maps::SourceMapGetter; use deno::ErrBox; @@ -20,9 +20,9 @@ pub trait DisplayFormatter { } fn format_source_name(script_name: String, line: i64, column: i64) -> String { - let script_name_c = ansi::cyan(script_name); - let line_c = ansi::yellow((1 + line).to_string()); - let column_c = ansi::yellow((1 + column).to_string()); + let script_name_c = colors::cyan(script_name); + let line_c = colors::yellow((1 + line).to_string()); + let column_c = colors::yellow((1 + column).to_string()); format!("{}:{}:{}", script_name_c, line_c, column_c,) } @@ -65,10 +65,10 @@ pub fn format_maybe_source_line( assert!(start_column.is_some()); assert!(end_column.is_some()); let line = (1 + line_number.unwrap()).to_string(); - let line_color = ansi::black_on_white(line.to_string()); + let line_color = colors::black_on_white(line.to_string()); let line_len = line.clone().len(); let line_padding = - ansi::black_on_white(format!("{:indent$}", "", indent = line_len)) + colors::black_on_white(format!("{:indent$}", "", indent = line_len)) .to_string(); let mut s = String::new(); let start_column = start_column.unwrap(); @@ -89,9 +89,9 @@ pub fn format_maybe_source_line( } } let color_underline = if is_error { - ansi::red(s).to_string() + colors::red(s).to_string() } else { - ansi::cyan(s).to_string() + colors::cyan(s).to_string() }; let indent = format!("{:indent$}", "", indent = level); @@ -104,13 +104,13 @@ pub fn format_maybe_source_line( /// Format a message to preface with `error: ` with ansi codes for red. pub fn format_error_message(msg: String) -> String { - let preamble = ansi::red("error:".to_string()); + let preamble = colors::red("error:".to_string()); format!("{} {}", preamble, msg) } fn format_stack_frame(frame: &StackFrame) -> String { // Note when we print to string, we change from 0-indexed to 1-indexed. - let function_name = ansi::italic_bold(frame.function_name.clone()); + let function_name = colors::italic_bold(frame.function_name.clone()); let source_loc = format_source_name(frame.script_name.clone(), frame.line, frame.column); @@ -159,7 +159,7 @@ impl DisplayFormatter for JSError { fn format_message(&self, _level: usize) -> String { format!( "{}{}", - ansi::red_bold("error: ".to_string()), + colors::red_bold("error: ".to_string()), self.0.message.clone() ) } @@ -218,7 +218,7 @@ impl Error for JSError {} #[cfg(test)] mod tests { use super::*; - use crate::ansi::strip_ansi_codes; + use crate::colors::strip_ansi_codes; fn error1() -> V8Exception { V8Exception { diff --git a/cli/integration_tests.rs b/cli/integration_tests.rs index 9bf0c43b6..b03f69596 100644 --- a/cli/integration_tests.rs +++ b/cli/integration_tests.rs @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -use crate::ansi::strip_ansi_codes; +use crate::colors::strip_ansi_codes; use os_pipe::pipe; use std::env; use std::io::Read; diff --git a/cli/main.rs b/cli/main.rs index 30acb3ebd..554a36e59 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -21,8 +21,8 @@ extern crate url; #[cfg(test)] mod integration_tests; -mod ansi; mod assets; +mod colors; pub mod compilers; pub mod deno_dir; pub mod deno_error; @@ -142,17 +142,17 @@ fn print_cache_info(worker: Worker) { println!( "{} {:?}", - ansi::bold("DENO_DIR location:".to_string()), + colors::bold("DENO_DIR location:".to_string()), state.dir.root ); println!( "{} {:?}", - ansi::bold("Remote modules cache:".to_string()), + colors::bold("Remote modules cache:".to_string()), state.dir.deps_cache.location ); println!( "{} {:?}", - ansi::bold("TypeScript compiler cache:".to_string()), + colors::bold("TypeScript compiler cache:".to_string()), state.dir.gen_cache.location ); } @@ -171,13 +171,13 @@ pub fn print_file_info( .and_then(|out| { println!( "{} {}", - ansi::bold("local:".to_string()), + colors::bold("local:".to_string()), out.filename.to_str().unwrap() ); println!( "{} {}", - ansi::bold("type:".to_string()), + colors::bold("type:".to_string()), msg::enum_name_media_type(out.media_type) ); @@ -201,7 +201,7 @@ pub fn print_file_info( println!( "{} {}", - ansi::bold("compiled:".to_string()), + colors::bold("compiled:".to_string()), compiled_source_file.filename.to_str().unwrap(), ); } @@ -213,7 +213,7 @@ pub fn print_file_info( { println!( "{} {}", - ansi::bold("map:".to_string()), + colors::bold("map:".to_string()), source_map.filename.to_str().unwrap() ); } @@ -221,7 +221,7 @@ pub fn print_file_info( if let Some(deps) = worker.state.modules.lock().unwrap().deps(&compiled.name) { - println!("{}{}", ansi::bold("deps:\n".to_string()), deps.name); + println!("{}{}", colors::bold("deps:\n".to_string()), deps.name); if let Some(ref depsdeps) = deps.deps { for d in depsdeps { println!("{}", d); @@ -230,7 +230,7 @@ pub fn print_file_info( } else { println!( "{} cannot retrieve full dependency graph", - ansi::bold("deps:".to_string()), + colors::bold("deps:".to_string()), ); } Ok(worker) diff --git a/cli/ops/os.rs b/cli/ops/os.rs index 61484ca41..e44f0c5f2 100644 --- a/cli/ops/os.rs +++ b/cli/ops/os.rs @@ -1,6 +1,6 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. use super::dispatch_json::{Deserialize, JsonOp, Value}; -use crate::ansi; +use crate::colors; use crate::fs as deno_fs; use crate::state::ThreadSafeState; use crate::version; @@ -39,7 +39,7 @@ pub fn op_start( "v8Version": version::v8(), "denoVersion": version::DENO, "tsVersion": version::typescript(), - "noColor": !ansi::use_color(), + "noColor": !colors::use_color(), "xevalDelim": state.flags.xeval_delim.clone(), "os": BUILD_OS, "arch": BUILD_ARCH, -- cgit v1.2.3