1- import {
2- Component , ElementRef , OnInit , Input , SimpleChange , EventEmitter , NgZone } from '@angular/core' ;
1+ import { Component , ElementRef , OnInit , Input , SimpleChange , EventEmitter } from '@angular/core' ;
32import { ISlimScrollOptions , SlimScrollEvent } from 'ngx-slimscroll' ;
43
54import * as AnsiUp from 'ansi_up' ;
@@ -16,7 +15,7 @@ export class AppTerminalComponent implements OnInit {
1615 scrollEvents : EventEmitter < SlimScrollEvent > ;
1716 commands : { command : string , visible : boolean , output : string } [ ] ;
1817
19- constructor ( private elementRef : ElementRef , private ngZone : NgZone ) {
18+ constructor ( private elementRef : ElementRef ) {
2019 this . scrollOptions = {
2120 position : 'right' ,
2221 barBackground : '#11121A' ,
@@ -33,6 +32,7 @@ export class AppTerminalComponent implements OnInit {
3332 } ;
3433
3534 this . scrollEvents = new EventEmitter < SlimScrollEvent > ( ) ;
35+ this . commands = [ ] ;
3636 }
3737
3838 ngOnInit ( ) {
@@ -46,52 +46,46 @@ export class AppTerminalComponent implements OnInit {
4646 return ;
4747 }
4848
49- this . ngZone . run ( ( ) => {
50- const el = this . elementRef . nativeElement . querySelector ( '.window-terminal-container' ) ;
51- if ( typeof this . data . clear !== 'undefined' ) {
52- el . innerHTML = '' ;
53- } else {
54- let output : string = this . au . ansi_to_html ( this . data ) ;
55- if ( output ) {
56- if ( this . commands . length > 0 ) {
57- if ( output . indexOf ( '==>' ) !== - 1 ) {
58- let command = output . split ( '</span>' ) [ 0 ] + '</span>' ;
59- this . commands . push ( {
60- command : command ,
61- visible : true ,
62- output : output
63- } ) ;
64- } else {
65- this . commands [ this . commands . length - 1 ] . command += ` ${ output } ` ;
66- }
67- } else {
68- let regexp = / < s p a n ( .* ) = = & g t ; / gi;
69- regexp . lastIndex = 1 ;
70- let match = regexp . exec ( output ) ;
71- if ( match ) {
72- let indexEnd = match . index ;
73- let indexStart = 0 ;
74- while ( indexEnd >= 0 ) {
75- let log = output . substring ( indexStart , indexEnd ) ;
76- let command = log . split ( '</span>' ) [ 0 ] + '</span>' ;
77- this . commands . push ( {
78- command : command ,
79- visible : true ,
80- output : log
81- } ) ;
82- indexStart = indexEnd ;
83- indexEnd = regexp . exec ( output ) . index ;
84- }
85- }
86- }
87- }
49+ if ( typeof this . data . clear !== 'undefined' ) {
50+ this . commands = [ ] ;
51+ } else {
52+ const output : string = this . au . ansi_to_html ( this . data ) ;
53+ const regex = / < s p a n ( .* ) = = & g t ; ( .* ) < \/ s p a n > / g;
54+ let match ;
55+ let commands : string [ ] = [ ] ;
8856
89- const recalculateEvent = new SlimScrollEvent ( { type : 'recalculate' } ) ;
90- const bottomEvent = new SlimScrollEvent ( { type : 'scrollToBottom' , duration : 300 } ) ;
57+ if ( output . match ( regex ) ) {
58+ while ( match = regex . exec ( output ) ) { commands . push ( match [ 0 ] ) ; }
59+
60+ if ( commands . length > 1 ) {
61+ this . commands = [ ] ;
62+ }
9163
92- setTimeout ( ( ) => el . scrollTop = el . scrollHeight ) ;
64+ this . commands = commands . reduce ( ( acc , curr , i ) => {
65+ const next = commands [ i + 1 ] || '' ;
66+ const re = new RegExp ( '(' + curr + ')(' + '[\\s\\S]*' + ')(' + next + ')' ) ;
67+ return acc . concat ( {
68+ command : curr ,
69+ visible : i === commands . length - 1 ? true : false ,
70+ output : output . match ( re ) && output . match ( re ) [ 2 ] ? output . match ( re ) [ 2 ] . trim ( ) : ''
71+ } ) ;
72+ } , this . commands ) ;
73+ } else {
74+ this . commands [ this . commands . length - 1 ] . output += output ;
75+ this . commands = this . commands . map ( ( cmd , i ) => {
76+ cmd . visible = i === this . commands . length - 1 ? true : false ;
77+ return cmd ;
78+ } ) ;
9379 }
94- } ) ;
80+
81+ const recalculateEvent = new SlimScrollEvent ( { type : 'recalculate' } ) ;
82+ const bottomEvent = new SlimScrollEvent ( { type : 'scrollToBottom' , duration : 300 } ) ;
83+
84+ setTimeout ( ( ) => {
85+ this . scrollEvents . emit ( recalculateEvent ) ;
86+ this . scrollEvents . emit ( bottomEvent ) ;
87+ } ) ;
88+ }
9589 }
9690
9791 toogleCommand ( index : number ) {
0 commit comments