Skip to content

Commit

Permalink
fix(terminal): fixed terminal output issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Izak88 authored and jkuri committed Nov 21, 2017
1 parent 8d36d0c commit 1753441
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ describe('Terminal Component', () => {
.createComponent(AppTerminalComponent);
});

it('should expect termReady to be false', () => {
expect(fixture.componentInstance.terminalReady).toBe(false);
it('should expect term to be defined', () => {
expect(fixture.componentInstance.term).toBeDefined();
});

it('should expect term to be defined', () => {
it('should expect data to be undefined', () => {
fixture.detectChanges();
expect(fixture.componentInstance.term).toBeDefined();
expect(fixture.componentInstance.data).toBeUndefined();
});
});
50 changes: 8 additions & 42 deletions src/app/components/app-terminal/app-terminal.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import {
Component,
ElementRef,
OnInit,
Input,
SimpleChange,
EventEmitter,
Inject
} from '@angular/core';
import { Component, ElementRef, OnInit, Input, SimpleChange, Inject } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import * as xterminal from 'xterm';

Expand All @@ -18,42 +10,27 @@ export class AppTerminalComponent implements OnInit {
@Input() data: any;
@Input() options: { size: 'normal' | 'large', newline: boolean };
term: any;
terminalReady: boolean;
unwritenChanges: string[];

constructor(
private elementRef: ElementRef,
@Inject(DOCUMENT) private document: any
) {
this.terminalReady = false;
this.unwritenChanges = [];
}

ngOnInit() {
let el = this.elementRef.nativeElement;
let xterm: any = <any>xterminal;
xterm.loadAddon('fit');
this.term = new xterm({
scrollback: 15000,
cols: 120
});
}

this.term.on('open', () => {
this.terminalReady = true;
if (this.unwritenChanges.length) {
this.unwritenChanges.forEach(p => this.printToTerminal(p));
this.unwritenChanges = [];
}
});
ngOnInit() {
let el = this.elementRef.nativeElement;

this.term.on('open', () => this.term.fit());
this.term.open(el.querySelector('.window-terminal-container'), true);
setTimeout(() => {
this.term.fit();
});
}

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

Expand All @@ -62,21 +39,10 @@ export class AppTerminalComponent implements OnInit {
return;
}

if (!this.terminalReady) {
this.unwritenChanges.push(this.data);
} else {
this.printToTerminal(this.data);
}
}

printToTerminal(data: string) {
if (this.options.newline) {
this.term.writeln(data);
this.term.writeln(this.data);
} else {
this.term.write(data);
this.term.write(this.data);
}
setTimeout(() => {
this.term.fit();
});
}
}

0 comments on commit 1753441

Please sign in to comment.