33
44import { inject , injectable } from 'inversify' ;
55import { Terminal , Uri } from 'vscode' ;
6+ import { sleep } from '../../../utils/async' ;
67import { ICondaService } from '../../interpreter/contracts' ;
78import { IServiceContainer } from '../../ioc/types' ;
89import { ITerminalManager , IWorkspaceService } from '../application/types' ;
@@ -29,9 +30,11 @@ const IS_TCSHELL = /(tcsh$)/i;
2930@injectable ( )
3031export class TerminalHelper implements ITerminalHelper {
3132 private readonly detectableShells : Map < TerminalShellType , RegExp > ;
33+ private readonly activatedTerminals : Set < Terminal > ;
3234 constructor ( @inject ( IServiceContainer ) private serviceContainer : IServiceContainer ) {
3335
3436 this . detectableShells = new Map < TerminalShellType , RegExp > ( ) ;
37+ this . activatedTerminals = new Set < Terminal > ( ) ;
3538 this . detectableShells . set ( TerminalShellType . powershell , IS_POWERSHELL ) ;
3639 this . detectableShells . set ( TerminalShellType . gitbash , IS_GITBASH ) ;
3740 this . detectableShells . set ( TerminalShellType . bash , IS_BASH ) ;
@@ -108,4 +111,26 @@ export class TerminalHelper implements ITerminalHelper {
108111 }
109112 }
110113 }
114+
115+ public async activateEnvironmentInTerminal ( terminal : Terminal , preserveFocus : boolean = true , resource ?: Uri ) {
116+ if ( this . activatedTerminals . has ( terminal ) ) {
117+ return ;
118+ }
119+ this . activatedTerminals . add ( terminal ) ;
120+ const shellPath = this . getTerminalShellPath ( ) ;
121+ const terminalShellType = ! shellPath || shellPath . length === 0 ? TerminalShellType . other : this . identifyTerminalShell ( shellPath ) ;
122+
123+ const activationCommamnds = await this . getEnvironmentActivationCommands ( terminalShellType , resource ) ;
124+ if ( activationCommamnds ) {
125+ for ( const command of activationCommamnds ! ) {
126+ terminal . show ( preserveFocus ) ;
127+ terminal . sendText ( command ) ;
128+
129+ // Give the command some time to complete.
130+ // Its been observed that sending commands too early will strip some text off.
131+ const delay = ( terminalShellType === TerminalShellType . powershell || TerminalShellType . powershellCore ) ? 1000 : 500 ;
132+ await sleep ( delay ) ;
133+ }
134+ }
135+ }
111136}
0 commit comments