@@ -4,6 +4,7 @@ import * as TerminalFit from 'xterm/lib/addons/fit/fit';
44import SpinnerOverlay from '@/components/elements/SpinnerOverlay' ;
55import { State , useStoreState } from 'easy-peasy' ;
66import { ApplicationState } from '@/state/types' ;
7+ import { connect } from 'formik' ;
78
89const theme = {
910 background : 'transparent' ,
@@ -27,7 +28,7 @@ const theme = {
2728} ;
2829
2930export default ( ) => {
30- const connected = useStoreState ( ( state : State < ApplicationState > ) => state . server . socket . connected ) ;
31+ const { instance , connected } = useStoreState ( ( state : State < ApplicationState > ) => state . server . socket ) ;
3132
3233 const ref = createRef < HTMLDivElement > ( ) ;
3334 const terminal = useRef ( new Terminal ( {
@@ -40,17 +41,34 @@ export default () => {
4041 theme : theme ,
4142 } ) ) ;
4243
44+ const handleServerLog = ( lines : string [ ] ) => lines . forEach ( data => {
45+ return data . split ( / \n / g) . forEach ( line => terminal . current . writeln ( line + '\u001b[0m' ) ) ;
46+ } ) ;
47+
48+ const handleConsoleOutput = ( line : string ) => terminal . current . writeln ( line . replace ( / (?: \r \n | \r | \n ) $ / im, '' ) + '\u001b[0m' ) ;
49+
4350 useEffect ( ( ) => {
4451 ref . current && terminal . current . open ( ref . current ) ;
4552
4653 // @see https://github.com/xtermjs/xterm.js/issues/2265
4754 // @see https://github.com/xtermjs/xterm.js/issues/2230
4855 TerminalFit . fit ( terminal . current ) ;
49-
50- terminal . current . writeln ( 'Testing console data' ) ;
51- terminal . current . writeln ( 'Testing other data' ) ;
5256 } , [ ] ) ;
5357
58+ useEffect ( ( ) => {
59+ if ( connected && instance ) {
60+ instance . addListener ( 'server log' , handleServerLog ) ;
61+ instance . addListener ( 'console output' , handleConsoleOutput ) ;
62+ }
63+ } , [ connected ] ) ;
64+
65+ useEffect ( ( ) => ( ) => {
66+ if ( instance ) {
67+ instance . removeListener ( 'server log' , handleServerLog ) ;
68+ instance . removeListener ( 'console output' , handleConsoleOutput ) ;
69+ }
70+ } ) ;
71+
5472 return (
5573 < div className = { 'text-xs font-mono relative' } >
5674 < SpinnerOverlay visible = { ! connected } large = { true } />
0 commit comments