mirror of
https://github.com/denoland/deno.git
synced 2024-12-25 00:29:09 -05:00
refactor: Remove duplicated colors.rs file (#11990)
This commit is contained in:
parent
00d62e64bf
commit
13991e5995
9 changed files with 35 additions and 178 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -613,7 +613,6 @@ dependencies = [
|
||||||
"shell-escape",
|
"shell-escape",
|
||||||
"sourcemap",
|
"sourcemap",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"termcolor",
|
|
||||||
"test_util",
|
"test_util",
|
||||||
"text-size",
|
"text-size",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
|
|
@ -88,7 +88,6 @@ serde = { version = "1.0.129", features = ["derive"] }
|
||||||
shell-escape = "0.1.5"
|
shell-escape = "0.1.5"
|
||||||
sourcemap = "6.0.1"
|
sourcemap = "6.0.1"
|
||||||
tempfile = "3.2.0"
|
tempfile = "3.2.0"
|
||||||
termcolor = "1.1.2"
|
|
||||||
text-size = "1.1.0"
|
text-size = "1.1.0"
|
||||||
tokio = { version = "1.10.1", features = ["full"] }
|
tokio = { version = "1.10.1", features = ["full"] }
|
||||||
tokio-rustls = "0.22.0"
|
tokio-rustls = "0.22.0"
|
||||||
|
|
132
cli/colors.rs
132
cli/colors.rs
|
@ -1,132 +0,0 @@
|
||||||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
|
||||||
|
|
||||||
use regex::Regex;
|
|
||||||
use std::fmt;
|
|
||||||
use std::io::Write;
|
|
||||||
use termcolor::Color::{Ansi256, Black, Blue, Cyan, Green, Red, White, Yellow};
|
|
||||||
use termcolor::{Ansi, ColorSpec, WriteColor};
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
use termcolor::{BufferWriter, ColorChoice};
|
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
|
||||||
// STRIP_ANSI_RE and strip_ansi_codes are lifted from the "console" crate.
|
|
||||||
// Copyright 2017 Armin Ronacher <armin.ronacher@active-4.com>. 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 = {
|
|
||||||
std::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<str> {
|
|
||||||
STRIP_ANSI_RE.replace_all(s, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn use_color() -> bool {
|
|
||||||
!(*NO_COLOR)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
pub fn enable_ansi() {
|
|
||||||
BufferWriter::stdout(ColorChoice::AlwaysAnsi);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn style<S: AsRef<str>>(s: S, colorspec: ColorSpec) -> impl fmt::Display {
|
|
||||||
if !use_color() {
|
|
||||||
return String::from(s.as_ref());
|
|
||||||
}
|
|
||||||
let mut v = Vec::new();
|
|
||||||
let mut ansi_writer = Ansi::new(&mut v);
|
|
||||||
ansi_writer.set_color(&colorspec).unwrap();
|
|
||||||
ansi_writer.write_all(s.as_ref().as_bytes()).unwrap();
|
|
||||||
ansi_writer.reset().unwrap();
|
|
||||||
String::from_utf8_lossy(&v).into_owned()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn red_bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
|
||||||
let mut style_spec = ColorSpec::new();
|
|
||||||
style_spec.set_fg(Some(Red)).set_bold(true);
|
|
||||||
style(s, style_spec)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn green_bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
|
||||||
let mut style_spec = ColorSpec::new();
|
|
||||||
style_spec.set_fg(Some(Green)).set_bold(true);
|
|
||||||
style(s, style_spec)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn italic<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
|
||||||
let mut style_spec = ColorSpec::new();
|
|
||||||
style_spec.set_italic(true);
|
|
||||||
style(s, style_spec)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn italic_gray<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
|
||||||
let mut style_spec = ColorSpec::new();
|
|
||||||
style_spec.set_fg(Some(Ansi256(8))).set_italic(true);
|
|
||||||
style(s, style_spec)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn italic_bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
|
||||||
let mut style_spec = ColorSpec::new();
|
|
||||||
style_spec.set_bold(true).set_italic(true);
|
|
||||||
style(s, style_spec)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn white_on_red<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
|
||||||
let mut style_spec = ColorSpec::new();
|
|
||||||
style_spec.set_bg(Some(Red)).set_fg(Some(White));
|
|
||||||
style(s, style_spec)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn black_on_green<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
|
||||||
let mut style_spec = ColorSpec::new();
|
|
||||||
style_spec.set_bg(Some(Green)).set_fg(Some(Black));
|
|
||||||
style(s, style_spec)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn yellow<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
|
||||||
let mut style_spec = ColorSpec::new();
|
|
||||||
style_spec.set_fg(Some(Yellow));
|
|
||||||
style(s, style_spec)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn cyan<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
|
||||||
let mut style_spec = ColorSpec::new();
|
|
||||||
style_spec.set_fg(Some(Cyan));
|
|
||||||
style(s, style_spec)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn red<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
|
||||||
let mut style_spec = ColorSpec::new();
|
|
||||||
style_spec.set_fg(Some(Red));
|
|
||||||
style(s, style_spec)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn green<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
|
||||||
let mut style_spec = ColorSpec::new();
|
|
||||||
style_spec.set_fg(Some(Green));
|
|
||||||
style(s, style_spec)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
|
||||||
let mut style_spec = ColorSpec::new();
|
|
||||||
style_spec.set_bold(true);
|
|
||||||
style(s, style_spec)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn gray<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
|
||||||
let mut style_spec = ColorSpec::new();
|
|
||||||
style_spec.set_fg(Some(Ansi256(8)));
|
|
||||||
style(s, style_spec)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn intense_blue<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
|
||||||
let mut style_spec = ColorSpec::new();
|
|
||||||
style_spec.set_fg(Some(Blue)).set_intense(true);
|
|
||||||
style(s, style_spec)
|
|
||||||
}
|
|
|
@ -1,6 +1,6 @@
|
||||||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
use crate::colors;
|
use deno_runtime::colors;
|
||||||
|
|
||||||
use deno_core::serde::Deserialize;
|
use deno_core::serde::Deserialize;
|
||||||
use deno_core::serde::Deserializer;
|
use deno_core::serde::Deserializer;
|
||||||
|
@ -421,9 +421,9 @@ impl Error for Diagnostics {}
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use colors::strip_ansi_codes;
|
|
||||||
use deno_core::serde_json;
|
use deno_core::serde_json;
|
||||||
use deno_core::serde_json::json;
|
use deno_core::serde_json::json;
|
||||||
|
use test_util::strip_ansi_codes;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_de_diagnostics() {
|
fn test_de_diagnostics() {
|
||||||
|
|
|
@ -215,7 +215,7 @@ mod tests {
|
||||||
|
|
||||||
fn run_test(diff_text1: &str, diff_text2: &str, expected_output: &str) {
|
fn run_test(diff_text1: &str, diff_text2: &str, expected_output: &str) {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
colors::strip_ansi_codes(&diff(diff_text1, diff_text2,)),
|
test_util::strip_ansi_codes(&diff(diff_text1, diff_text2,)),
|
||||||
expected_output,
|
expected_output,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -246,7 +246,7 @@ impl Error for PrettyJsError {}
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::colors::strip_ansi_codes;
|
use test_util::strip_ansi_codes;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_format_none_source_line() {
|
fn test_format_none_source_line() {
|
||||||
|
|
|
@ -410,7 +410,7 @@ mod test {
|
||||||
fn text_module_graph_info_display() {
|
fn text_module_graph_info_display() {
|
||||||
let fixture = get_fixture();
|
let fixture = get_fixture();
|
||||||
let text = fixture.to_string();
|
let text = fixture.to_string();
|
||||||
let actual = colors::strip_ansi_codes(&text);
|
let actual = test_util::strip_ansi_codes(&text);
|
||||||
let expected = r#"local: /cache/deps/https/deno.land/x/a.ts
|
let expected = r#"local: /cache/deps/https/deno.land/x/a.ts
|
||||||
type: TypeScript
|
type: TypeScript
|
||||||
emit: /cache/emit/https/deno.land/x/a.js
|
emit: /cache/emit/https/deno.land/x/a.js
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
mod ast;
|
mod ast;
|
||||||
mod auth_tokens;
|
mod auth_tokens;
|
||||||
mod checksum;
|
mod checksum;
|
||||||
mod colors;
|
|
||||||
mod config_file;
|
mod config_file;
|
||||||
mod deno_dir;
|
mod deno_dir;
|
||||||
mod diagnostics;
|
mod diagnostics;
|
||||||
|
@ -74,6 +73,7 @@ use deno_core::serde_json::json;
|
||||||
use deno_core::v8_set_flags;
|
use deno_core::v8_set_flags;
|
||||||
use deno_core::JsRuntime;
|
use deno_core::JsRuntime;
|
||||||
use deno_core::ModuleSpecifier;
|
use deno_core::ModuleSpecifier;
|
||||||
|
use deno_runtime::colors;
|
||||||
use deno_runtime::ops::worker_host::CreateWebWorkerCb;
|
use deno_runtime::ops::worker_host::CreateWebWorkerCb;
|
||||||
use deno_runtime::permissions::Permissions;
|
use deno_runtime::permissions::Permissions;
|
||||||
use deno_runtime::web_worker::WebWorker;
|
use deno_runtime::web_worker::WebWorker;
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||||
use regex::Regex;
|
|
||||||
use std::env;
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use termcolor::Color::{Ansi256, Black, Blue, Cyan, Green, Red, White, Yellow};
|
use termcolor::Color::{Ansi256, Black, Blue, Cyan, Green, Red, White, Yellow};
|
||||||
|
@ -10,18 +9,7 @@ use termcolor::{Ansi, ColorSpec, WriteColor};
|
||||||
use termcolor::{BufferWriter, ColorChoice};
|
use termcolor::{BufferWriter, ColorChoice};
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
lazy_static::lazy_static! {
|
||||||
// STRIP_ANSI_RE and strip_ansi_codes are lifted from the "console" crate.
|
static ref NO_COLOR: bool = std::env::var_os("NO_COLOR").is_some();
|
||||||
// Copyright 2017 Armin Ronacher <armin.ronacher@active-4.com>. 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<str> {
|
|
||||||
STRIP_ANSI_RE.replace_all(s, "")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn use_color() -> bool {
|
pub fn use_color() -> bool {
|
||||||
|
@ -33,94 +21,97 @@ pub fn enable_ansi() {
|
||||||
BufferWriter::stdout(ColorChoice::AlwaysAnsi);
|
BufferWriter::stdout(ColorChoice::AlwaysAnsi);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn style(s: &str, colorspec: ColorSpec) -> impl fmt::Display {
|
fn style<S: AsRef<str>>(s: S, colorspec: ColorSpec) -> impl fmt::Display {
|
||||||
if !use_color() {
|
if !use_color() {
|
||||||
return String::from(s);
|
return String::from(s.as_ref());
|
||||||
}
|
}
|
||||||
let mut v = Vec::new();
|
let mut v = Vec::new();
|
||||||
let mut ansi_writer = Ansi::new(&mut v);
|
let mut ansi_writer = Ansi::new(&mut v);
|
||||||
ansi_writer.set_color(&colorspec).unwrap();
|
ansi_writer.set_color(&colorspec).unwrap();
|
||||||
ansi_writer.write_all(s.as_bytes()).unwrap();
|
ansi_writer.write_all(s.as_ref().as_bytes()).unwrap();
|
||||||
ansi_writer.reset().unwrap();
|
ansi_writer.reset().unwrap();
|
||||||
String::from_utf8_lossy(&v).into_owned()
|
String::from_utf8_lossy(&v).into_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn red_bold(s: &str) -> impl fmt::Display {
|
pub fn red_bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
||||||
let mut style_spec = ColorSpec::new();
|
let mut style_spec = ColorSpec::new();
|
||||||
style_spec.set_fg(Some(Red)).set_bold(true);
|
style_spec.set_fg(Some(Red)).set_bold(true);
|
||||||
style(s, style_spec)
|
style(s, style_spec)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn green_bold(s: &str) -> impl fmt::Display {
|
pub fn green_bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
||||||
let mut style_spec = ColorSpec::new();
|
let mut style_spec = ColorSpec::new();
|
||||||
style_spec.set_fg(Some(Green)).set_bold(true);
|
style_spec.set_fg(Some(Green)).set_bold(true);
|
||||||
style(s, style_spec)
|
style(s, style_spec)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn italic_bold(s: &str) -> impl fmt::Display {
|
pub fn italic<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
||||||
|
let mut style_spec = ColorSpec::new();
|
||||||
|
style_spec.set_italic(true);
|
||||||
|
style(s, style_spec)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn italic_gray<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
||||||
|
let mut style_spec = ColorSpec::new();
|
||||||
|
style_spec.set_fg(Some(Ansi256(8))).set_italic(true);
|
||||||
|
style(s, style_spec)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn italic_bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
||||||
let mut style_spec = ColorSpec::new();
|
let mut style_spec = ColorSpec::new();
|
||||||
style_spec.set_bold(true).set_italic(true);
|
style_spec.set_bold(true).set_italic(true);
|
||||||
style(s, style_spec)
|
style(s, style_spec)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn white_on_red(s: &str) -> impl fmt::Display {
|
pub fn white_on_red<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
||||||
let mut style_spec = ColorSpec::new();
|
let mut style_spec = ColorSpec::new();
|
||||||
style_spec.set_bg(Some(Red)).set_fg(Some(White));
|
style_spec.set_bg(Some(Red)).set_fg(Some(White));
|
||||||
style(s, style_spec)
|
style(s, style_spec)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn black_on_green(s: &str) -> impl fmt::Display {
|
pub fn black_on_green<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
||||||
let mut style_spec = ColorSpec::new();
|
let mut style_spec = ColorSpec::new();
|
||||||
style_spec.set_bg(Some(Green)).set_fg(Some(Black));
|
style_spec.set_bg(Some(Green)).set_fg(Some(Black));
|
||||||
style(s, style_spec)
|
style(s, style_spec)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn yellow(s: &str) -> impl fmt::Display {
|
pub fn yellow<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
||||||
let mut style_spec = ColorSpec::new();
|
let mut style_spec = ColorSpec::new();
|
||||||
style_spec.set_fg(Some(Yellow));
|
style_spec.set_fg(Some(Yellow));
|
||||||
style(s, style_spec)
|
style(s, style_spec)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cyan(s: &str) -> impl fmt::Display {
|
pub fn cyan<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
||||||
let mut style_spec = ColorSpec::new();
|
let mut style_spec = ColorSpec::new();
|
||||||
style_spec.set_fg(Some(Cyan));
|
style_spec.set_fg(Some(Cyan));
|
||||||
style(s, style_spec)
|
style(s, style_spec)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn red(s: &str) -> impl fmt::Display {
|
pub fn red<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
||||||
let mut style_spec = ColorSpec::new();
|
let mut style_spec = ColorSpec::new();
|
||||||
style_spec.set_fg(Some(Red));
|
style_spec.set_fg(Some(Red));
|
||||||
style(s, style_spec)
|
style(s, style_spec)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn green(s: &str) -> impl fmt::Display {
|
pub fn green<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
||||||
let mut style_spec = ColorSpec::new();
|
let mut style_spec = ColorSpec::new();
|
||||||
style_spec.set_fg(Some(Green));
|
style_spec.set_fg(Some(Green));
|
||||||
style(s, style_spec)
|
style(s, style_spec)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bold(s: &str) -> impl fmt::Display {
|
pub fn bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
||||||
let mut style_spec = ColorSpec::new();
|
let mut style_spec = ColorSpec::new();
|
||||||
style_spec.set_bold(true);
|
style_spec.set_bold(true);
|
||||||
style(s, style_spec)
|
style(s, style_spec)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn gray(s: &str) -> impl fmt::Display {
|
pub fn gray<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
||||||
let mut style_spec = ColorSpec::new();
|
let mut style_spec = ColorSpec::new();
|
||||||
style_spec.set_fg(Some(Ansi256(8)));
|
style_spec.set_fg(Some(Ansi256(8)));
|
||||||
style(s, style_spec)
|
style(s, style_spec)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn italic_bold_gray(s: &str) -> impl fmt::Display {
|
pub fn intense_blue<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
||||||
let mut style_spec = ColorSpec::new();
|
|
||||||
style_spec
|
|
||||||
.set_fg(Some(Ansi256(8)))
|
|
||||||
.set_bold(true)
|
|
||||||
.set_italic(true);
|
|
||||||
style(s, style_spec)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn intense_blue(s: &str) -> impl fmt::Display {
|
|
||||||
let mut style_spec = ColorSpec::new();
|
let mut style_spec = ColorSpec::new();
|
||||||
style_spec.set_fg(Some(Blue)).set_intense(true);
|
style_spec.set_fg(Some(Blue)).set_intense(true);
|
||||||
style(s, style_spec)
|
style(s, style_spec)
|
||||||
|
|
Loading…
Reference in a new issue