diff options
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/js/40_tty.js | 4 | ||||
-rw-r--r-- | runtime/js/90_deno_ns.js | 2 | ||||
-rw-r--r-- | runtime/ops/tty.rs | 84 |
3 files changed, 54 insertions, 36 deletions
diff --git a/runtime/js/40_tty.js b/runtime/js/40_tty.js index be5154fa2..8ce3e83ef 100644 --- a/runtime/js/40_tty.js +++ b/runtime/js/40_tty.js @@ -10,8 +10,8 @@ const ops = core.ops; const size = new Uint32Array(2); - function consoleSize(rid) { - ops.op_console_size(rid, size); + function consoleSize() { + ops.op_console_size(size); return { columns: size[0], rows: size[1] }; } diff --git a/runtime/js/90_deno_ns.js b/runtime/js/90_deno_ns.js index 3bfd26da7..00d343f74 100644 --- a/runtime/js/90_deno_ns.js +++ b/runtime/js/90_deno_ns.js @@ -120,10 +120,10 @@ refTimer: __bootstrap.timers.refTimer, unrefTimer: __bootstrap.timers.unrefTimer, hostname: __bootstrap.os.hostname, + consoleSize: __bootstrap.tty.consoleSize, }; __bootstrap.denoNsUnstable = { - consoleSize: __bootstrap.tty.consoleSize, DiagnosticCategory: __bootstrap.diagnostics.DiagnosticCategory, osRelease: __bootstrap.os.osRelease, systemMemoryInfo: __bootstrap.os.systemMemoryInfo, diff --git a/runtime/ops/tty.rs b/runtime/ops/tty.rs index f8dae1d26..6382f967f 100644 --- a/runtime/ops/tty.rs +++ b/runtime/ops/tty.rs @@ -192,48 +192,66 @@ fn op_isatty( #[op(fast)] fn op_console_size( state: &mut OpState, - rid: u32, result: &mut [u32], ) -> Result<(), AnyError> { - super::check_unstable(state, "Deno.consoleSize"); - StdFileResource::with_file(state, rid, move |std_file| { - #[cfg(windows)] - { - use std::os::windows::io::AsRawHandle; - let handle = std_file.as_raw_handle(); + fn check_console_size( + state: &mut OpState, + result: &mut [u32], + rid: u32, + ) -> Result<(), AnyError> { + StdFileResource::with_file(state, rid, move |std_file| { + #[cfg(windows)] + { + use std::os::windows::io::AsRawHandle; + let handle = std_file.as_raw_handle(); - // SAFETY: winapi calls - unsafe { - let mut bufinfo: winapi::um::wincon::CONSOLE_SCREEN_BUFFER_INFO = - std::mem::zeroed(); + // SAFETY: winapi calls + 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()); + if winapi::um::wincon::GetConsoleScreenBufferInfo( + handle, + &mut bufinfo, + ) == 0 + { + return Err(Error::last_os_error().into()); + } + result[0] = bufinfo.dwSize.X as u32; + result[1] = bufinfo.dwSize.Y as u32; + Ok(()) } - result[0] = bufinfo.dwSize.X as u32; - result[1] = bufinfo.dwSize.Y as u32; - Ok(()) } - } - #[cfg(unix)] - { - use std::os::unix::io::AsRawFd; + #[cfg(unix)] + { + use std::os::unix::io::AsRawFd; - let fd = std_file.as_raw_fd(); - // TODO(bartlomieju): - #[allow(clippy::undocumented_unsafe_blocks)] - 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()); + let fd = std_file.as_raw_fd(); + // TODO(bartlomieju): + #[allow(clippy::undocumented_unsafe_blocks)] + 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()); + } + result[0] = size.ws_col as u32; + result[1] = size.ws_row as u32; + Ok(()) } - result[0] = size.ws_col as u32; - result[1] = size.ws_row as u32; - Ok(()) } + }) + } + + let mut last_result = Ok(()); + // Since stdio might be piped we try to get the size of the console for all + // of them and return the first one that succeeds. + for rid in [0, 1, 2] { + last_result = check_console_size(state, result, rid); + if last_result.is_ok() { + return last_result; } - }) + } + + last_result } |