diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2020-02-24 17:18:15 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-24 17:18:15 -0500 |
commit | 4005174f6c584fd5200dbe450b269a7f0de487ce (patch) | |
tree | 7cc3b463e81054f0f29219276ec64aeb5fd87d14 | |
parent | 79c6e052ed29b9b78c31f2e01da3b91f76b6a017 (diff) |
Revert "Remove ansi_term dependency"
Broke colors
https://github.com/denoland/deno/issues/4112#issuecomment-590545385
This reverts commit c250778704a4e0065e54e6bf6ca6c39d556a6d8d.
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | cli/Cargo.toml | 1 | ||||
-rw-r--r-- | cli/colors.rs | 98 | ||||
-rw-r--r-- | cli/lib.rs | 3 | ||||
-rw-r--r-- | cli/permissions.rs | 10 | ||||
-rw-r--r-- | std/encoding/testdata/cargo.toml | 1 | ||||
-rw-r--r-- | std/encoding/toml_test.ts | 1 |
7 files changed, 67 insertions, 48 deletions
diff --git a/Cargo.lock b/Cargo.lock index 29f8e334c..94e5d3240 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -433,6 +433,7 @@ dependencies = [ name = "deno" version = "0.34.0" dependencies = [ + "ansi_term", "atty", "base64 0.11.0", "byteorder", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 52bc4f2c3..fdca9cceb 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -26,6 +26,7 @@ deno_typescript = { path = "../deno_typescript", version = "0.34.0" } deno_core = { path = "../core", version = "0.34.0" } deno_typescript = { path = "../deno_typescript", version = "0.34.0" } +ansi_term = "0.11.0" atty = "0.2.13" base64 = "0.11.0" bytes = "0.5.3" diff --git a/cli/colors.rs b/cli/colors.rs index 4443a315f..0bf048943 100644 --- a/cli/colors.rs +++ b/cli/colors.rs @@ -1,20 +1,22 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +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; -use std::io::Write; -use termcolor::Color::{Ansi256, Black, Red, White}; -use termcolor::{Ansi, ColorSpec, WriteColor}; 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 = { - env::var_os("NO_COLOR").is_some() - }; + // 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 = { + env::var_os("NO_COLOR").is_some() + }; } /// Helper function to strip ansi codes. @@ -26,60 +28,68 @@ pub fn use_color() -> bool { !(*NO_COLOR) } -fn style(s: &str, colorspec: ColorSpec) -> impl fmt::Display { - let mut v = Vec::new(); - let mut ansi_writer = Ansi::new(&mut v); +pub fn red_bold(s: String) -> impl fmt::Display { + let mut style = Style::new(); if use_color() { - ansi_writer.set_color(&colorspec).unwrap(); + style = style.bold().fg(Red); } - ansi_writer.write_all(s.as_bytes()).unwrap(); - String::from_utf8_lossy(&v).into_owned() -} - -pub fn red_bold(s: String) -> impl fmt::Display { - let mut style_spec = ColorSpec::new(); - style_spec.set_fg(Some(Red)).set_bold(true); - style(&s, style_spec) + style.paint(s) } pub fn italic_bold(s: String) -> impl fmt::Display { - let mut style_spec = ColorSpec::new(); - style_spec.set_bold(true).set_italic(true); - style(&s, style_spec) + 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_spec = ColorSpec::new(); - style_spec.set_bg(Some(White)).set_fg(Some(Black)); - style(&s, style_spec) + 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_spec = ColorSpec::new(); - style_spec.set_fg(Some(Ansi256(11))); - style(&s, style_spec) + 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_spec = ColorSpec::new(); - style_spec.set_fg(Some(Ansi256(14))); - style(&s, style_spec) + 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_spec = ColorSpec::new(); - style_spec.set_fg(Some(Red)); - style(&s, style_spec) + let mut style = Style::new(); + if use_color() { + style = style.fg(Red); + } + style.paint(s) } pub fn green(s: String) -> impl fmt::Display { - let mut style_spec = ColorSpec::new(); - style_spec.set_fg(Some(Ansi256(10))); - style(&s, style_spec) + let mut style = Style::new(); + if use_color() { + style = style.fg(Fixed(10)).bold(); + } + style.paint(s) } pub fn bold(s: String) -> impl fmt::Display { - let mut style_spec = ColorSpec::new(); - style_spec.set_bold(true); - style(&s, style_spec) + let mut style = Style::new(); + if use_color() { + style = style.bold(); + } + style.paint(s) } diff --git a/cli/lib.rs b/cli/lib.rs index 57a96c414..25c4b33a0 100644 --- a/cli/lib.rs +++ b/cli/lib.rs @@ -388,6 +388,9 @@ async fn test_command( } pub fn main() { + #[cfg(windows)] + ansi_term::enable_ansi_support().ok(); // For Windows 10 + log::set_logger(&LOGGER).unwrap(); let args: Vec<String> = env::args().collect(); let flags = flags::flags_from_vec(args); diff --git a/cli/permissions.rs b/cli/permissions.rs index a14a1b4f2..a6d0210ea 100644 --- a/cli/permissions.rs +++ b/cli/permissions.rs @@ -1,7 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -use crate::colors; use crate::flags::DenoFlags; use crate::op_error::OpError; +use ansi_term::Style; #[cfg(not(test))] use atty; use log; @@ -305,7 +305,7 @@ fn permission_prompt(message: &str) -> bool { PERMISSION_EMOJI, message ); // print to stderr so that if deno is > to a file this is still displayed. - eprint!("{}", colors::bold(msg)); + eprint!("{}", Style::new().bold().paint(msg)); loop { let mut input = String::new(); let stdin = io::stdin(); @@ -321,7 +321,7 @@ fn permission_prompt(message: &str) -> bool { // If we don't get a recognized option try again. let msg_again = format!("Unrecognized option '{}' [g/d (g = grant, d = deny)] ", ch); - eprint!("{}", colors::bold(msg_again)); + eprint!("{}", Style::new().bold().paint(msg_again)); } }; } @@ -352,7 +352,9 @@ fn log_perm_access(message: &str) { if log_enabled!(log::Level::Info) { eprintln!( "{}", - colors::bold(format!("{}️ Granted {}", PERMISSION_EMOJI, message)) + Style::new() + .bold() + .paint(format!("{}️ Granted {}", PERMISSION_EMOJI, message)) ); } } diff --git a/std/encoding/testdata/cargo.toml b/std/encoding/testdata/cargo.toml index 667814233..93583edbf 100644 --- a/std/encoding/testdata/cargo.toml +++ b/std/encoding/testdata/cargo.toml @@ -23,6 +23,7 @@ edition = "2018" [dependencies] deno_core = { path = "./core" } +ansi_term = "0.11.0" atty = "0.2.11" dirs = "1.0.5" flatbuffers = "0.5.0" diff --git a/std/encoding/toml_test.ts b/std/encoding/toml_test.ts index 425b8a22c..f1f89b428 100644 --- a/std/encoding/toml_test.ts +++ b/std/encoding/toml_test.ts @@ -260,6 +260,7 @@ Deno.test({ package: { name: "deno", version: "0.3.4", edition: "2018" }, dependencies: { deno_core: { path: "./core" }, + ansi_term: "0.11.0", atty: "0.2.11", dirs: "1.0.5", flatbuffers: "0.5.0", |