import deepmerge from 'deepmerge' ; import { Terminal } from 'xterm' ; import dateTime from 'date-time' ; import { FitAddon } from 'xterm-addon-fit' ; import { WebLinksAddon } from 'xterm-addon-web-links' ; const defaultTheme = { foreground : '#fff' , background : '#333646' , cursor : 'rgba(0, 0, 0, 0.5)' , selection : 'rgba(0, 0, 0, 0.5)' , brightGreen : '#42b983' , brightYellow : '#ea6e00' , } ; const defaultOptions = { cols : 90 , rows : 36 , theme : defaultTheme , fontSize : 12 , } ; const writeChunkFn = ( term ) => { const writeChunk = ( data , ln = true ) => { if ( data && data . indexOf ( '\n' ) !== - 1 ) { data . split ( '\n' ) . forEach ( ( value ) => writeChunk ( value ) ) ; return ; } if ( typeof data === 'string' ) { if ( ln ) { term . writeln ( data ) ; } else { term . write ( data ) ; } } } ; return writeChunk ; } ; const writeLog = ( term ) => ( data ) => term . writeln ( `${ dateTime ( ) } ${ data }` ) ; class XtermManager { terms : any ; constructor ( ) { this . terms = { } ; } create ( id , container , options = { } ) { const opts = deepmerge ( defaultOptions , options ) ; if ( ! this . terms [ id ] ) { this . terms [ id ] = new Terminal ( opts ) ; this . terms [ id ] . writeChunk = writeChunkFn ( this . terms [ id ] ) ; this . terms [ id ] . writeLog = writeLog ( this . terms [ id ] ) ; const fitAddon = new FitAddon ( ) ; this . terms [ id ] . loadAddon ( fitAddon ) ; this . terms [ id ] . fitAddon = fitAddon ; this . terms [ id ] . loadAddon ( new WebLinksAddon ( ) ) ; } else { this . terms [ id ] . _core . options . theme = opts . theme ; } this . terms [ id ] . open ( container ) ; this . terms [ id ] . fitAddon . fit ( ) ; return this . terms [ id ] ; } param { string } id getTerm ( id ) { return this . terms [ id ] ; } } export default new XtermManager ( ) ;