summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
Diffstat (limited to 'runtime')
-rw-r--r--runtime/colors.rs4
-rw-r--r--runtime/js/40_tty.js5
-rw-r--r--runtime/ops/tty.rs50
-rw-r--r--runtime/permissions/prompter.rs3
4 files changed, 29 insertions, 33 deletions
diff --git a/runtime/colors.rs b/runtime/colors.rs
index a9ad12e8c..fe5a3dcbe 100644
--- a/runtime/colors.rs
+++ b/runtime/colors.rs
@@ -1,8 +1,8 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
-use atty;
use once_cell::sync::Lazy;
use std::fmt;
+use std::io::IsTerminal;
use std::io::Write;
use termcolor::Ansi;
use termcolor::Color::Ansi256;
@@ -25,7 +25,7 @@ use termcolor::ColorChoice;
static NO_COLOR: Lazy<bool> =
Lazy::new(|| std::env::var_os("NO_COLOR").is_some());
-static IS_TTY: Lazy<bool> = Lazy::new(|| atty::is(atty::Stream::Stdout));
+static IS_TTY: Lazy<bool> = Lazy::new(|| std::io::stdout().is_terminal());
pub fn is_tty() -> bool {
*IS_TTY
diff --git a/runtime/js/40_tty.js b/runtime/js/40_tty.js
index 859f49275..7476daad3 100644
--- a/runtime/js/40_tty.js
+++ b/runtime/js/40_tty.js
@@ -4,7 +4,6 @@ const ops = core.ops;
const primordials = globalThis.__bootstrap.primordials;
const {
Uint32Array,
- Uint8Array,
} = primordials;
const size = new Uint32Array(2);
@@ -14,10 +13,8 @@ function consoleSize() {
return { columns: size[0], rows: size[1] };
}
-const isattyBuffer = new Uint8Array(1);
function isatty(rid) {
- ops.op_isatty(rid, isattyBuffer);
- return !!isattyBuffer[0];
+ return ops.op_isatty(rid);
}
export { consoleSize, isatty };
diff --git a/runtime/ops/tty.rs b/runtime/ops/tty.rs
index b24c9d893..1d4c123a2 100644
--- a/runtime/ops/tty.rs
+++ b/runtime/ops/tty.rs
@@ -1,10 +1,13 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use std::io::Error;
+use std::io::IsTerminal;
use deno_core::error::AnyError;
use deno_core::op;
+use deno_core::op2;
use deno_core::OpState;
+use deno_core::ResourceHandle;
#[cfg(unix)]
use deno_core::ResourceId;
@@ -162,32 +165,27 @@ fn op_stdin_set_raw(
}
}
-#[op(fast)]
-fn op_isatty(
- state: &mut OpState,
- rid: u32,
- out: &mut [u8],
-) -> Result<(), AnyError> {
- let raw_fd = state.resource_table.get_fd(rid)?;
- #[cfg(windows)]
- {
- use winapi::shared::minwindef::FALSE;
- use winapi::um::consoleapi;
-
- let handle = raw_fd;
- let mut test_mode: DWORD = 0;
- // If I cannot get mode out of console, it is not a console.
- out[0] =
- // SAFETY: Windows API
- unsafe { consoleapi::GetConsoleMode(handle, &mut test_mode) != FALSE }
- as u8;
- }
- #[cfg(unix)]
- {
- // SAFETY: Posix API
- out[0] = unsafe { libc::isatty(raw_fd as libc::c_int) == 1 } as u8;
- }
- Ok(())
+#[op2(fast)]
+fn op_isatty(state: &mut OpState, rid: u32) -> Result<bool, AnyError> {
+ let handle = state.resource_table.get_handle(rid)?;
+ // TODO(mmastrac): this can migrate to the deno_core implementation when it lands
+ Ok(match handle {
+ ResourceHandle::Fd(fd) if handle.is_valid() => {
+ #[cfg(windows)]
+ {
+ // SAFETY: The resource remains open for the for the duration of borrow_raw
+ unsafe {
+ std::os::windows::io::BorrowedHandle::borrow_raw(fd).is_terminal()
+ }
+ }
+ #[cfg(unix)]
+ {
+ // SAFETY: The resource remains open for the for the duration of borrow_raw
+ unsafe { std::os::fd::BorrowedFd::borrow_raw(fd).is_terminal() }
+ }
+ }
+ _ => false,
+ })
}
#[op(fast)]
diff --git a/runtime/permissions/prompter.rs b/runtime/permissions/prompter.rs
index 93a7c9686..78761aa32 100644
--- a/runtime/permissions/prompter.rs
+++ b/runtime/permissions/prompter.rs
@@ -6,6 +6,7 @@ use deno_core::parking_lot::Mutex;
use once_cell::sync::Lazy;
use std::fmt::Write;
use std::io::BufRead;
+use std::io::IsTerminal;
use std::io::StderrLock;
use std::io::StdinLock;
use std::io::Write as IoWrite;
@@ -84,7 +85,7 @@ impl PermissionPrompter for TtyPrompter {
api_name: Option<&str>,
is_unary: bool,
) -> PromptResponse {
- if !atty::is(atty::Stream::Stdin) || !atty::is(atty::Stream::Stderr) {
+ if !std::io::stdin().is_terminal() || !std::io::stderr().is_terminal() {
return PromptResponse::Deny;
};