1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

refactor: extract out runtime::colors to deno_terminal::colors (#22324)

This commit is contained in:
David Sherret 2024-02-07 11:25:14 -05:00 committed by GitHub
parent a32e7f0eb2
commit 83d72e5c1c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 40 additions and 258 deletions

13
Cargo.lock generated
View file

@ -994,6 +994,7 @@ dependencies = [
"deno_runtime",
"deno_semver",
"deno_task_shell",
"deno_terminal",
"dissimilar",
"dotenvy",
"dprint-plugin-json",
@ -1681,6 +1682,7 @@ dependencies = [
"deno_napi",
"deno_net",
"deno_node",
"deno_terminal",
"deno_tls",
"deno_url",
"deno_web",
@ -1712,7 +1714,6 @@ dependencies = [
"rustyline",
"serde",
"signal-hook-registry",
"termcolor",
"test_util",
"tokio",
"tokio-metrics",
@ -1752,6 +1753,16 @@ dependencies = [
"tokio-util",
]
[[package]]
name = "deno_terminal"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b847702ef57565e1417fad2291f65a3c8a5ecf2ba38d64e56f02828e7546d891"
dependencies = [
"once_cell",
"termcolor",
]
[[package]]
name = "deno_tls"
version = "0.123.0"

View file

@ -48,6 +48,7 @@ deno_bench_util = { version = "0.130.0", path = "./bench_util" }
deno_lockfile = "0.18.0"
deno_media_type = { version = "0.1.1", features = ["module_specifier"] }
deno_runtime = { version = "0.144.0", path = "./runtime" }
deno_terminal = "0.1.0"
napi_sym = { version = "0.66.0", path = "./cli/napi/sym" }
test_util = { path = "./test_util" }

View file

@ -66,6 +66,7 @@ deno_npm = "=0.16.0"
deno_runtime = { workspace = true, features = ["include_js_files_for_snapshotting"] }
deno_semver = "=0.5.4"
deno_task_shell = "=0.14.3"
deno_terminal.workspace = true
eszip = "=0.60.0"
napi_sym.workspace = true

View file

@ -42,7 +42,6 @@ use deno_core::normalize_path;
use deno_core::parking_lot::Mutex;
use deno_core::serde_json;
use deno_core::url::Url;
use deno_runtime::colors;
use deno_runtime::deno_node::PackageJson;
use deno_runtime::deno_tls::deno_native_certs::load_native_certs;
use deno_runtime::deno_tls::rustls;
@ -51,6 +50,7 @@ use deno_runtime::deno_tls::rustls_pemfile;
use deno_runtime::deno_tls::webpki_roots;
use deno_runtime::inspector_server::InspectorServer;
use deno_runtime::permissions::PermissionsOptions;
use deno_terminal::colors;
use dotenvy::from_filename;
use once_cell::sync::Lazy;
use once_cell::sync::OnceCell;

View file

@ -11,7 +11,7 @@ use deno_ast::SourcePos;
use deno_ast::SourceRange;
use deno_ast::SourceRanged;
use deno_ast::SourceTextInfo;
use deno_runtime::colors;
use deno_terminal::colors;
use unicode_width::UnicodeWidthStr;
use crate::cache::LazyGraphSourceParser;

View file

@ -42,9 +42,9 @@ use deno_core::error::JsError;
use deno_core::futures::FutureExt;
use deno_core::unsync::JoinHandle;
use deno_npm::resolution::SnapshotFromLockfileError;
use deno_runtime::colors;
use deno_runtime::fmt_errors::format_js_error;
use deno_runtime::tokio_util::create_and_run_current_thread_with_maybe_metrics;
use deno_terminal::colors;
use factory::CliFactory;
use std::env;
use std::env::current_exe;

View file

@ -52,10 +52,10 @@ use deno_graph::JsonModule;
use deno_graph::Module;
use deno_graph::Resolution;
use deno_lockfile::Lockfile;
use deno_runtime::colors;
use deno_runtime::deno_fs;
use deno_runtime::permissions::PermissionsContainer;
use deno_semver::npm::NpmPackageReqReference;
use deno_terminal::colors;
use std::borrow::Cow;
use std::collections::HashSet;
use std::pin::Pin;

View file

@ -4,7 +4,7 @@ use std::path::PathBuf;
use deno_core::error::AnyError;
use deno_graph::Module;
use deno_runtime::colors;
use deno_terminal::colors;
use crate::args::BundleFlags;
use crate::args::CliOptions;

View file

@ -9,8 +9,8 @@ use deno_ast::ModuleSpecifier;
use deno_core::error::AnyError;
use deno_graph::Module;
use deno_graph::ModuleGraph;
use deno_runtime::colors;
use deno_runtime::deno_node::NodeResolver;
use deno_terminal::colors;
use once_cell::sync::Lazy;
use regex::Regex;

View file

@ -11,7 +11,7 @@ use deno_core::error::generic_error;
use deno_core::error::AnyError;
use deno_core::resolve_url_or_path;
use deno_graph::GraphKind;
use deno_runtime::colors;
use deno_terminal::colors;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;

View file

@ -20,10 +20,10 @@ use deno_graph::Resolution;
use deno_npm::resolution::NpmResolutionSnapshot;
use deno_npm::NpmPackageId;
use deno_npm::NpmResolutionPackage;
use deno_runtime::colors;
use deno_semver::npm::NpmPackageNvReference;
use deno_semver::npm::NpmPackageReqReference;
use deno_semver::package::PackageNv;
use deno_terminal::colors;
use crate::args::Flags;
use crate::args::InfoFlags;

View file

@ -13,11 +13,11 @@ use deno_core::located_script_name;
use deno_core::resolve_url_or_path;
use deno_core::serde::Deserialize;
use deno_core::serde_json;
use deno_runtime::colors;
use deno_runtime::deno_io::Stdio;
use deno_runtime::deno_io::StdioPipe;
use deno_runtime::permissions::Permissions;
use deno_runtime::permissions::PermissionsContainer;
use deno_terminal::colors;
use tokio::sync::mpsc;
use tokio::sync::mpsc::unbounded_channel;
use tokio::sync::mpsc::UnboundedSender;

View file

@ -15,8 +15,8 @@ use deno_core::futures::FutureExt;
use deno_core::serde_json;
use deno_core::serde_json::json;
use deno_core::unsync::JoinSet;
use deno_runtime::colors;
use deno_runtime::deno_fetch::reqwest;
use deno_terminal::colors;
use import_map::ImportMap;
use lsp_types::Url;
use serde::Serialize;

View file

@ -11,7 +11,7 @@ use deno_core::serde_json::json;
use deno_core::serde_json::{self};
use deno_core::url::Url;
use deno_core::LocalInspectorSession;
use deno_runtime::colors;
use deno_terminal::colors;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;

View file

@ -2,7 +2,7 @@
use deno_ast::ModuleSpecifier;
use deno_graph::ModuleGraph;
use deno_runtime::colors;
use deno_terminal::colors;
use deno_core::serde::Deserialize;
use deno_core::serde::Deserializer;

View file

@ -2,7 +2,7 @@
use std::time::Duration;
use deno_runtime::colors;
use deno_terminal::colors;
use crate::util::display::human_download_size;

View file

@ -23,7 +23,6 @@ use deno_core::PollEventLoopOptions;
use deno_core::SharedArrayBufferStore;
use deno_core::SourceMapGetter;
use deno_lockfile::Lockfile;
use deno_runtime::colors;
use deno_runtime::deno_broadcast_channel::InMemoryBroadcastChannel;
use deno_runtime::deno_fs;
use deno_runtime::deno_node;
@ -44,6 +43,7 @@ use deno_runtime::BootstrapOptions;
use deno_runtime::WorkerLogLevel;
use deno_semver::npm::NpmPackageReqReference;
use deno_semver::package::PackageReqReference;
use deno_terminal::colors;
use tokio::select;
use crate::args::package_json::PackageJsonDeps;
@ -583,7 +583,7 @@ impl CliMainWorkerFactory {
locale: deno_core::v8::icu::get_language_tag(),
location: shared.options.location.clone(),
no_color: !colors::use_color(),
is_tty: colors::is_tty(),
is_tty: deno_terminal::is_stdout_tty(),
unstable: shared.options.unstable,
unstable_features,
user_agent: version::get_user_agent().to_string(),
@ -789,7 +789,7 @@ fn create_web_worker_callback(
locale: deno_core::v8::icu::get_language_tag(),
location: Some(args.main_module.clone()),
no_color: !colors::use_color(),
is_tty: colors::is_tty(),
is_tty: deno_terminal::is_stdout_tty(),
unstable: shared.options.unstable,
unstable_features,
user_agent: version::get_user_agent().to_string(),

View file

@ -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

View file

@ -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)
}

View file

@ -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)]

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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(),