Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

fix issue where color formatting text is leaking #1216

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 9 additions & 2 deletions lib/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,16 +305,23 @@ TaskReporter_.prototype.log_ = function(data) {
tag += (' #' + this.task.taskId);
tag += '] ';


data = data.toString();
for ( var i = 0; i < data.length; i++ ) {
if (this.insertTag) {
this.insertTag = false;
// This ensures that the '\x1B[0m' appears before the tag, so that
// data remains correct when color is not processed.
// See https://github.com/angular/protractor/pull/1216
if (data[i] === '\x1B' && data.substring(i, i+4) === '\x1B[0m' ) {
Copy link
Member

Choose a reason for hiding this comment

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

can we add a comment here on what this craziness is doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Basically, jasmines sends back colored strings as:

\x1B[31mSome text\n\x1B[0mnew line

which gets formatted into

[color]Some text[/color]
[no color]new line[/no color]

However, if you simply add a new tag after the \n, the \x1B[0m appears before the text (and after the tag) in the new line. This is not an issue if you are using console or something that process the \x1B[0m properly. But if the \x1B[0m is not processed, then the \x1B[0m becomes concatenated with the info.

This craziness basically append the tag only after the \x1B[0m.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

another note, technically data.substring(i, i+4) === '\x1B[0m' is all that's needed to do the job, but I did data[i] === '\x1B' && data.substring(i, i+4) === '\x1B[0m' as a slight optimization to avoid running a substring on every character.

Copy link
Member

Choose a reason for hiding this comment

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

Oh I mean add a comment to the actual code so that we'll know what's going on in 2 months.

this.buffer += ('\x1B[0m' + tag);
i += 3;
continue;
}

this.buffer += tag;
}
if (data[i] === '\n') {
this.insertTag = true;
this.buffer += '\x1B[0m'; // Prevent color from leaking into next line
}
this.buffer += data[i];
}
Expand Down