Skip to content

Commit

Permalink
feat(): collapse/expand commands log in terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
Izak88 authored and jkuri committed Aug 8, 2017
1 parent 9fd48a3 commit 1544fb1
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 15 deletions.
44 changes: 44 additions & 0 deletions src/app/assets/public/images/icons/collapse.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions src/app/assets/public/images/icons/expand.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 12 additions & 1 deletion src/app/components/app-terminal/app-terminal.component.html
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
<pre class="window-terminal-container batman-ansi-theme" [ngClass]="{ large: options.size === 'large' }" slimScroll [options]="scrollOptions" [scrollEvents]="scrollEvents"></pre>
<pre class="window-terminal-container dracula-ansi-theme" [ngClass]="{ large: options.size === 'large' }" slimScroll [options]="scrollOptions" [scrollEvents]="scrollEvents">
<div class="command columns log" *ngFor="let cmd of commands; let i = index;" [id]="i">
<div class="terminal">
<span class="icon">
<img *ngIf="cmd.visible" src="images/icons/collapse.svg" (click)="toogleCommand(i)">
<img *ngIf="!cmd.visible" src="images/icons/expand.svg" (click)="toogleCommand(i)">
</span>
<div class="output" [class.is-hidden]="!cmd.visible" [innerHTML]="cmd.output"></div>
<div class="output" [class.is-hidden]="cmd.visible" [innerHTML]="cmd.command"></div>
</div>
</div>
</pre>
68 changes: 54 additions & 14 deletions src/app/components/app-terminal/app-terminal.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Component, ElementRef, OnInit, Input, SimpleChange, EventEmitter } from '@angular/core';
import {
Component, ElementRef, OnInit, Input, SimpleChange, EventEmitter, NgZone } from '@angular/core';
import { ISlimScrollOptions, SlimScrollEvent } from 'ngx-slimscroll';

import * as AnsiUp from 'ansi_up';

@Component({
Expand All @@ -12,8 +14,9 @@ export class AppTerminalComponent implements OnInit {
au: any;
scrollOptions: ISlimScrollOptions;
scrollEvents: EventEmitter<SlimScrollEvent>;
commands: { command: string, visible: boolean, output: string }[];

constructor(private elementRef: ElementRef) {
constructor(private elementRef: ElementRef, private ngZone: NgZone) {
this.scrollOptions = {
position: 'right',
barBackground: '#11121A',
Expand All @@ -35,26 +38,63 @@ export class AppTerminalComponent implements OnInit {
ngOnInit() {
this.au = new AnsiUp.default();
this.au.use_classes = true;
this.commands = [];
}

ngOnChanges(changes: SimpleChange) {
if (!this.data) {
return;
}

const el = this.elementRef.nativeElement.querySelector('.window-terminal-container');
if (typeof this.data.clear !== 'undefined') {
el.innerHTML = '';
} else {
el.innerHTML += this.au.ansi_to_html(this.data);
this.ngZone.run(() => {
const el = this.elementRef.nativeElement.querySelector('.window-terminal-container');
if (typeof this.data.clear !== 'undefined') {
el.innerHTML = '';
} else {
let output: string = this.au.ansi_to_html(this.data);
if (output) {
if (this.commands.length > 0) {
if (output.indexOf('==&gt;') !== -1) {
let command = output.split('</span>')[0] + '</span>';
this.commands.push({
command: command,
visible: true,
output: output
});
} else {
this.commands[this.commands.length - 1].command += ` ${output}`;
}
} else {
let regexp = /<span(.*)==&gt;/gi;
regexp.lastIndex = 1;
let match = regexp.exec(output);
if (match) {
let indexEnd = match.index;
let indexStart = 0;
while (indexEnd >= 0) {
let log = output.substring(indexStart, indexEnd);
let command = log.split('</span>')[0] + '</span>';
this.commands.push({
command: command,
visible: true,
output: log
});
indexStart = indexEnd;
indexEnd = regexp.exec(output).index;
}
}
}
}

const recalculateEvent = new SlimScrollEvent({ type: 'recalculate' });
const bottomEvent = new SlimScrollEvent({ type: 'scrollToBottom', duration: 300 });
const recalculateEvent = new SlimScrollEvent({ type: 'recalculate' });
const bottomEvent = new SlimScrollEvent({ type: 'scrollToBottom', duration: 300 });

setTimeout(() => {
this.scrollEvents.emit(recalculateEvent);
this.scrollEvents.emit(bottomEvent);
});
}
setTimeout(() => el.scrollTop = el.scrollHeight);
}
});
}

toogleCommand(index: number) {
this.commands[index].visible = !this.commands[index].visible;
}
}
11 changes: 11 additions & 0 deletions src/app/styles/terminal.sass
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,14 @@

&.large
height: 700px

.output
padding: 0
margin: 0

.terminal
display: flex
align-items: left
height: 100%
width: 100%
text-align: left

0 comments on commit 1544fb1

Please sign in to comment.