@@ -85,6 +85,7 @@ async function pickDevice(): Promise<void> {
8585}
8686
8787const activeDebugSessions = new Set < string > ( ) ;
88+ let debugTerminal : vscode . Terminal ;
8889
8990async function handleCustomDebugEvent ( event : vscode . DebugSessionCustomEvent ) : Promise < void > {
9091 let device : Device | undefined ;
@@ -116,35 +117,93 @@ async function handleCustomDebugEvent(event: vscode.DebugSessionCustomEvent): Pr
116117 // run the program
117118 try {
118119 const dirname = path . posix . dirname ( args . program ) ;
119- const command = `brickrun --directory="${ dirname } " "${ args . program } "` ;
120- output . show ( true ) ;
121- output . clear ( ) ;
122- output . appendLine ( `Starting: ${ command } ` ) ;
123- const channel = await device . exec ( command ) ;
124- channel . on ( 'close' , ( ) => {
125- event . session . customRequest ( 'ev3devBrowser.debugger.terminate' ) ;
126- } ) ;
127- channel . on ( 'exit' , ( code , signal , coreDump , desc ) => {
128- output . appendLine ( '----------' ) ;
129- if ( code === 0 ) {
130- output . appendLine ( 'Completed successfully.' ) ;
131- }
132- else if ( code ) {
133- output . appendLine ( `Exited with error code ${ code } .` ) ;
134- }
135- else {
136- output . appendLine ( `Exited with signal ${ signal } .` ) ;
120+ const command = `brickrun -r --directory="${ dirname } " "${ args . program } "` ;
121+ if ( args . interactiveTerminal ) {
122+ const config = vscode . workspace . getConfiguration ( `terminal.integrated.env.${ getPlatform ( ) } ` ) ;
123+ const termEnv = config . get < string > ( 'TERM' ) ;
124+ const ch = await device . exec ( command , { term : termEnv || process . env [ 'TERM' ] || 'xterm-256color' } ) ;
125+ const writeEmitter = new vscode . EventEmitter < string > ( ) ;
126+ ch . stdout . on ( 'data' , ( data : string | Buffer ) => writeEmitter . fire ( String ( data ) ) ) ;
127+ ch . stderr . on ( 'data' , ( data : string | Buffer ) => writeEmitter . fire ( String ( data ) ) ) ;
128+ if ( debugTerminal ) {
129+ debugTerminal . dispose ( ) ;
137130 }
138- activeDebugSessions . delete ( event . session . id ) ;
139- } ) ;
140- channel . on ( 'data' , ( chunk ) => {
141- output . append ( chunk . toString ( ) ) ;
142- } ) ;
143- channel . stderr . on ( 'data' , ( chunk ) => {
144- output . append ( chunk . toString ( ) ) ;
145- } ) ;
146- output . appendLine ( 'Started.' ) ;
147- output . appendLine ( '----------' ) ;
131+ debugTerminal = vscode . window . createTerminal ( {
132+ name : `SSH: ${ device . name } ` ,
133+ pty : {
134+ onDidWrite : writeEmitter . event ,
135+ open : ( dim : vscode . TerminalDimensions | undefined ) => {
136+ if ( dim !== undefined ) {
137+ ch . setWindow ( dim . rows , dim . columns , 0 , 0 ) ;
138+ }
139+ writeEmitter . fire ( `Starting: ${ command } \r\n` ) ;
140+ writeEmitter . fire ( '----------\r\n' ) ;
141+ } ,
142+ close : ( ) => {
143+ ch . close ( ) ;
144+ activeDebugSessions . delete ( event . session . id ) ;
145+ } ,
146+ handleInput : ( data : string ) => {
147+ ch . stdin . write ( data ) ;
148+ } ,
149+ setDimensions : ( dim : vscode . TerminalDimensions ) => {
150+ ch . setWindow ( dim . rows , dim . columns , 0 , 0 ) ;
151+ } ,
152+ } ,
153+ } ) ;
154+ ch . on ( 'close' , ( ) => {
155+ event . session . customRequest ( 'ev3devBrowser.debugger.terminate' ) ;
156+ ch . destroy ( ) ;
157+ } ) ;
158+ ch . on ( 'exit' , ( code , signal , coreDump , desc ) => {
159+ writeEmitter . fire ( '----------\r\n' ) ;
160+ if ( code === 0 ) {
161+ writeEmitter . fire ( 'Completed successfully.\r\n' ) ;
162+ }
163+ else if ( code ) {
164+ writeEmitter . fire ( `Exited with error code ${ code } .\r\n` ) ;
165+ }
166+ else {
167+ writeEmitter . fire ( `Exited with signal ${ signal } .\r\n` ) ;
168+ }
169+ activeDebugSessions . delete ( event . session . id ) ;
170+ } ) ;
171+ ch . on ( 'error' , ( err : any ) => {
172+ vscode . window . showErrorMessage ( `Connection error: ${ err || err . message } ` ) ;
173+ debugTerminal . dispose ( ) ;
174+ ch . destroy ( ) ;
175+ } ) ;
176+ debugTerminal . show ( ) ;
177+ }
178+ else {
179+ output . show ( true ) ;
180+ output . clear ( ) ;
181+ output . appendLine ( `Starting: ${ command } ` ) ;
182+ const channel = await device . exec ( command ) ;
183+ channel . on ( 'close' , ( ) => {
184+ event . session . customRequest ( 'ev3devBrowser.debugger.terminate' ) ;
185+ } ) ;
186+ channel . on ( 'exit' , ( code , signal , coreDump , desc ) => {
187+ output . appendLine ( '----------' ) ;
188+ if ( code === 0 ) {
189+ output . appendLine ( 'Completed successfully.' ) ;
190+ }
191+ else if ( code ) {
192+ output . appendLine ( `Exited with error code ${ code } .` ) ;
193+ }
194+ else {
195+ output . appendLine ( `Exited with signal ${ signal } .` ) ;
196+ }
197+ activeDebugSessions . delete ( event . session . id ) ;
198+ } ) ;
199+ channel . on ( 'data' , ( chunk : string | Buffer ) => {
200+ output . append ( chunk . toString ( ) ) ;
201+ } ) ;
202+ channel . stderr . on ( 'data' , ( chunk ) => {
203+ output . append ( chunk . toString ( ) ) ;
204+ } ) ;
205+ output . appendLine ( '----------' ) ;
206+ }
148207 activeDebugSessions . add ( event . session . id ) ;
149208 }
150209 catch ( err ) {
0 commit comments