Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Stabilize Deno.consoleSize() API #15933

Merged
merged 14 commits into from
Oct 25, 2022
2 changes: 1 addition & 1 deletion cli/bench/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ function bench(fun) {
}

bench(() => {
Deno.consoleSize(0);
Deno.consoleSize();
});
1 change: 0 additions & 1 deletion cli/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const UNSTABLE_DENO_PROPS: &[&str] = &[
"addSignalListener",
"bench",
"connect",
"consoleSize",
"createHttpClient",
"kill",
"listen",
Expand Down
13 changes: 13 additions & 0 deletions cli/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1901,6 +1901,19 @@ declare namespace Deno {
*/
export const File: typeof FsFile;

/** Gets the size of the console as columns/rows.
*
* ```ts
* const { columns, rows } = Deno.consoleSize();
* ```
*
* @category I/O
*/
export function consoleSize(): {
columns: number;
rows: number;
};

/** @category I/O */
export interface SetRawOptions {
/**
Expand Down
17 changes: 0 additions & 17 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,23 +226,6 @@ declare namespace Deno {
*/
export function umask(mask?: number): number;

/** **UNSTABLE**: New API, yet to be vetted.
*
* Gets the size of the console as columns/rows.
*
* ```ts
* const { columns, rows } = Deno.consoleSize(Deno.stdout.rid);
* ```
*
* @category I/O
*/
export function consoleSize(
rid: number,
): {
columns: number;
rows: number;
};

/** **UNSTABLE**: New API, yet to be vetted.
*
* Returns the release version of the Operating System.
Expand Down
22 changes: 8 additions & 14 deletions cli/tests/unit/tty_test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert, assertThrows } from "./test_util.ts";
import { assert } from "./test_util.ts";

// 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");
assertThrows(() => {
Deno.consoleSize(file.rid);
}, Error);
file.close();
});

Deno.test(function consoleSizeError() {
assertThrows(() => {
// Absurdly large rid.
Deno.consoleSize(0x7fffffff);
}, Deno.errors.BadResource);
Deno.test(function consoleSize() {
if (!Deno.isatty(Deno.stdout.rid)) {
return;
}
const result = Deno.consoleSize();
assert(typeof result.columns !== "undefined");
assert(typeof result.rows !== "undefined");
});

Deno.test({ permissions: { read: true } }, function isatty() {
Expand Down
4 changes: 2 additions & 2 deletions runtime/js/40_tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
const ops = core.ops;

const size = new Uint32Array(2);
function consoleSize(rid) {
ops.op_console_size(rid, size);
function consoleSize() {
ops.op_console_size(size);
return { columns: size[0], rows: size[1] };
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/js/90_deno_ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@
refTimer: __bootstrap.timers.refTimer,
unrefTimer: __bootstrap.timers.unrefTimer,
hostname: __bootstrap.os.hostname,
consoleSize: __bootstrap.tty.consoleSize,
};

__bootstrap.denoNsUnstable = {
consoleSize: __bootstrap.tty.consoleSize,
DiagnosticCategory: __bootstrap.diagnostics.DiagnosticCategory,
osRelease: __bootstrap.os.osRelease,
systemMemoryInfo: __bootstrap.os.systemMemoryInfo,
Expand Down
84 changes: 51 additions & 33 deletions runtime/ops/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,48 +192,66 @@ fn op_isatty(
#[op(fast)]
fn op_console_size(
state: &mut OpState,
rid: u32,
result: &mut [u32],
) -> Result<(), AnyError> {
super::check_unstable(state, "Deno.consoleSize");
StdFileResource::with_file(state, rid, move |std_file| {
#[cfg(windows)]
{
use std::os::windows::io::AsRawHandle;
let handle = std_file.as_raw_handle();
fn check_console_size(
state: &mut OpState,
result: &mut [u32],
rid: u32,
) -> Result<(), AnyError> {
StdFileResource::with_file(state, rid, move |std_file| {
#[cfg(windows)]
{
use std::os::windows::io::AsRawHandle;
let handle = std_file.as_raw_handle();

// SAFETY: winapi calls
unsafe {
let mut bufinfo: winapi::um::wincon::CONSOLE_SCREEN_BUFFER_INFO =
std::mem::zeroed();
// SAFETY: winapi calls
unsafe {
let mut bufinfo: winapi::um::wincon::CONSOLE_SCREEN_BUFFER_INFO =
std::mem::zeroed();

if winapi::um::wincon::GetConsoleScreenBufferInfo(handle, &mut bufinfo)
== 0
{
return Err(Error::last_os_error().into());
if winapi::um::wincon::GetConsoleScreenBufferInfo(
handle,
&mut bufinfo,
) == 0
{
return Err(Error::last_os_error().into());
}
result[0] = bufinfo.dwSize.X as u32;
result[1] = bufinfo.dwSize.Y as u32;
Ok(())
}
result[0] = bufinfo.dwSize.X as u32;
result[1] = bufinfo.dwSize.Y as u32;
Ok(())
}
}

#[cfg(unix)]
{
use std::os::unix::io::AsRawFd;
#[cfg(unix)]
{
use std::os::unix::io::AsRawFd;

let fd = std_file.as_raw_fd();
// TODO(bartlomieju):
#[allow(clippy::undocumented_unsafe_blocks)]
unsafe {
let mut size: libc::winsize = std::mem::zeroed();
if libc::ioctl(fd, libc::TIOCGWINSZ, &mut size as *mut _) != 0 {
return Err(Error::last_os_error().into());
let fd = std_file.as_raw_fd();
// TODO(bartlomieju):
#[allow(clippy::undocumented_unsafe_blocks)]
unsafe {
let mut size: libc::winsize = std::mem::zeroed();
if libc::ioctl(fd, libc::TIOCGWINSZ, &mut size as *mut _) != 0 {
return Err(Error::last_os_error().into());
}
result[0] = size.ws_col as u32;
result[1] = size.ws_row as u32;
Ok(())
}
result[0] = size.ws_col as u32;
result[1] = size.ws_row as u32;
Ok(())
}
})
}

let mut last_result = Ok(());
// Since stdio might be piped we try to get the size of the console for all
// of them and return the first one that succeeds.
for rid in [0, 1, 2] {
last_result = check_console_size(state, result, rid);
if last_result.is_ok() {
return last_result;
}
})
}

last_result
}
2 changes: 1 addition & 1 deletion test_util/std
Submodule std updated 89 files
+0 −1 .github/CODEOWNERS
+3 −3 .github/workflows/ci.yml
+1 −1 .gitignore
+3 −0 _tools/check_deprecation.ts
+2 −0 _tools/check_licence.ts
+20 −0 crypto/README.md
+0 −8 crypto/_benches/bench.ts
+38 −0 crypto/_util.ts
+5 −0 crypto/mod.ts
+27 −0 crypto/test.ts
+32 −0 crypto/util.ts
+36 −130 crypto/util_test.ts
+6 −8 deno.json
+35 −15 fs/expand_glob.ts
+13 −6 fs/expand_glob_test.ts
+0 −92 hash/README.md
+0 −80 hash/_fnv/fnv32.ts
+0 −90 hash/_fnv/fnv64.ts
+0 −53 hash/_fnv/util.ts
+0 −69 hash/_fnv/util_test.ts
+0 −53 hash/_sha3/keccak.ts
+0 −791 hash/_sha3/keccakf.ts
+0 −57 hash/_sha3/sha3.ts
+0 −52 hash/_sha3/shake.ts
+0 −112 hash/_sha3/sponge.ts
+0 −382 hash/_wasm/Cargo.lock
+0 −28 hash/_wasm/Cargo.toml
+0 −21 hash/_wasm/README.md
+0 −86 hash/_wasm/hash.ts
+0 −2,809 hash/_wasm/lib/deno_hash.generated.mjs
+0 −1 hash/_wasm/rust-toolchain
+0 −53 hash/_wasm/src/lib.rs
+0 −5 hash/fnv.ts
+0 −117 hash/fnv_test.ts
+0 −11 hash/hasher.ts
+0 −251 hash/md5.ts
+0 −54 hash/md5_test.ts
+0 −51 hash/mod.ts
+0 −506 hash/sha1.ts
+0 −811 hash/sha1_test.ts
+0 −647 hash/sha256.ts
+0 −1,614 hash/sha256_test.ts
+0 −6 hash/sha3.ts
+0 −585 hash/sha3_test.ts
+0 −1,235 hash/sha512.ts
+0 −2,409 hash/sha512_test.ts
+0 −1 hash/testdata/hashtest
+15 −56 http/file_server.ts
+4 −15 http/file_server_test.ts
+6 −0 node/README.md
+7 −0 node/_core.ts
+13 −27 node/_fs/_fs_opendir.ts
+6 −0 node/_process/streams.mjs
+8 −8 node/_stream.mjs
+2,812 −0 node/_tools/TODO.md
+22 −1 node/_tools/config.json
+95 −0 node/_tools/list_remaining_tests.ts
+46 −0 node/_tools/test/parallel/test-btoa-atob.js
+15 −16 node/_tools/test/parallel/test-fs-opendir.js
+1 −5 node/_tools/test/parallel/test-stream-writable-change-default-encoding.js
+68 −0 node/_tools/test/parallel/test-whatwg-encoding-custom-api-basics.js
+68 −0 node/_tools/test/parallel/test-whatwg-encoding-custom-fatal-streaming.js
+91 −0 node/_tools/test/parallel/test-whatwg-encoding-custom-textdecoder-fatal.js
+37 −0 node/_tools/test/parallel/test-whatwg-encoding-custom-textdecoder-ignorebom.js
+45 −0 node/_tools/test/parallel/test-whatwg-encoding-custom-textdecoder-streaming.js
+63 −0 node/_tools/test/parallel/test-whatwg-encoding-custom-textdecoder-utf16-surrogates.js
+72 −0 node/_tools/test/parallel/test-whatwg-events-add-event-listener-options-passive.js
+166 −0 node/_tools/test/parallel/test-whatwg-events-add-event-listener-options-signal.js
+40 −0 node/_tools/test/parallel/test-whatwg-events-customevent.js
+25 −0 node/_tools/test/parallel/test-whatwg-url-custom-deepequal.js
+64 −0 node/_tools/test/parallel/test-whatwg-url-custom-domainto.js
+33 −0 node/_tools/test/parallel/test-whatwg-url-custom-global.js
+22 −0 node/_tools/test/parallel/test-whatwg-url-custom-href-side-effect.js
+75 −0 node/_tools/test/parallel/test-whatwg-url-custom-inspect.js
+87 −0 node/_tools/test/parallel/test-whatwg-url-custom-parsing.js
+67 −0 node/_tools/test/parallel/test-whatwg-url-custom-setters.js
+39 −0 node/_tools/test/parallel/test-whatwg-url-custom-tostringtag.js
+27 −0 node/_tools/test/parallel/test-whatwg-url-override-hostname.js
+93 −0 node/_tools/test/parallel/test-whatwg-url-toascii.js
+3 −3 node/_tools/vendor_readable_stream.ts
+2 −0 node/assertion_error.ts
+3 −1 node/module.ts
+16 −0 node/module_test.ts
+5 −1 node/net.ts
+28 −0 node/net_test.ts
+4 −0 node/process_test.ts
+ node/testdata/libhello_darwin.node
+0 −5 streams/conversion.ts
+0 −26 uuid/_common.ts