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

Implement console.groupCollapsed #1452

Merged
merged 2 commits into from
Jan 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 76 additions & 7 deletions js/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type ConsoleOptions = Partial<{
showHidden: boolean;
depth: number;
colors: boolean;
indentLevel: number;
collapsedAt: number | null;
}>;

// Default depth of logging nested objects
Expand Down Expand Up @@ -322,13 +324,26 @@ function stringifyWithQuotes(
}
}

// Returns true when the console is collapsed.
function isCollapsed(
collapsedAt: number | null | undefined,
indentLevel: number | null | undefined
) {
if (collapsedAt == null || indentLevel == null) {
return false;
}

return collapsedAt <= indentLevel;
}

/** TODO Do not expose this from "deno" namespace. */
export function stringifyArgs(
// tslint:disable-next-line:no-any
args: any[],
options: ConsoleOptions = {}
): string {
const out: string[] = [];
const { collapsedAt, indentLevel } = options;
for (const a of args) {
if (typeof a === "string") {
out.push(a);
Expand All @@ -346,22 +361,45 @@ export function stringifyArgs(
);
}
}
return out.join(" ");
let outstr = out.join(" ");
if (
!isCollapsed(collapsedAt, indentLevel) &&
indentLevel != null &&
indentLevel > 0
) {
const groupIndent = " ".repeat(indentLevel);
if (outstr.indexOf("\n") !== -1) {
outstr = outstr.replace(/\n/g, `\n${groupIndent}`);
}
outstr = groupIndent + outstr;
}
return outstr;
}

type PrintFunc = (x: string, isErr?: boolean) => void;
type PrintFunc = (x: string, isErr?: boolean, printsNewline?: boolean) => void;

const countMap = new Map<string, number>();
const timerMap = new Map<string, number>();

/** TODO Do not expose this from "deno". */
export class Console {
constructor(private printFunc: PrintFunc) {}
indentLevel: number;
collapsedAt: number | null;
constructor(private printFunc: PrintFunc) {
this.indentLevel = 0;
this.collapsedAt = null;
}

/** Writes the arguments to stdout */
// tslint:disable-next-line:no-any
log = (...args: any[]): void => {
this.printFunc(stringifyArgs(args));
this.printFunc(
stringifyArgs(args, {
indentLevel: this.indentLevel,
collapsedAt: this.collapsedAt
}),
false,
!isCollapsed(this.collapsedAt, this.indentLevel)
);
};

/** Writes the arguments to stdout */
Expand All @@ -372,13 +410,20 @@ export class Console {
/** Writes the properties of the supplied `obj` to stdout */
// tslint:disable-next-line:no-any
dir = (obj: any, options: ConsoleOptions = {}) => {
this.printFunc(stringifyArgs([obj], options));
this.log(stringifyArgs([obj], options));
};

/** Writes the arguments to stdout */
// tslint:disable-next-line:no-any
warn = (...args: any[]): void => {
this.printFunc(stringifyArgs(args), true);
this.printFunc(
stringifyArgs(args, {
indentLevel: this.indentLevel,
collapsedAt: this.collapsedAt
}),
true,
!isCollapsed(this.collapsedAt, this.indentLevel)
);
};

/** Writes the arguments to stdout */
Expand Down Expand Up @@ -473,6 +518,30 @@ export class Console {

this.info(`${label}: ${duration}ms`);
};

group = (...label: Array<unknown>): void => {
if (label.length > 0) {
this.log(...label);
}
this.indentLevel += 2;
};

groupCollapsed = (...label: Array<unknown>): void => {
if (this.collapsedAt == null) {
this.collapsedAt = this.indentLevel;
}
this.group(...label);
};

groupEnd = (): void => {
if (this.indentLevel > 0) {
this.indentLevel -= 2;
}
if (this.collapsedAt != null && this.collapsedAt >= this.indentLevel) {
this.collapsedAt = null;
this.log(); // When the collapsed state ended, outputs a sinle new line.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use deno.stdout.write ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this class seems using PrintFunc for any output. Do you think we should use deno.stdout/deno.stderr for any output of console's methods?

Copy link
Member Author

@kt3k kt3k Jan 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ry I tried to replace printFunc calls with stdout/stderr calls, but I realized that stdout.write is async function and not usable as replacement of printFunc (libdeno.print). Probably we need to have sync version of write first. How do you think?

ref: kt3k@1e9bd29

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right - makes sense - I forgot that the writes to stdout/err are non-blocking.

}
};
}

/**
Expand Down
6 changes: 5 additions & 1 deletion js/console_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ test(function consoleTestStringifyCircular() {
assertEqual(
stringify(console),
// tslint:disable-next-line:max-line-length
"Console { printFunc: [Function], log: [Function], debug: [Function], info: [Function], dir: [Function], warn: [Function], error: [Function], assert: [Function], count: [Function], countReset: [Function], time: [Function], timeLog: [Function], timeEnd: [Function] }"
"Console { printFunc: [Function], log: [Function], debug: [Function], info: [Function], dir: [Function], warn: [Function], error: [Function], assert: [Function], count: [Function], countReset: [Function], time: [Function], timeLog: [Function], timeEnd: [Function], group: [Function], groupCollapsed: [Function], groupEnd: [Function], indentLevel: 0, collapsedAt: null }"
);
// test inspect is working the same
assertEqual(inspect(nestedObj), nestedObjExpected);
Expand Down Expand Up @@ -189,6 +189,8 @@ test(function consoleDetachedLog() {
const consoleTime = console.time;
const consoleTimeLog = console.timeLog;
const consoleTimeEnd = console.timeEnd;
const consoleGroup = console.group;
const consoleGroupEnd = console.groupEnd;
log("Hello world");
dir("Hello world");
debug("Hello world");
Expand All @@ -201,4 +203,6 @@ test(function consoleDetachedLog() {
consoleTime("Hello world");
consoleTimeLog("Hello world");
consoleTimeEnd("Hello world");
consoleGroup("Hello world");
consoleGroupEnd();
});
8 changes: 6 additions & 2 deletions libdeno/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,21 @@ void PromiseRejectCallback(v8::PromiseRejectMessage promise_reject_message) {

void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
CHECK_GE(args.Length(), 1);
CHECK_LE(args.Length(), 2);
CHECK_LE(args.Length(), 3);
auto* isolate = args.GetIsolate();
DenoIsolate* d = FromIsolate(isolate);
auto context = d->context_.Get(d->isolate_);
v8::HandleScope handle_scope(isolate);
v8::String::Utf8Value str(isolate, args[0]);
bool is_err =
args.Length() >= 2 ? args[1]->BooleanValue(context).ToChecked() : false;
bool prints_newline =
args.Length() >= 3 ? args[2]->BooleanValue(context).ToChecked() : true;
FILE* file = is_err ? stderr : stdout;
fwrite(*str, sizeof(**str), str.length(), file);
fprintf(file, "\n");
if (prints_newline) {
fprintf(file, "\n");
}
fflush(file);
}

Expand Down
2 changes: 2 additions & 0 deletions tests/console_group.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
args: tests/console_group.ts --reload
output: tests/console_group.ts.out
15 changes: 15 additions & 0 deletions tests/console_group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
console.group("1");
console.log("2");
console.group("3");
console.log("4");
console.groupEnd();
console.groupEnd();

console.groupCollapsed("5");
console.log("6");
console.group("7");
console.log("8");
console.groupEnd();
console.groupEnd();
console.log("9");
console.log("10");
7 changes: 7 additions & 0 deletions tests/console_group.ts.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1
2
3
4
5678
9
10
3 changes: 3 additions & 0 deletions tests/console_group_warn.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
args: tests/console_group_warn.ts --reload
check_stderr: true
output: tests/console_group_warn.ts.out
20 changes: 20 additions & 0 deletions tests/console_group_warn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
console.warn("1");
console.group();
console.warn("2");
console.group();
console.warn("3");
console.groupEnd();
console.warn("4");
console.groupEnd();
console.warn("5");

console.groupCollapsed();
console.warn("6");
console.group();
console.warn("7");
console.groupEnd();
console.warn("8");
console.groupEnd();

console.warn("9");
console.warn("10");
9 changes: 9 additions & 0 deletions tests/console_group_warn.ts.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Compiling [WILDCARD].ts
1
2
3
4
5
678
9
10