mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 23:34:47 -05:00
refactor: decouple Console implementation from stdout (#4899)
When creating a console instance, one must pass "printFunc" arg which is used internally by Console to output messages. Due to numerous refactors there was a single method ("console.clear()") that used "Deno.stdout" instead of "printFunc". This commit unifies how "Console" outpus message, by using "printFunc" in all methods; consequently "Deno.stdout" is no longer imported in "cli/js/console.ts" making it a standalone module that doesn't depend on any CLI-specific APIs.
This commit is contained in:
parent
8e4333fd99
commit
8e9ab9e33e
2 changed files with 6 additions and 32 deletions
|
@ -5,8 +5,6 @@ import { assert, assertEquals, unitTest } from "./test_util.ts";
|
||||||
// in order to "trick" TypeScript.
|
// in order to "trick" TypeScript.
|
||||||
const {
|
const {
|
||||||
inspect,
|
inspect,
|
||||||
writeSync,
|
|
||||||
stdout,
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
} = Deno as any;
|
} = Deno as any;
|
||||||
|
|
||||||
|
@ -744,21 +742,10 @@ unitTest(function consoleTestError(): void {
|
||||||
});
|
});
|
||||||
|
|
||||||
unitTest(function consoleTestClear(): void {
|
unitTest(function consoleTestClear(): void {
|
||||||
const stdoutWriteSync = stdout.writeSync;
|
mockConsole((console, out) => {
|
||||||
const uint8 = new TextEncoder().encode("\x1b[1;1H" + "\x1b[0J");
|
console.clear();
|
||||||
let buffer = new Uint8Array(0);
|
assertEquals(out.toString(), "\x1b[1;1H" + "\x1b[0J");
|
||||||
|
});
|
||||||
stdout.writeSync = (u8: Uint8Array): Promise<number> => {
|
|
||||||
const tmp = new Uint8Array(buffer.length + u8.length);
|
|
||||||
tmp.set(buffer, 0);
|
|
||||||
tmp.set(u8, buffer.length);
|
|
||||||
buffer = tmp;
|
|
||||||
|
|
||||||
return writeSync(stdout.rid, u8);
|
|
||||||
};
|
|
||||||
console.clear();
|
|
||||||
stdout.writeSync = stdoutWriteSync;
|
|
||||||
assertEquals(buffer, uint8);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Test bound this issue
|
// Test bound this issue
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
import { isTypedArray, TypedArray } from "./util.ts";
|
import { isTypedArray, TypedArray } from "./util.ts";
|
||||||
import { TextEncoder } from "./text_encoding.ts";
|
|
||||||
import { SyncWriter } from "../io.ts";
|
|
||||||
import { stdout } from "../files.ts";
|
|
||||||
import { cliTable } from "./console_table.ts";
|
import { cliTable } from "./console_table.ts";
|
||||||
import { exposeForTest } from "../internals.ts";
|
import { exposeForTest } from "../internals.ts";
|
||||||
import { PromiseState } from "./promise.ts";
|
import { PromiseState } from "./promise.ts";
|
||||||
|
@ -39,16 +36,6 @@ export class CSI {
|
||||||
|
|
||||||
/* eslint-disable @typescript-eslint/no-use-before-define */
|
/* eslint-disable @typescript-eslint/no-use-before-define */
|
||||||
|
|
||||||
function cursorTo(stream: SyncWriter, _x: number, _y?: number): void {
|
|
||||||
const uint8 = new TextEncoder().encode(CSI.kClear);
|
|
||||||
stream.writeSync(uint8);
|
|
||||||
}
|
|
||||||
|
|
||||||
function clearScreenDown(stream: SyncWriter): void {
|
|
||||||
const uint8 = new TextEncoder().encode(CSI.kClearScreenDown);
|
|
||||||
stream.writeSync(uint8);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getClassInstanceName(instance: unknown): string {
|
function getClassInstanceName(instance: unknown): string {
|
||||||
if (typeof instance !== "object") {
|
if (typeof instance !== "object") {
|
||||||
return "";
|
return "";
|
||||||
|
@ -934,8 +921,8 @@ export class Console {
|
||||||
|
|
||||||
clear = (): void => {
|
clear = (): void => {
|
||||||
this.indentLevel = 0;
|
this.indentLevel = 0;
|
||||||
cursorTo(stdout, 0, 0);
|
this.#printFunc(CSI.kClear, false);
|
||||||
clearScreenDown(stdout);
|
this.#printFunc(CSI.kClearScreenDown, false);
|
||||||
};
|
};
|
||||||
|
|
||||||
trace = (...args: unknown[]): void => {
|
trace = (...args: unknown[]): void => {
|
||||||
|
|
Loading…
Reference in a new issue