1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-12 00:54:02 -05:00

feat(unstable): Deno.setRaw -> Deno.stdin.setRaw (#15797)

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
This commit is contained in:
Luca Casonato 2022-09-28 15:03:56 +02:00 committed by GitHub
parent fa9e7aab6d
commit 70bc0eb72b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 43 additions and 50 deletions

View file

@ -49,7 +49,6 @@ const UNSTABLE_DENO_PROPS: &[&str] = &[
"osRelease", "osRelease",
"ppid", "ppid",
"removeSignalListener", "removeSignalListener",
"setRaw",
"shutdown", "shutdown",
"Signal", "Signal",
"startTls", "startTls",

View file

@ -1331,6 +1331,13 @@ declare namespace Deno {
readonly writable: WritableStream<Uint8Array>; readonly writable: WritableStream<Uint8Array>;
} }
/** **UNSTABLE**: new API, yet to be vetted.
*
* @category I/O */
export interface SetRawOptions {
cbreak: boolean;
}
/** A handle for `stdin`. /** A handle for `stdin`.
* *
* @category I/O * @category I/O
@ -1338,6 +1345,26 @@ declare namespace Deno {
export const stdin: Reader & ReaderSync & Closer & { export const stdin: Reader & ReaderSync & Closer & {
readonly rid: number; readonly rid: number;
readonly readable: ReadableStream<Uint8Array>; readonly readable: ReadableStream<Uint8Array>;
/** **UNSTABLE**: new API, yet to be vetted.
*
* Set TTY to be under raw mode or not. In raw mode, characters are read and
* returned as is, without being processed. All special processing of
* characters by the terminal is disabled, including echoing input
* characters. Reading from a TTY device in raw mode is faster than reading
* from a TTY device in canonical mode.
*
* The `cbreak` option can be used to indicate that characters that
* correspond to a signal should still be generated. When disabling raw
* mode, this option is ignored. This functionality currently only works on
* Linux and Mac OS.
*
* ```ts
* Deno.stdin.setRaw(true, { cbreak: true });
* ```
*
* @category I/O
*/
setRaw(mode: boolean, options?: SetRawOptions): void;
}; };
/** A handle for `stdout`. /** A handle for `stdout`.
* *

View file

@ -880,39 +880,7 @@ declare namespace Deno {
symbols: S, symbols: S,
): DynamicLibrary<S>; ): DynamicLibrary<S>;
/** **UNSTABLE**: New API, yet to be vetted. /** **UNSTABLE**: needs investigation into high precision time.
*
* @category I/O
*/
export type SetRawOptions = {
cbreak: boolean;
};
/** **UNSTABLE**: New API, yet to be vetted.
*
* Set TTY to be under raw mode or not. In raw mode, characters are read and
* returned as is, without being processed. All special processing of
* characters by the terminal is disabled, including echoing input characters.
* Reading from a TTY device in raw mode is faster than reading from a TTY
* device in canonical mode.
*
* The `cbreak` option can be used to indicate that characters that correspond
* to a signal should still be generated. When disabling raw mode, this option
* is ignored. This functionality currently only works on Linux and Mac OS.
*
* ```ts
* Deno.setRaw(Deno.stdin.rid, true, { cbreak: true });
* ```
*
* @category I/O
*/
export function setRaw(
rid: number,
mode: boolean,
options?: SetRawOptions,
): void;
/** **UNSTABLE**: New API, yet to be vetted.
* *
* Synchronously changes the access (`atime`) and modification (`mtime`) times * Synchronously changes the access (`atime`) and modification (`mtime`) times
* of a file system object referenced by `path`. Given times are either in * of a file system object referenced by `path`. Given times are either in

View file

@ -3303,7 +3303,7 @@ fn set_raw_should_not_panic_on_no_tty() {
let output = util::deno_cmd() let output = util::deno_cmd()
.arg("eval") .arg("eval")
.arg("--unstable") .arg("--unstable")
.arg("Deno.setRaw(Deno.stdin.rid, true)") .arg("Deno.stdin.setRaw(true)")
// stdin set to piped so it certainly does not refer to TTY // stdin set to piped so it certainly does not refer to TTY
.stdin(std::process::Stdio::piped()) .stdin(std::process::Stdio::piped())
// stderr is piped so we can capture output. // stderr is piped so we can capture output.

View file

@ -1,2 +1,2 @@
[Function: query] [Function: query]
[Function: setRaw] [Function: consoleSize]

View file

@ -1,5 +1,5 @@
console.log(Deno.permissions.query); console.log(Deno.permissions.query);
console.log(Deno.setRaw); console.log(Deno.consoleSize);
self.onmessage = () => { self.onmessage = () => {
self.close(); self.close();
}; };

View file

@ -1,7 +1,7 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "./test_util.ts"; import { assertEquals } from "./test_util.ts";
// Note tests for Deno.setRaw is in integration tests. // Note tests for Deno.stdin.setRaw is in integration tests.
Deno.test( Deno.test(
{ permissions: { run: true, read: true } }, { permissions: { run: true, read: true } },

View file

@ -1,7 +1,7 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert, assertThrows } from "./test_util.ts"; import { assert, assertThrows } from "./test_util.ts";
// Note tests for Deno.setRaw is in integration tests. // Note tests for Deno.stdin.setRaw is in integration tests.
Deno.test({ permissions: { read: true } }, function consoleSizeFile() { Deno.test({ permissions: { read: true } }, function consoleSizeFile() {
const file = Deno.openSync("cli/tests/testdata/assets/hello.txt"); const file = Deno.openSync("cli/tests/testdata/assets/hello.txt");

View file

@ -181,6 +181,11 @@
} }
return this.#readable; return this.#readable;
} }
setRaw(mode, options = {}) {
const cbreak = !!(options.cbreak ?? false);
ops.op_stdin_set_raw(mode, cbreak);
}
} }
class Stdout { class Stdout {

View file

@ -21,14 +21,8 @@
return !!isattyBuffer[0]; return !!isattyBuffer[0];
} }
const DEFAULT_CBREAK = false;
function setRaw(rid, mode, options = {}) {
ops.op_set_raw(rid, mode, options.cbreak || DEFAULT_CBREAK);
}
window.__bootstrap.tty = { window.__bootstrap.tty = {
consoleSize, consoleSize,
isatty, isatty,
setRaw,
}; };
})(this); })(this);

View file

@ -117,7 +117,6 @@
}; };
__bootstrap.denoNsUnstable = { __bootstrap.denoNsUnstable = {
setRaw: __bootstrap.tty.setRaw,
consoleSize: __bootstrap.tty.consoleSize, consoleSize: __bootstrap.tty.consoleSize,
DiagnosticCategory: __bootstrap.diagnostics.DiagnosticCategory, DiagnosticCategory: __bootstrap.diagnostics.DiagnosticCategory,
loadavg: __bootstrap.os.loadavg, loadavg: __bootstrap.os.loadavg,

View file

@ -40,7 +40,7 @@ fn get_windows_handle(
pub fn init() -> Extension { pub fn init() -> Extension {
Extension::builder() Extension::builder()
.ops(vec![ .ops(vec![
op_set_raw::decl(), op_stdin_set_raw::decl(),
op_isatty::decl(), op_isatty::decl(),
op_console_size::decl(), op_console_size::decl(),
]) ])
@ -48,13 +48,14 @@ pub fn init() -> Extension {
} }
#[op(fast)] #[op(fast)]
fn op_set_raw( fn op_stdin_set_raw(
state: &mut OpState, state: &mut OpState,
rid: u32,
is_raw: bool, is_raw: bool,
cbreak: bool, cbreak: bool,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
super::check_unstable(state, "Deno.setRaw"); super::check_unstable(state, "Deno.stdin.setRaw");
let rid = 0; // stdin is always rid=0
// From https://github.com/kkawakam/rustyline/blob/master/src/tty/windows.rs // 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/kkawakam/rustyline/blob/master/src/tty/unix.rs