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:
parent
fa9e7aab6d
commit
70bc0eb72b
12 changed files with 43 additions and 50 deletions
|
@ -49,7 +49,6 @@ const UNSTABLE_DENO_PROPS: &[&str] = &[
|
|||
"osRelease",
|
||||
"ppid",
|
||||
"removeSignalListener",
|
||||
"setRaw",
|
||||
"shutdown",
|
||||
"Signal",
|
||||
"startTls",
|
||||
|
|
27
cli/dts/lib.deno.ns.d.ts
vendored
27
cli/dts/lib.deno.ns.d.ts
vendored
|
@ -1331,6 +1331,13 @@ declare namespace Deno {
|
|||
readonly writable: WritableStream<Uint8Array>;
|
||||
}
|
||||
|
||||
/** **UNSTABLE**: new API, yet to be vetted.
|
||||
*
|
||||
* @category I/O */
|
||||
export interface SetRawOptions {
|
||||
cbreak: boolean;
|
||||
}
|
||||
|
||||
/** A handle for `stdin`.
|
||||
*
|
||||
* @category I/O
|
||||
|
@ -1338,6 +1345,26 @@ declare namespace Deno {
|
|||
export const stdin: Reader & ReaderSync & Closer & {
|
||||
readonly rid: number;
|
||||
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`.
|
||||
*
|
||||
|
|
34
cli/dts/lib.deno.unstable.d.ts
vendored
34
cli/dts/lib.deno.unstable.d.ts
vendored
|
@ -880,39 +880,7 @@ declare namespace Deno {
|
|||
symbols: S,
|
||||
): DynamicLibrary<S>;
|
||||
|
||||
/** **UNSTABLE**: New API, yet to be vetted.
|
||||
*
|
||||
* @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.
|
||||
/** **UNSTABLE**: needs investigation into high precision time.
|
||||
*
|
||||
* Synchronously changes the access (`atime`) and modification (`mtime`) times
|
||||
* of a file system object referenced by `path`. Given times are either in
|
||||
|
|
|
@ -3303,7 +3303,7 @@ fn set_raw_should_not_panic_on_no_tty() {
|
|||
let output = util::deno_cmd()
|
||||
.arg("eval")
|
||||
.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(std::process::Stdio::piped())
|
||||
// stderr is piped so we can capture output.
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
[Function: query]
|
||||
[Function: setRaw]
|
||||
[Function: consoleSize]
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
console.log(Deno.permissions.query);
|
||||
console.log(Deno.setRaw);
|
||||
console.log(Deno.consoleSize);
|
||||
self.onmessage = () => {
|
||||
self.close();
|
||||
};
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
||||
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(
|
||||
{ permissions: { run: true, read: true } },
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
||||
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() {
|
||||
const file = Deno.openSync("cli/tests/testdata/assets/hello.txt");
|
||||
|
|
|
@ -181,6 +181,11 @@
|
|||
}
|
||||
return this.#readable;
|
||||
}
|
||||
|
||||
setRaw(mode, options = {}) {
|
||||
const cbreak = !!(options.cbreak ?? false);
|
||||
ops.op_stdin_set_raw(mode, cbreak);
|
||||
}
|
||||
}
|
||||
|
||||
class Stdout {
|
||||
|
|
|
@ -21,14 +21,8 @@
|
|||
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 = {
|
||||
consoleSize,
|
||||
isatty,
|
||||
setRaw,
|
||||
};
|
||||
})(this);
|
||||
|
|
|
@ -117,7 +117,6 @@
|
|||
};
|
||||
|
||||
__bootstrap.denoNsUnstable = {
|
||||
setRaw: __bootstrap.tty.setRaw,
|
||||
consoleSize: __bootstrap.tty.consoleSize,
|
||||
DiagnosticCategory: __bootstrap.diagnostics.DiagnosticCategory,
|
||||
loadavg: __bootstrap.os.loadavg,
|
||||
|
|
|
@ -40,7 +40,7 @@ fn get_windows_handle(
|
|||
pub fn init() -> Extension {
|
||||
Extension::builder()
|
||||
.ops(vec![
|
||||
op_set_raw::decl(),
|
||||
op_stdin_set_raw::decl(),
|
||||
op_isatty::decl(),
|
||||
op_console_size::decl(),
|
||||
])
|
||||
|
@ -48,13 +48,14 @@ pub fn init() -> Extension {
|
|||
}
|
||||
|
||||
#[op(fast)]
|
||||
fn op_set_raw(
|
||||
fn op_stdin_set_raw(
|
||||
state: &mut OpState,
|
||||
rid: u32,
|
||||
is_raw: bool,
|
||||
cbreak: bool,
|
||||
) -> 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
|
||||
// and https://github.com/kkawakam/rustyline/blob/master/src/tty/unix.rs
|
||||
|
|
Loading…
Reference in a new issue