Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

add colors to logs from ANSI control chars. #1103

Merged
merged 10 commits into from Aug 3, 2018
Merged

Conversation

snatchev
Copy link
Member

@snatchev snatchev commented Jul 25, 2018

  • I have run rspec and corrected all errors
  • I have run rubocop and corrected all errors
  • I have run npm run test and corrected all errors
  • I have tested this change locally and tried to launch the server as well as access a project, and with that at least one build

This PR adds colors to the logs by parsing out ANSI control chars:
image 1

@snatchev snatchev changed the title add colors to logs from ANSI control chars. [WIP] add colors to logs from ANSI control chars. Jul 25, 2018
Copy link
Member

@KrauseFx KrauseFx left a comment

Choose a reason for hiding this comment

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

So good 😍

lib/ansi.rb Outdated
@@ -0,0 +1,62 @@
module FastlaneCI
##
# little helper class for parsing ansi to html
Copy link
Member

Choose a reason for hiding this comment

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

Did you write this class, or is it taken from somewhere?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's original content :)

@snatchev snatchev changed the title [WIP] add colors to logs from ANSI control chars. add colors to logs from ANSI control chars. Jul 26, 2018
@nakhbari
Copy link
Contributor

Can we move this Ansi color library to the front-end? I think we should avoid html injection when possible.

@snatchev
Copy link
Member Author

@nakhbari I am in favor of moving it to the front-end. But I didn't know where to start, so I didn't do it there.

@nakhbari
Copy link
Contributor

@snatchev I'm more than happy to pair on this. I think it would take half a day.

@KrauseFx KrauseFx added the status: blocked Waiting for something else before we can proceed label Jul 30, 2018
@KrauseFx
Copy link
Member

Talking with @snatchev, not merging this until things are moved to frontend

@KrauseFx
Copy link
Member

Related GH issue #1054

@snatchev snatchev removed the status: blocked Waiting for something else before we can proceed label Jul 31, 2018
@snatchev snatchev requested a review from nakhbari July 31, 2018 15:35
@ViewChild('logLine', { read: ElementRef }) logLineEl: ElementRef;

ngAfterViewInit() {
const parentEl = this.logLineEl.nativeElement;
Copy link
Contributor

Choose a reason for hiding this comment

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

can we encapsulate the work being done in this method into a function? It's easier to following what's being done after this lifecycle hook is called.

let currentSpanEl = parentEl;
const stack = this.tokenize(this.log.message);

stack.forEach(tuple => {
Copy link
Contributor

Choose a reason for hiding this comment

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

please use a for-of loop instead

for (const ansiSection of ansiSectionsStack) {
...
}


stack.forEach(tuple => {
const [code, text] = tuple;
if (code === '0') {
Copy link
Contributor

Choose a reason for hiding this comment

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

could you explain what code 0 is here? or maybe make it a constant like const ANSI_RESET_CODE = '0';

* this method will split and group the result into an array of tuples [ansi code, text]
* NOTE: that the capture comes after the string, so we transpose them.
**/
private tokenize(text: string): [string, string][] {
Copy link
Contributor

Choose a reason for hiding this comment

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

totally optional, but you can create a type alias for ansi code to make the typings mean more than string.

ex.

type AnsiCode = string;

private tokenize(text: string): [AnsiCode, string][] { ... }

if (tuples[0] === '') {
tuples.shift();
}
// otherwise, we must assume we are starting each line as the default '0'
Copy link
Contributor

Choose a reason for hiding this comment

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

can you put the comment inside the block or alongside the previous comment?

}

private injectSpan(parent: HTMLElement, text: string, ansiCode: string): HTMLSpanElement {
const span = document.createElement('span');
Copy link
Contributor

Choose a reason for hiding this comment

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

can you provide the document in the constructor. We should be using injectables instead of the direct global var

ex. https://github.com/fastlane/ci/blob/master/web/app/onboard/onboard.component.ts#L29

@@ -0,0 +1 @@

Copy link
Contributor

Choose a reason for hiding this comment

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

can we add the background + color styles here?

i think it's this:


            padding: 8px;
            color: #93a1a1;
            background-color: #073642;
            font-family: 'Google Sans Monospace', monospace;

fixture.detectChanges();
});

it('should create', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

let's add a test to ensure that the loglines are rendered.

const stack = this.tokenize(this.log.message);

for (const tuple of stack) {
const [code, text] = tuple;
Copy link
Contributor

Choose a reason for hiding this comment

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

you can actually merge the previous two lines

for (const [ansiCode, text] of stack) { ... }

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 tried... Typescript complained at me.

Copy link
Contributor

@nakhbari nakhbari left a comment

Choose a reason for hiding this comment

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

Looks Dope 🚢

@snatchev snatchev merged commit e42a223 into master Aug 3, 2018
@snatchev snatchev deleted the add-ansi-colors-to-logs branch August 3, 2018 21:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants