-
Notifications
You must be signed in to change notification settings - Fork 30k
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
Conversation
@@ -86,7 +86,12 @@ function createWriteErrorHandler(stream) { | |||
}; | |||
} | |||
|
|||
function write(ignoreErrors, stream, string, errorhandler) { | |||
function write(ignoreErrors, stream, string, errorhandler, groupIndent) { | |||
if (groupIndent.length !== 0) { |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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';
There was a problem hiding this comment.
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`);
There was a problem hiding this comment.
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.)
There was a problem hiding this 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
@Trott just a thought - what do you think about hiding and protecting the Symbol a bit?
|
There was a problem hiding this 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.
@TimothyGu I would suggest to keep the implementation similar to the ones used in browsers. I do not think we should support |
@TimothyGu I agree that there's a rabbit-hole danger, but this seems pretty important IMO considering |
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.
8858cc8
to
9df027b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better. thank you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
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 <ruben@bridgewater.de> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Landed in af11867 |
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 <ruben@bridgewater.de> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
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 <ruben@bridgewater.de> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Setting as don't land as @Trott please change label if neccessary |
Changed! :-D |
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 <ruben@bridgewater.de> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
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 <ruben@bridgewater.de> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
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 <ruben@bridgewater.de> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
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 <ruben@bridgewater.de> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
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 |
Preserve indentation for multiline strings, objects that span multiple
lines, etc.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
console
/cc @BridgeAR