summaryrefslogtreecommitdiff
path: root/cli/js
diff options
context:
space:
mode:
authorKevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com>2020-02-25 22:01:24 -0800
committerGitHub <noreply@github.com>2020-02-26 01:01:24 -0500
commit5946808f66aab1983ade3db2541734bb43626a72 (patch)
treeadb526497a9efc29d1b5744ae52449f08f453ef0 /cli/js
parente53064c4f22efeb8a4eda2712e15c77d2699a686 (diff)
tty: Deno.setRaw(rid, mode) to turn on/off raw mode (#3958)
Diffstat (limited to 'cli/js')
-rw-r--r--cli/js/deno.ts2
-rw-r--r--cli/js/lib.deno.ns.d.ts25
-rw-r--r--cli/js/os.ts7
-rw-r--r--cli/js/os_test.ts4
-rw-r--r--cli/js/tty.ts14
-rw-r--r--cli/js/tty_test.ts22
-rw-r--r--cli/js/unit_tests.ts1
7 files changed, 53 insertions, 22 deletions
diff --git a/cli/js/deno.ts b/cli/js/deno.ts
index e2052f729..c563d5112 100644
--- a/cli/js/deno.ts
+++ b/cli/js/deno.ts
@@ -90,7 +90,6 @@ export {
dir,
env,
exit,
- isTTY,
execPath,
hostname,
loadavg,
@@ -124,6 +123,7 @@ export { statSync, lstatSync, stat, lstat } from "./stat.ts";
export { symlinkSync, symlink } from "./symlink.ts";
export { connectTLS, listenTLS } from "./tls.ts";
export { truncateSync, truncate } from "./truncate.ts";
+export { isatty, setRaw } from "./tty.ts";
export { utimeSync, utime } from "./utime.ts";
export { version } from "./version.ts";
export { writeFileSync, writeFile, WriteFileOptions } from "./write_file.ts";
diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts
index b22d89ebe..f94d28407 100644
--- a/cli/js/lib.deno.ns.d.ts
+++ b/cli/js/lib.deno.ns.d.ts
@@ -30,16 +30,6 @@ declare namespace Deno {
export function runTests(opts?: RunTestsOptions): Promise<void>;
- /** Check if running in terminal.
- *
- * console.log(Deno.isTTY().stdout);
- */
- export function isTTY(): {
- stdin: boolean;
- stdout: boolean;
- stderr: boolean;
- };
-
/** Get the loadavg. Requires the `--allow-env` flag.
*
* console.log(Deno.loadavg());
@@ -492,6 +482,7 @@ declare namespace Deno {
seekSync(offset: number, whence: SeekMode): void;
close(): void;
}
+
/** An instance of `File` for stdin. */
export const stdin: File;
/** An instance of `File` for stdout. */
@@ -555,6 +546,20 @@ declare namespace Deno {
/** Read-write. Behaves like `x` and allows to read from file. */
| "x+";
+ // @url js/tty.d.ts
+
+ /** UNSTABLE: newly added API
+ *
+ * Check if a given resource is TTY
+ */
+ export function isatty(rid: number): boolean;
+
+ /** UNSTABLE: newly added API
+ *
+ * Set TTY to be under raw mode or not.
+ */
+ export function setRaw(rid: number, mode: boolean): void;
+
// @url js/buffer.d.ts
/** A Buffer is a variable-sized buffer of bytes with read() and write()
diff --git a/cli/js/os.ts b/cli/js/os.ts
index 2a68ff8d3..309f5e1ff 100644
--- a/cli/js/os.ts
+++ b/cli/js/os.ts
@@ -3,13 +3,6 @@ import { sendSync } from "./dispatch_json.ts";
import { errors } from "./errors.ts";
import * as util from "./util.ts";
-/** Check if running in terminal.
- *
- * console.log(Deno.isTTY().stdout);
- */
-export function isTTY(): { stdin: boolean; stdout: boolean; stderr: boolean } {
- return sendSync("op_is_tty");
-}
/** Get the loadavg.
* Requires the `--allow-env` flag.
*
diff --git a/cli/js/os_test.ts b/cli/js/os_test.ts
index 6e771fe98..cdf72fdd7 100644
--- a/cli/js/os_test.ts
+++ b/cli/js/os_test.ts
@@ -115,10 +115,6 @@ test(function osPid(): void {
assert(Deno.pid > 0);
});
-test(function osIsTTYSmoke(): void {
- console.log(Deno.isTTY());
-});
-
testPerm({ env: true }, function getDir(): void {
type supportOS = "mac" | "win" | "linux";
diff --git a/cli/js/tty.ts b/cli/js/tty.ts
new file mode 100644
index 000000000..2ad44d025
--- /dev/null
+++ b/cli/js/tty.ts
@@ -0,0 +1,14 @@
+import { sendSync } from "./dispatch_json.ts";
+
+/** Check if a given resource is TTY. */
+export function isatty(rid: number): boolean {
+ return sendSync("op_isatty", { rid });
+}
+
+/** Set TTY to be under raw mode or not. */
+export function setRaw(rid: number, mode: boolean): void {
+ sendSync("op_set_raw", {
+ rid,
+ mode
+ });
+}
diff --git a/cli/js/tty_test.ts b/cli/js/tty_test.ts
new file mode 100644
index 000000000..f58784a7c
--- /dev/null
+++ b/cli/js/tty_test.ts
@@ -0,0 +1,22 @@
+// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+import { test, testPerm, assert } from "./test_util.ts";
+
+// Note tests for Deno.setRaw is in integration tests.
+
+testPerm({ read: true }, function isatty(): void {
+ // CI not under TTY, so cannot test stdin/stdout/stderr.
+ const f = Deno.openSync("cli/tests/hello.txt");
+ assert(!Deno.isatty(f.rid));
+});
+
+test(function isattyError(): void {
+ let caught = false;
+ try {
+ // Absurdly large rid.
+ Deno.isatty(0x7fffffff);
+ } catch (e) {
+ caught = true;
+ assert(e instanceof Deno.errors.BadResource);
+ }
+ assert(caught);
+});
diff --git a/cli/js/unit_tests.ts b/cli/js/unit_tests.ts
index 1c8237466..2495c938b 100644
--- a/cli/js/unit_tests.ts
+++ b/cli/js/unit_tests.ts
@@ -54,6 +54,7 @@ import "./text_encoding_test.ts";
import "./timers_test.ts";
import "./tls_test.ts";
import "./truncate_test.ts";
+import "./tty_test.ts";
import "./url_test.ts";
import "./url_search_params_test.ts";
import "./utime_test.ts";