diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-05-11 12:48:38 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-11 12:48:38 -0400 |
commit | 0ea6b51bf038108ba62577f7585bc76413f1f199 (patch) | |
tree | 7381a264701338196d0db4973ea971a1bf45ecdd /runtime/ops/tty.rs | |
parent | b67f874b3fb172168997be410b1d5e3c3109c763 (diff) |
fix: stdout and stderr encoding on Windows (#14559)
Diffstat (limited to 'runtime/ops/tty.rs')
-rw-r--r-- | runtime/ops/tty.rs | 118 |
1 files changed, 55 insertions, 63 deletions
diff --git a/runtime/ops/tty.rs b/runtime/ops/tty.rs index 7644d7567..ad152e2da 100644 --- a/runtime/ops/tty.rs +++ b/runtime/ops/tty.rs @@ -1,7 +1,6 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. use super::io::StdFileResource; -use deno_core::error::bad_resource_id; use deno_core::error::AnyError; use deno_core::op; use deno_core::Extension; @@ -89,8 +88,8 @@ fn op_set_raw(state: &mut OpState, args: SetRawArgs) -> Result<(), AnyError> { return Err(deno_core::error::not_supported()); } - let std_file = resource.std_file()?; - let std_file = std_file.lock().unwrap(); // hold the lock + let std_file = resource.std_file(); + let std_file = std_file.lock(); // hold the lock let handle = std_file.as_raw_handle(); if handle == handleapi::INVALID_HANDLE_VALUE { @@ -120,8 +119,8 @@ fn op_set_raw(state: &mut OpState, args: SetRawArgs) -> Result<(), AnyError> { use std::os::unix::io::AsRawFd; let resource = state.resource_table.get::<StdFileResource>(rid)?; - let std_file = resource.std_file()?; - let raw_fd = std_file.lock().unwrap().as_raw_fd(); + let std_file = resource.std_file(); + let raw_fd = std_file.lock().as_raw_fd(); let mut meta_data = resource.metadata_mut(); let maybe_tty_mode = &mut meta_data.tty.mode; @@ -164,25 +163,23 @@ fn op_set_raw(state: &mut OpState, args: SetRawArgs) -> Result<(), AnyError> { #[op] fn op_isatty(state: &mut OpState, rid: ResourceId) -> Result<bool, AnyError> { - let isatty: bool = StdFileResource::with(state, rid, move |r| match r { - Ok(std_file) => { - #[cfg(windows)] - { - use winapi::um::consoleapi; - - let handle = get_windows_handle(std_file)?; - let mut test_mode: DWORD = 0; - // If I cannot get mode out of console, it is not a console. - Ok(unsafe { consoleapi::GetConsoleMode(handle, &mut test_mode) != 0 }) - } - #[cfg(unix)] - { - use std::os::unix::io::AsRawFd; - let raw_fd = std_file.as_raw_fd(); - Ok(unsafe { libc::isatty(raw_fd as libc::c_int) == 1 }) - } + let isatty: bool = StdFileResource::with_file(state, rid, move |std_file| { + #[cfg(windows)] + { + use winapi::shared::minwindef::FALSE; + use winapi::um::consoleapi; + + let handle = get_windows_handle(std_file)?; + let mut test_mode: DWORD = 0; + // If I cannot get mode out of console, it is not a console. + Ok(unsafe { consoleapi::GetConsoleMode(handle, &mut test_mode) != FALSE }) + } + #[cfg(unix)] + { + use std::os::unix::io::AsRawFd; + let raw_fd = std_file.as_raw_fd(); + Ok(unsafe { libc::isatty(raw_fd as libc::c_int) == 1 }) } - _ => Ok(false), })?; Ok(isatty) } @@ -200,52 +197,47 @@ fn op_console_size( ) -> Result<ConsoleSize, AnyError> { super::check_unstable(state, "Deno.consoleSize"); - let size = StdFileResource::with(state, rid, move |r| match r { - Ok(std_file) => { - #[cfg(windows)] - { - use std::os::windows::io::AsRawHandle; - let handle = std_file.as_raw_handle(); - - unsafe { - let mut bufinfo: winapi::um::wincon::CONSOLE_SCREEN_BUFFER_INFO = - std::mem::zeroed(); - - if winapi::um::wincon::GetConsoleScreenBufferInfo( - handle, - &mut bufinfo, - ) == 0 - { - return Err(Error::last_os_error().into()); - } - - Ok(ConsoleSize { - columns: bufinfo.dwSize.X as u32, - rows: bufinfo.dwSize.Y as u32, - }) + let size = StdFileResource::with_file(state, rid, move |std_file| { + #[cfg(windows)] + { + use std::os::windows::io::AsRawHandle; + let handle = std_file.as_raw_handle(); + + unsafe { + let mut bufinfo: winapi::um::wincon::CONSOLE_SCREEN_BUFFER_INFO = + std::mem::zeroed(); + + if winapi::um::wincon::GetConsoleScreenBufferInfo(handle, &mut bufinfo) + == 0 + { + return Err(Error::last_os_error().into()); } + + Ok(ConsoleSize { + columns: bufinfo.dwSize.X as u32, + rows: bufinfo.dwSize.Y as u32, + }) } + } - #[cfg(unix)] - { - use std::os::unix::io::AsRawFd; - - let fd = std_file.as_raw_fd(); - unsafe { - let mut size: libc::winsize = std::mem::zeroed(); - if libc::ioctl(fd, libc::TIOCGWINSZ, &mut size as *mut _) != 0 { - return Err(Error::last_os_error().into()); - } - - // TODO (caspervonb) return a tuple instead - Ok(ConsoleSize { - columns: size.ws_col as u32, - rows: size.ws_row as u32, - }) + #[cfg(unix)] + { + use std::os::unix::io::AsRawFd; + + let fd = std_file.as_raw_fd(); + unsafe { + let mut size: libc::winsize = std::mem::zeroed(); + if libc::ioctl(fd, libc::TIOCGWINSZ, &mut size as *mut _) != 0 { + return Err(Error::last_os_error().into()); } + + // TODO (caspervonb) return a tuple instead + Ok(ConsoleSize { + columns: size.ws_col as u32, + rows: size.ws_row as u32, + }) } } - Err(_) => Err(bad_resource_id()), })?; Ok(size) |