summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2024-02-07 11:25:14 -0500
committerGitHub <noreply@github.com>2024-02-07 11:25:14 -0500
commit83d72e5c1c0b983f4b63f3f64f9a1de7600985f4 (patch)
tree49dfd980df2fce512fa13c3650d1c45a3e500a51 /runtime
parenta32e7f0eb23fa17f5af2fc4c8abfd79762934244 (diff)
refactor: extract out `runtime::colors` to `deno_terminal::colors` (#22324)
Diffstat (limited to 'runtime')
-rw-r--r--runtime/Cargo.toml2
-rw-r--r--runtime/colors.rs230
-rw-r--r--runtime/fmt_errors.rs8
-rw-r--r--runtime/lib.rs1
-rw-r--r--runtime/permissions/mod.rs2
-rw-r--r--runtime/permissions/prompter.rs2
-rw-r--r--runtime/web_worker.rs2
-rw-r--r--runtime/worker_bootstrap.rs4
8 files changed, 10 insertions, 241 deletions
diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml
index 083d8005f..1c897b938 100644
--- a/runtime/Cargo.toml
+++ b/runtime/Cargo.toml
@@ -90,6 +90,7 @@ deno_kv.workspace = true
deno_napi.workspace = true
deno_net.workspace = true
deno_node.workspace = true
+deno_terminal.workspace = true
deno_tls.workspace = true
deno_url.workspace = true
deno_web.workspace = true
@@ -119,7 +120,6 @@ ring.workspace = true
rustyline = { workspace = true, features = ["custom-bindings"] }
serde.workspace = true
signal-hook-registry = "1.4.0"
-termcolor = "1.1.3"
tokio.workspace = true
tokio-metrics.workspace = true
uuid.workspace = true
diff --git a/runtime/colors.rs b/runtime/colors.rs
deleted file mode 100644
index ff9ee24db..000000000
--- a/runtime/colors.rs
+++ /dev/null
@@ -1,230 +0,0 @@
-// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
-
-use once_cell::sync::Lazy;
-use std::fmt;
-use std::fmt::Write as _;
-use std::io::IsTerminal;
-use termcolor::Ansi;
-use termcolor::Color::Ansi256;
-use termcolor::Color::Black;
-use termcolor::Color::Blue;
-use termcolor::Color::Cyan;
-use termcolor::Color::Green;
-use termcolor::Color::Magenta;
-use termcolor::Color::Red;
-use termcolor::Color::White;
-use termcolor::Color::Yellow;
-use termcolor::ColorSpec;
-use termcolor::WriteColor;
-
-#[cfg(windows)]
-use termcolor::BufferWriter;
-#[cfg(windows)]
-use termcolor::ColorChoice;
-
-static NO_COLOR: Lazy<bool> = Lazy::new(|| {
- std::env::var_os("NO_COLOR")
- .map(|v| !v.is_empty())
- .unwrap_or(false)
-});
-
-static IS_TTY: Lazy<bool> = Lazy::new(|| std::io::stdout().is_terminal());
-
-pub fn is_tty() -> bool {
- *IS_TTY
-}
-
-pub fn use_color() -> bool {
- !(*NO_COLOR)
-}
-
-#[cfg(windows)]
-pub fn enable_ansi() {
- BufferWriter::stdout(ColorChoice::AlwaysAnsi);
-}
-
-/// A struct that can adapt a `fmt::Write` to a `std::io::Write`. If anything
-/// that can not be represented as UTF-8 is written to this writer, it will
-/// return an error.
-struct StdFmtStdIoWriter<'a>(&'a mut dyn fmt::Write);
-
-impl std::io::Write for StdFmtStdIoWriter<'_> {
- fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
- let str = std::str::from_utf8(buf).map_err(|_| {
- std::io::Error::new(
- std::io::ErrorKind::Other,
- "failed to convert bytes to str",
- )
- })?;
- match self.0.write_str(str) {
- Ok(_) => Ok(buf.len()),
- Err(_) => Err(std::io::Error::new(
- std::io::ErrorKind::Other,
- "failed to write to fmt::Write",
- )),
- }
- }
-
- fn flush(&mut self) -> std::io::Result<()> {
- Ok(())
- }
-}
-
-/// A struct that can adapt a `std::io::Write` to a `fmt::Write`.
-struct StdIoStdFmtWriter<'a>(&'a mut dyn std::io::Write);
-
-impl fmt::Write for StdIoStdFmtWriter<'_> {
- fn write_str(&mut self, s: &str) -> fmt::Result {
- self.0.write_all(s.as_bytes()).map_err(|_| fmt::Error)?;
- Ok(())
- }
-}
-
-pub struct Style<I: fmt::Display> {
- colorspec: ColorSpec,
- inner: I,
-}
-
-impl<I: fmt::Display> fmt::Display for Style<I> {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- if !use_color() {
- return fmt::Display::fmt(&self.inner, f);
- }
- let mut ansi_writer = Ansi::new(StdFmtStdIoWriter(f));
- ansi_writer
- .set_color(&self.colorspec)
- .map_err(|_| fmt::Error)?;
- write!(StdIoStdFmtWriter(&mut ansi_writer), "{}", self.inner)?;
- ansi_writer.reset().map_err(|_| fmt::Error)?;
- Ok(())
- }
-}
-
-#[inline]
-fn style<'a, S: fmt::Display + 'a>(s: S, colorspec: ColorSpec) -> Style<S> {
- Style {
- colorspec,
- inner: s,
- }
-}
-
-pub fn red_bold<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
- let mut style_spec = ColorSpec::new();
- style_spec.set_fg(Some(Red)).set_bold(true);
- style(s, style_spec)
-}
-
-pub fn green_bold<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
- let mut style_spec = ColorSpec::new();
- style_spec.set_fg(Some(Green)).set_bold(true);
- style(s, style_spec)
-}
-
-pub fn yellow_bold<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
- let mut style_spec = ColorSpec::new();
- style_spec.set_fg(Some(Yellow)).set_bold(true);
- style(s, style_spec)
-}
-
-pub fn italic<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
- let mut style_spec = ColorSpec::new();
- style_spec.set_italic(true);
- style(s, style_spec)
-}
-
-pub fn italic_gray<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
- let mut style_spec = ColorSpec::new();
- style_spec.set_fg(Some(Ansi256(8))).set_italic(true);
- style(s, style_spec)
-}
-
-pub fn italic_bold<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
- let mut style_spec = ColorSpec::new();
- style_spec.set_bold(true).set_italic(true);
- style(s, style_spec)
-}
-
-pub fn white_on_red<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
- 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<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
- let mut style_spec = ColorSpec::new();
- style_spec.set_bg(Some(Green)).set_fg(Some(Black));
- style(s, style_spec)
-}
-
-pub fn yellow<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
- let mut style_spec = ColorSpec::new();
- style_spec.set_fg(Some(Yellow));
- style(s, style_spec)
-}
-
-pub fn cyan<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
- let mut style_spec = ColorSpec::new();
- style_spec.set_fg(Some(Cyan));
- style(s, style_spec)
-}
-
-pub fn cyan_with_underline<'a>(
- s: impl fmt::Display + 'a,
-) -> impl fmt::Display + 'a {
- let mut style_spec = ColorSpec::new();
- style_spec.set_fg(Some(Cyan)).set_underline(true);
- style(s, style_spec)
-}
-
-pub fn cyan_bold<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
- let mut style_spec = ColorSpec::new();
- style_spec.set_fg(Some(Cyan)).set_bold(true);
- style(s, style_spec)
-}
-
-pub fn magenta<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
- let mut style_spec = ColorSpec::new();
- style_spec.set_fg(Some(Magenta));
- style(s, style_spec)
-}
-
-pub fn red<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
- let mut style_spec = ColorSpec::new();
- style_spec.set_fg(Some(Red));
- style(s, style_spec)
-}
-
-pub fn green<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
- let mut style_spec = ColorSpec::new();
- style_spec.set_fg(Some(Green));
- style(s, style_spec)
-}
-
-pub fn bold<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
- let mut style_spec = ColorSpec::new();
- style_spec.set_bold(true);
- style(s, style_spec)
-}
-
-pub fn gray<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
- let mut style_spec = ColorSpec::new();
- style_spec.set_fg(Some(Ansi256(245)));
- style(s, style_spec)
-}
-
-pub fn intense_blue<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
- let mut style_spec = ColorSpec::new();
- style_spec.set_fg(Some(Blue)).set_intense(true);
- style(s, style_spec)
-}
-
-pub fn white_bold_on_red<'a>(
- s: impl fmt::Display + 'a,
-) -> impl fmt::Display + 'a {
- let mut style_spec = ColorSpec::new();
- style_spec
- .set_bold(true)
- .set_bg(Some(Red))
- .set_fg(Some(White));
- style(s, style_spec)
-}
diff --git a/runtime/fmt_errors.rs b/runtime/fmt_errors.rs
index 6ec63c007..af7d76051 100644
--- a/runtime/fmt_errors.rs
+++ b/runtime/fmt_errors.rs
@@ -1,12 +1,12 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
//! This mod provides DenoError to unify errors across Deno.
-use crate::colors::cyan;
-use crate::colors::italic_bold;
-use crate::colors::red;
-use crate::colors::yellow;
use deno_core::error::format_file_name;
use deno_core::error::JsError;
use deno_core::error::JsStackFrame;
+use deno_terminal::colors::cyan;
+use deno_terminal::colors::italic_bold;
+use deno_terminal::colors::red;
+use deno_terminal::colors::yellow;
use std::fmt::Write as _;
#[derive(Debug, Clone)]
diff --git a/runtime/lib.rs b/runtime/lib.rs
index fd791974d..b63fd4134 100644
--- a/runtime/lib.rs
+++ b/runtime/lib.rs
@@ -24,7 +24,6 @@ pub use deno_webidl;
pub use deno_websocket;
pub use deno_webstorage;
-pub mod colors;
pub mod errors;
pub mod fmt_errors;
pub mod fs_util;
diff --git a/runtime/permissions/mod.rs b/runtime/permissions/mod.rs
index bdefe4f12..2cdbc6014 100644
--- a/runtime/permissions/mod.rs
+++ b/runtime/permissions/mod.rs
@@ -1,6 +1,5 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
-use crate::colors;
use crate::fs_util::resolve_from_cwd;
use deno_core::error::custom_error;
use deno_core::error::type_error;
@@ -15,6 +14,7 @@ use deno_core::serde_json;
use deno_core::url;
use deno_core::url::Url;
use deno_core::ModuleSpecifier;
+use deno_terminal::colors;
use log;
use once_cell::sync::Lazy;
use std::borrow::Cow;
diff --git a/runtime/permissions/prompter.rs b/runtime/permissions/prompter.rs
index 145e0a82e..f054b31f3 100644
--- a/runtime/permissions/prompter.rs
+++ b/runtime/permissions/prompter.rs
@@ -1,8 +1,8 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
-use crate::colors;
use deno_core::error::AnyError;
use deno_core::parking_lot::Mutex;
+use deno_terminal::colors;
use once_cell::sync::Lazy;
use std::fmt::Write;
use std::io::BufRead;
diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs
index 78674af02..b21833a2e 100644
--- a/runtime/web_worker.rs
+++ b/runtime/web_worker.rs
@@ -1,5 +1,4 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
-use crate::colors;
use crate::inspector_server::InspectorServer;
use crate::ops;
use crate::permissions::PermissionsContainer;
@@ -45,6 +44,7 @@ use deno_fs::FileSystem;
use deno_http::DefaultHttpPropertyExtractor;
use deno_io::Stdio;
use deno_kv::dynamic::MultiBackendDbHandler;
+use deno_terminal::colors;
use deno_tls::RootCertStoreProvider;
use deno_web::create_entangled_message_port;
use deno_web::BlobStore;
diff --git a/runtime/worker_bootstrap.rs b/runtime/worker_bootstrap.rs
index 519abf74c..ca2d4d8ec 100644
--- a/runtime/worker_bootstrap.rs
+++ b/runtime/worker_bootstrap.rs
@@ -6,7 +6,7 @@ use serde::Serialize;
use std::cell::RefCell;
use std::thread;
-use crate::colors;
+use deno_terminal::colors;
/// The log level to use when printing diagnostic log messages, warnings,
/// or errors in the worker.
@@ -77,7 +77,7 @@ impl Default for BootstrapOptions {
user_agent,
cpu_count,
no_color: !colors::use_color(),
- is_tty: colors::is_tty(),
+ is_tty: deno_terminal::is_stdout_tty(),
enable_op_summary_metrics: Default::default(),
enable_testing_features: Default::default(),
log_level: Default::default(),