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

console: improve console.group() #14999

Closed
wants to merge 2 commits into from

Conversation

Trott
Copy link
Member

@Trott Trott commented Aug 24, 2017

Preserve indentation for multiline strings, objects that span multiple
lines, etc.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

console

/cc @BridgeAR

@Trott Trott added the console Issues and PRs related to the console subsystem. label Aug 24, 2017
@Trott
Copy link
Member Author

Trott commented Aug 24, 2017

@@ -86,7 +86,12 @@ function createWriteErrorHandler(stream) {
};
}

function write(ignoreErrors, stream, string, errorhandler) {
function write(ignoreErrors, stream, string, errorhandler, groupIndent) {
if (groupIndent.length !== 0) {
Copy link
Member

Choose a reason for hiding this comment

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

You can also check for \n first by adding a indexOf guard. Using indexOf for cases were there is no newline it is a lot faster and almost negligible in case there is one.

if (groupIndent.length !== 0 && string.indexOf("\n") !== -1) {
  // ...

(Yes, indexOf is still faster then includes)

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd have to move the prepending of groupIndent if I did that so the result would probably be:

if (groupIndent.length !== 0) {
  if (string.indexOf('\n') !== -1 {
    string = string.replace(/\n/g, `\n${groupIndent}`);
  }
  string = groupIndent + string;
}
string += '\n';

...or...

if (groupIndent.length !== 0 && string.indexOf("\n") !== -1) {
  string = string.replace(/\n/g, `\n${groupIndent}`);
}
string = groupIndent + string + '\n';

Copy link
Member

Choose a reason for hiding this comment

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

I would do the latter while I would actually not replace the string again but simply place it like this

if (!ignoreErrors) return stream.write(`${groupIndent}${string}\n`);

Copy link
Member Author

Choose a reason for hiding this comment

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

(I went with the first option in my above comment.)

Copy link
Member

@BridgeAR BridgeAR left a comment

Choose a reason for hiding this comment

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

A nit but otherwise LGTM

@BridgeAR
Copy link
Member

@Trott just a thought - what do you think about hiding and protecting the Symbol a bit?

{
    writable: true,
    enumerable: false,
    configurable: false
}

Copy link
Member

@TimothyGu TimothyGu left a comment

Choose a reason for hiding this comment

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

The reason why I didn't suggest this on the original PR is that we can go down the rabbit hole real quick with this kind of changes. For one, I can see a legitimate argument to handle '\r\n' and even '\r'. If we do handle them, we might need to adjust column width etc. for modules that are e.g. curses work-alikes using \r. And the list goes on.

I'd prefer to keep group() minimal.

@BridgeAR
Copy link
Member

@TimothyGu I would suggest to keep the implementation similar to the ones used in browsers. I do not think we should support \r and this would also break the indentation of not user supplied line breaks but also from util.inspect that is called depending on the input to console.log.

@Trott
Copy link
Member Author

Trott commented Aug 24, 2017

@TimothyGu I agree that there's a rabbit-hole danger, but this seems pretty important IMO considering console.log(objectThatGetsDisplayedMultiline).

@Trott
Copy link
Member Author

Trott commented Aug 24, 2017

@Trott just a thought - what do you think about hiding and protecting the Symbol a bit?

@BridgeAR Done.

Trott added 2 commits August 23, 2017 22:22
Preserve indentation for multiline strings, objects that span multiple
lines, etc.
Hide the internal `groupIndent` key a bit by making it non-enumerable
and non-configurable.
@Trott Trott force-pushed the console-group-known-issues branch from 8858cc8 to 9df027b Compare August 24, 2017 05:22
Copy link
Member

@jasnell jasnell left a comment

Choose a reason for hiding this comment

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

Better. thank you

@Trott
Copy link
Member Author

Trott commented Aug 24, 2017

Copy link
Member

@hiroppy hiroppy left a comment

Choose a reason for hiding this comment

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

👍

jasnell pushed a commit that referenced this pull request Aug 25, 2017
Preserve indentation for multiline strings, objects that span multiple
lines, etc.

also make groupIndent non-enumerable

Hide the internal `groupIndent` key a bit by making it non-enumerable
and non-configurable.

PR-URL: #14999
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Daijiro Wachi <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
@jasnell
Copy link
Member

jasnell commented Aug 25, 2017

Landed in af11867

@jasnell jasnell closed this Aug 25, 2017
ghost pushed a commit to ayojs/ayo that referenced this pull request Aug 30, 2017
Preserve indentation for multiline strings, objects that span multiple
lines, etc.

also make groupIndent non-enumerable

Hide the internal `groupIndent` key a bit by making it non-enumerable
and non-configurable.

PR-URL: nodejs/node#14999
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Daijiro Wachi <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
ghost pushed a commit to ayojs/ayo that referenced this pull request Aug 30, 2017
Preserve indentation for multiline strings, objects that span multiple
lines, etc.

also make groupIndent non-enumerable

Hide the internal `groupIndent` key a bit by making it non-enumerable
and non-configurable.

PR-URL: nodejs/node#14999
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Daijiro Wachi <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
@MylesBorins
Copy link
Contributor

Setting as don't land as console.group doesn't exist on v8.x

@Trott please change label if neccessary

@Trott
Copy link
Member Author

Trott commented Sep 10, 2017

@Trott please change label if neccessary

Changed! :-D

MylesBorins pushed a commit that referenced this pull request Sep 10, 2017
Preserve indentation for multiline strings, objects that span multiple
lines, etc.

also make groupIndent non-enumerable

Hide the internal `groupIndent` key a bit by making it non-enumerable
and non-configurable.

PR-URL: #14999
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Daijiro Wachi <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
@MylesBorins MylesBorins mentioned this pull request Sep 10, 2017
MylesBorins pushed a commit that referenced this pull request Sep 11, 2017
Preserve indentation for multiline strings, objects that span multiple
lines, etc.

also make groupIndent non-enumerable

Hide the internal `groupIndent` key a bit by making it non-enumerable
and non-configurable.

PR-URL: #14999
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Daijiro Wachi <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
MylesBorins pushed a commit that referenced this pull request Sep 11, 2017
Preserve indentation for multiline strings, objects that span multiple
lines, etc.

also make groupIndent non-enumerable

Hide the internal `groupIndent` key a bit by making it non-enumerable
and non-configurable.

PR-URL: #14999
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Daijiro Wachi <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
MylesBorins pushed a commit that referenced this pull request Sep 12, 2017
Preserve indentation for multiline strings, objects that span multiple
lines, etc.

also make groupIndent non-enumerable

Hide the internal `groupIndent` key a bit by making it non-enumerable
and non-configurable.

PR-URL: #14999
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Daijiro Wachi <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
@MylesBorins
Copy link
Contributor

This does not land cleanly in LTS. Please feel free to manually backport by following the guide. Please also feel free to replace do-not-land if it is being backported

@Trott Trott deleted the console-group-known-issues branch January 13, 2022 22:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
console Issues and PRs related to the console subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants