1- const { killDotnetProcessAsync, startDotnetProcess } = require ( '../../tasks' ) ;
1+ const { killDotnetProcessAsync, startDotnetProcess, startCleanProcess } = require ( '../../tasks' ) ;
2+ const Terminal = require ( 'xterm' ) . Terminal ;
3+ const fit = require ( 'xterm/lib/addons/fit/fit' ) ;
4+ const fullScreen = require ( 'xterm/lib/addons/fullscreen/fullscreen' ) ;
5+ const debounce = require ( '../../utils/debounce' ) ;
6+
7+ Terminal . applyAddon ( fit ) ;
8+ Terminal . applyAddon ( fullScreen ) ;
9+
210const WebComponentBase = require ( '../WebComponentBase' ) ;
311
412module . exports = class RunnerElement extends WebComponentBase {
@@ -14,6 +22,8 @@ module.exports = class RunnerElement extends WebComponentBase {
1422
1523 this . _name = '' ;
1624
25+ this . _terminalProcess ;
26+
1727 this . setState ( RunnerElement . states . stopped ) ;
1828 }
1929
@@ -34,10 +44,48 @@ module.exports = class RunnerElement extends WebComponentBase {
3444 this . setState ( this . state ) ;
3545
3646 const clearLog = shadow . querySelector ( '.clear-log' ) ;
47+ const clean = shadow . querySelector ( '.clean' ) ;
48+ const full = shadow . querySelector ( '.full' ) ;
49+ const terminal = shadow . querySelector ( '.terminals' ) ;
3750
3851 clearLog . addEventListener ( 'click' , ( ) => this . clearData ( ) ) ;
52+ clean . addEventListener ( 'click' , ( ) => this . clean ( ) ) ;
53+
54+ terminal . addEventListener ( 'click' , ( e ) => {
55+ if ( e . target . classList . contains ( 'full-screen' ) ) {
56+ terminal . classList . remove ( 'full-screen' ) ;
57+ this . _terminalProcess . fit ( ) ;
58+ }
59+ } ) ;
60+
61+ full . addEventListener ( 'click' , ( ) => {
62+ terminal . classList . add ( 'full-screen' ) ;
63+
64+ this . resize ( ) ;
65+ } ) ;
66+
67+ this . _terminalProcess = new Terminal ( ) ;
68+ this . _terminalProcess . setOption ( 'disableStdin' , true ) ;
69+ this . _terminalProcess . setOption ( 'fontFamily' , "Consolas, 'Courier New', monospace" ) ;
70+
71+ this . _terminalProcess . open ( shadow . querySelector ( '.terminals' ) ) ;
3972
4073 shadow . querySelector ( '.action' ) . addEventListener ( 'click' , this . _onToggle . bind ( this ) ) ;
74+
75+ // Terminal wont fit itself on resize.
76+ window . addEventListener ( 'resize' , debounce ( this . resize . bind ( this ) , {
77+ delay : 100 ,
78+ executeOnFirstRun : true
79+ } ) ) ;
80+
81+ this . resize ( ) ;
82+ }
83+
84+ resize ( ) {
85+ this . _terminalProcess . fit ( ) ;
86+
87+ if ( this . _runningProccess )
88+ this . _runningProccess . resize ( this . _terminalProcess . cols , this . _terminalProcess . rows ) ;
4189 }
4290
4391 _enableAction ( ) {
@@ -67,6 +115,20 @@ module.exports = class RunnerElement extends WebComponentBase {
67115 }
68116 }
69117
118+ _enableClean ( ) {
119+ if ( ! this . shadowRoot )
120+ return ;
121+
122+ this . shadowRoot . querySelector ( '.clean' ) . removeAttribute ( 'disabled' ) ;
123+ }
124+
125+ _disableClean ( ) {
126+ if ( ! this . shadowRoot )
127+ return ;
128+
129+ this . shadowRoot . querySelector ( '.clean' ) . setAttribute ( 'disabled' , 'disabled' ) ;
130+ }
131+
70132 onStart ( ) {
71133 if ( this . state === RunnerElement . states . running || this . state === RunnerElement . states . starting )
72134 return ;
@@ -75,43 +137,43 @@ module.exports = class RunnerElement extends WebComponentBase {
75137
76138 this . setState ( RunnerElement . states . starting ) ;
77139
78- this . _runningProccess = startDotnetProcess ( this . cwd , true , this . runCommandArguments ) ;
140+ this . _terminalProcess . writeln ( "Starting..." ) ;
79141
80- this . _runningProccess . on ( 'close' , ( ) => {
142+ this . _runningProccess = startDotnetProcess ( this . cwd , true , this . runCommandArguments , this . _terminalProcess . cols , this . _terminalProcess . rows ) ;
143+
144+ this . _runningProccess . on ( 'exit' , ( ) => {
81145 this . setState ( RunnerElement . states . stopped ) ;
82146
83147 this . _runningProccess = undefined ;
84148 } ) ;
85149
86- this . _runningProccess . stdout . on ( 'data' , ( d ) => this . onData ( d . toString ( ) ) ) ;
87- this . _runningProccess . stderr . on ( 'data' , ( d ) => this . onData ( d . toString ( ) , true ) ) ;
88- }
150+ this . _runningProccess . once ( 'data' , ( ) => this . setState ( RunnerElement . states . running ) ) ;
89151
90- onData ( d , errorData ) {
91- if ( this . state === RunnerElement . states . starting )
92- this . setState ( RunnerElement . states . running ) ;
152+ this . _runningProccess . on ( 'data' , ( d ) => {
153+ this . _terminalProcess . write ( d ) ;
154+ } ) ;
155+ }
93156
94- const el = document . createElement ( 'span' ) ;
95- const terminal = this . shadowRoot . querySelector ( '.terminal' ) ;
157+ clean ( ) {
158+ if ( this . state === RunnerElement . states . running || this . state === RunnerElement . states . starting )
159+ return ;
96160
97- el . classList . add ( 'log-item' ) ;
161+ this . _runningProccess = startCleanProcess ( this . cwd ) ;
98162
99- if ( errorData )
100- el . classList . add ( 'error' ) ;
101-
102- el . textContent = d ;
163+ this . _runningProccess . on ( 'exit' , ( ) => {
164+ this . setState ( RunnerElement . states . stopped ) ;
103165
104- terminal . appendChild ( el ) ;
166+ this . _runningProccess = undefined ;
167+ } ) ;
105168
106- terminal . scrollTop = terminal . scrollHeight ;
169+ this . _runningProccess . on ( 'data' , ( d ) => {
170+ this . setState ( RunnerElement . states . running ) ;
171+ this . _terminalProcess . write ( d ) ;
172+ } ) ;
107173 }
108174
109175 clearData ( ) {
110- const terminal = this . shadowRoot . querySelector ( '.terminal' ) ;
111-
112- while ( terminal . firstChild ) {
113- terminal . removeChild ( terminal . firstChild ) ;
114- }
176+ this . _terminalProcess . clear ( ) ;
115177 }
116178
117179 onTerminate ( ) {
@@ -136,6 +198,7 @@ module.exports = class RunnerElement extends WebComponentBase {
136198
137199 setState ( state ) {
138200 this . _disableAction ( ) ;
201+ this . _disableClean ( ) ;
139202
140203 this . state = state ;
141204
@@ -166,6 +229,7 @@ module.exports = class RunnerElement extends WebComponentBase {
166229 stateEl . textContent = 'Stopped' ;
167230 stateEl . className = 'state badge badge-secondary' ;
168231 this . _enableAction ( ) ;
232+ this . _enableClean ( ) ;
169233 actionBtn . textContent = 'Start' ;
170234 break ;
171235 }
0 commit comments