Skip to content

Commit

Permalink
feat(unstable): Deno.setRaw -> Deno.stdin.setRaw (#15797)
Browse files Browse the repository at this point in the history
Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
  • Loading branch information
lucacasonato and bartlomieju committed Sep 28, 2022
1 parent fa9e7aa commit 70bc0eb
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 50 deletions.
1 change: 0 additions & 1 deletion cli/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ const UNSTABLE_DENO_PROPS: &[&str] = &[
"osRelease",
"ppid",
"removeSignalListener",
"setRaw",
"shutdown",
"Signal",
"startTls",
Expand Down
27 changes: 27 additions & 0 deletions cli/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1331,13 +1331,40 @@ 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
*/
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`.
*
Expand Down
34 changes: 1 addition & 33 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/integration/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/testdata/run/unstable_worker.ts.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[Function: query]
[Function: setRaw]
[Function: consoleSize]
2 changes: 1 addition & 1 deletion cli/tests/testdata/workers/worker_unstable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
console.log(Deno.permissions.query);
console.log(Deno.setRaw);
console.log(Deno.consoleSize);
self.onmessage = () => {
self.close();
};
2 changes: 1 addition & 1 deletion cli/tests/unit/tty_color_test.ts
Original file line number Diff line number Diff line change
@@ -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 } },
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/unit/tty_test.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
5 changes: 5 additions & 0 deletions runtime/js/40_files.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@
}
return this.#readable;
}

setRaw(mode, options = {}) {
const cbreak = !!(options.cbreak ?? false);
ops.op_stdin_set_raw(mode, cbreak);
}
}

class Stdout {
Expand Down
6 changes: 0 additions & 6 deletions runtime/js/40_tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
1 change: 0 additions & 1 deletion runtime/js/90_deno_ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@
};

__bootstrap.denoNsUnstable = {
setRaw: __bootstrap.tty.setRaw,
consoleSize: __bootstrap.tty.consoleSize,
DiagnosticCategory: __bootstrap.diagnostics.DiagnosticCategory,
loadavg: __bootstrap.os.loadavg,
Expand Down
9 changes: 5 additions & 4 deletions runtime/ops/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,22 @@ 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(),
])
.build()
}

#[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
Expand Down

0 comments on commit 70bc0eb

Please sign in to comment.