diff options
Diffstat (limited to 'runtime/ops/tty.rs')
-rw-r--r-- | runtime/ops/tty.rs | 88 |
1 files changed, 35 insertions, 53 deletions
diff --git a/runtime/ops/tty.rs b/runtime/ops/tty.rs index 2e6d1ea7c..e267fc07f 100644 --- a/runtime/ops/tty.rs +++ b/runtime/ops/tty.rs @@ -5,9 +5,6 @@ use deno_core::error::AnyError; use deno_core::op; use deno_core::Extension; use deno_core::OpState; -use deno_core::ResourceId; -use serde::Deserialize; -use serde::Serialize; use std::io::Error; #[cfg(unix)] @@ -50,27 +47,15 @@ pub fn init() -> Extension { .build() } -#[derive(Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct SetRawOptions { +#[op(fast)] +fn op_set_raw( + state: &mut OpState, + rid: u32, + is_raw: bool, cbreak: bool, -} - -#[derive(Deserialize)] -pub struct SetRawArgs { - rid: ResourceId, - mode: bool, - options: SetRawOptions, -} - -#[op] -fn op_set_raw(state: &mut OpState, args: SetRawArgs) -> Result<(), AnyError> { +) -> Result<(), AnyError> { super::check_unstable(state, "Deno.setRaw"); - let rid = args.rid; - let is_raw = args.mode; - let cbreak = args.options.cbreak; - // From https://github.com/kkawakam/rustyline/blob/master/src/tty/windows.rs // and https://github.com/kkawakam/rustyline/blob/master/src/tty/unix.rs // and https://github.com/crossterm-rs/crossterm/blob/e35d4d2c1cc4c919e36d242e014af75f6127ab50/src/terminal/sys/windows.rs @@ -168,9 +153,13 @@ 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_file(state, rid, move |std_file| { +#[op(fast)] +fn op_isatty( + state: &mut OpState, + rid: u32, + out: &mut [u8], +) -> Result<(), AnyError> { + StdFileResource::with_file(state, rid, move |std_file| { #[cfg(windows)] { use winapi::shared::minwindef::FALSE; @@ -181,7 +170,11 @@ fn op_isatty(state: &mut OpState, rid: ResourceId) -> Result<bool, AnyError> { // If I cannot get mode out of console, it is not a console. // TODO(bartlomieju): #[allow(clippy::undocumented_unsafe_blocks)] - Ok(unsafe { consoleapi::GetConsoleMode(handle, &mut test_mode) != FALSE }) + { + out[0] = unsafe { + consoleapi::GetConsoleMode(handle, &mut test_mode) != FALSE + } as u8; + } } #[cfg(unix)] { @@ -189,26 +182,22 @@ fn op_isatty(state: &mut OpState, rid: ResourceId) -> Result<bool, AnyError> { let raw_fd = std_file.as_raw_fd(); // TODO(bartlomieju): #[allow(clippy::undocumented_unsafe_blocks)] - Ok(unsafe { libc::isatty(raw_fd as libc::c_int) == 1 }) + { + out[0] = unsafe { libc::isatty(raw_fd as libc::c_int) == 1 } as u8; + } } - })?; - Ok(isatty) -} - -#[derive(Serialize)] -struct ConsoleSize { - columns: u32, - rows: u32, + Ok(()) + }) } -#[op] +#[op(fast)] fn op_console_size( state: &mut OpState, - rid: ResourceId, -) -> Result<ConsoleSize, AnyError> { + rid: u32, + result: &mut [u32], +) -> Result<(), AnyError> { super::check_unstable(state, "Deno.consoleSize"); - - let size = StdFileResource::with_file(state, rid, move |std_file| { + StdFileResource::with_file(state, rid, move |std_file| { #[cfg(windows)] { use std::os::windows::io::AsRawHandle; @@ -224,11 +213,9 @@ fn op_console_size( { return Err(Error::last_os_error().into()); } - - Ok(ConsoleSize { - columns: bufinfo.dwSize.X as u32, - rows: bufinfo.dwSize.Y as u32, - }) + result[0] = bufinfo.dwSize.X as u32; + result[1] = bufinfo.dwSize.Y as u32; + Ok(()) } } @@ -244,15 +231,10 @@ fn op_console_size( 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, - }) + result[0] = size.ws_col as u32; + result[1] = size.ws_row as u32; + Ok(()) } } - })?; - - Ok(size) + }) } |