mirror of
https://github.com/denoland/deno.git
synced 2024-12-25 08:39:09 -05:00
feat: Stabilize Deno.consoleSize() API (#15933)
This commit stabilizes "Deno.consoleSize()" API. There is one change compared to previous unstable API, in that the API doesn't accept any arguments. Console size is established by querying syscalls for stdio streams at fd 0, 1 and 2.
This commit is contained in:
parent
af62e0833d
commit
ab0c33ebf8
9 changed files with 77 additions and 70 deletions
|
@ -17,5 +17,5 @@ function bench(fun) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bench(() => {
|
bench(() => {
|
||||||
Deno.consoleSize(0);
|
Deno.consoleSize();
|
||||||
});
|
});
|
||||||
|
|
|
@ -35,7 +35,6 @@ const UNSTABLE_DENO_PROPS: &[&str] = &[
|
||||||
"addSignalListener",
|
"addSignalListener",
|
||||||
"bench",
|
"bench",
|
||||||
"connect",
|
"connect",
|
||||||
"consoleSize",
|
|
||||||
"createHttpClient",
|
"createHttpClient",
|
||||||
"kill",
|
"kill",
|
||||||
"listen",
|
"listen",
|
||||||
|
|
13
cli/dts/lib.deno.ns.d.ts
vendored
13
cli/dts/lib.deno.ns.d.ts
vendored
|
@ -1901,6 +1901,19 @@ declare namespace Deno {
|
||||||
*/
|
*/
|
||||||
export const File: typeof FsFile;
|
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 */
|
/** @category I/O */
|
||||||
export interface SetRawOptions {
|
export interface SetRawOptions {
|
||||||
/**
|
/**
|
||||||
|
|
17
cli/dts/lib.deno.unstable.d.ts
vendored
17
cli/dts/lib.deno.unstable.d.ts
vendored
|
@ -226,23 +226,6 @@ declare namespace Deno {
|
||||||
*/
|
*/
|
||||||
export function umask(mask?: number): number;
|
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.
|
/** **UNSTABLE**: New API, yet to be vetted.
|
||||||
*
|
*
|
||||||
* Returns the release version of the Operating System.
|
* Returns the release version of the Operating System.
|
||||||
|
|
|
@ -1,21 +1,15 @@
|
||||||
// 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 } from "./test_util.ts";
|
||||||
|
|
||||||
// Note tests for Deno.stdin.setRaw is in integration tests.
|
// Note tests for Deno.stdin.setRaw is in integration tests.
|
||||||
|
|
||||||
Deno.test({ permissions: { read: true } }, function consoleSizeFile() {
|
Deno.test(function consoleSize() {
|
||||||
const file = Deno.openSync("cli/tests/testdata/assets/hello.txt");
|
if (!Deno.isatty(Deno.stdout.rid)) {
|
||||||
assertThrows(() => {
|
return;
|
||||||
Deno.consoleSize(file.rid);
|
}
|
||||||
}, Error);
|
const result = Deno.consoleSize();
|
||||||
file.close();
|
assert(typeof result.columns !== "undefined");
|
||||||
});
|
assert(typeof result.rows !== "undefined");
|
||||||
|
|
||||||
Deno.test(function consoleSizeError() {
|
|
||||||
assertThrows(() => {
|
|
||||||
// Absurdly large rid.
|
|
||||||
Deno.consoleSize(0x7fffffff);
|
|
||||||
}, Deno.errors.BadResource);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test({ permissions: { read: true } }, function isatty() {
|
Deno.test({ permissions: { read: true } }, function isatty() {
|
||||||
|
|
|
@ -10,8 +10,8 @@
|
||||||
const ops = core.ops;
|
const ops = core.ops;
|
||||||
|
|
||||||
const size = new Uint32Array(2);
|
const size = new Uint32Array(2);
|
||||||
function consoleSize(rid) {
|
function consoleSize() {
|
||||||
ops.op_console_size(rid, size);
|
ops.op_console_size(size);
|
||||||
return { columns: size[0], rows: size[1] };
|
return { columns: size[0], rows: size[1] };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -120,10 +120,10 @@
|
||||||
refTimer: __bootstrap.timers.refTimer,
|
refTimer: __bootstrap.timers.refTimer,
|
||||||
unrefTimer: __bootstrap.timers.unrefTimer,
|
unrefTimer: __bootstrap.timers.unrefTimer,
|
||||||
hostname: __bootstrap.os.hostname,
|
hostname: __bootstrap.os.hostname,
|
||||||
|
consoleSize: __bootstrap.tty.consoleSize,
|
||||||
};
|
};
|
||||||
|
|
||||||
__bootstrap.denoNsUnstable = {
|
__bootstrap.denoNsUnstable = {
|
||||||
consoleSize: __bootstrap.tty.consoleSize,
|
|
||||||
DiagnosticCategory: __bootstrap.diagnostics.DiagnosticCategory,
|
DiagnosticCategory: __bootstrap.diagnostics.DiagnosticCategory,
|
||||||
osRelease: __bootstrap.os.osRelease,
|
osRelease: __bootstrap.os.osRelease,
|
||||||
systemMemoryInfo: __bootstrap.os.systemMemoryInfo,
|
systemMemoryInfo: __bootstrap.os.systemMemoryInfo,
|
||||||
|
|
|
@ -192,48 +192,66 @@ fn op_isatty(
|
||||||
#[op(fast)]
|
#[op(fast)]
|
||||||
fn op_console_size(
|
fn op_console_size(
|
||||||
state: &mut OpState,
|
state: &mut OpState,
|
||||||
rid: u32,
|
|
||||||
result: &mut [u32],
|
result: &mut [u32],
|
||||||
) -> Result<(), AnyError> {
|
) -> Result<(), AnyError> {
|
||||||
super::check_unstable(state, "Deno.consoleSize");
|
fn check_console_size(
|
||||||
StdFileResource::with_file(state, rid, move |std_file| {
|
state: &mut OpState,
|
||||||
#[cfg(windows)]
|
result: &mut [u32],
|
||||||
{
|
rid: u32,
|
||||||
use std::os::windows::io::AsRawHandle;
|
) -> Result<(), AnyError> {
|
||||||
let handle = std_file.as_raw_handle();
|
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
|
// SAFETY: winapi calls
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut bufinfo: winapi::um::wincon::CONSOLE_SCREEN_BUFFER_INFO =
|
let mut bufinfo: winapi::um::wincon::CONSOLE_SCREEN_BUFFER_INFO =
|
||||||
std::mem::zeroed();
|
std::mem::zeroed();
|
||||||
|
|
||||||
if winapi::um::wincon::GetConsoleScreenBufferInfo(handle, &mut bufinfo)
|
if winapi::um::wincon::GetConsoleScreenBufferInfo(
|
||||||
== 0
|
handle,
|
||||||
{
|
&mut bufinfo,
|
||||||
return Err(Error::last_os_error().into());
|
) == 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)]
|
#[cfg(unix)]
|
||||||
{
|
{
|
||||||
use std::os::unix::io::AsRawFd;
|
use std::os::unix::io::AsRawFd;
|
||||||
|
|
||||||
let fd = std_file.as_raw_fd();
|
let fd = std_file.as_raw_fd();
|
||||||
// TODO(bartlomieju):
|
// TODO(bartlomieju):
|
||||||
#[allow(clippy::undocumented_unsafe_blocks)]
|
#[allow(clippy::undocumented_unsafe_blocks)]
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut size: libc::winsize = std::mem::zeroed();
|
let mut size: libc::winsize = std::mem::zeroed();
|
||||||
if libc::ioctl(fd, libc::TIOCGWINSZ, &mut size as *mut _) != 0 {
|
if libc::ioctl(fd, libc::TIOCGWINSZ, &mut size as *mut _) != 0 {
|
||||||
return Err(Error::last_os_error().into());
|
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 0ce558fec1a1beeda3ed7710c6507b9e991829c9
|
Subproject commit f2c97ea9c382fb3e225929c7650934417e2fc8c9
|
Loading…
Reference in a new issue