Skip to content

Commit

Permalink
Add support to run code in Integrated Terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
formulahendry committed Oct 14, 2016
1 parent aa5180a commit aa16c6d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"publisher": "formulahendry",
"icon": "images/logo.png",
"engines": {
"vscode": "^1.0.0"
"vscode": "^1.6.0"
},
"categories": [
"Languages",
Expand Down Expand Up @@ -152,6 +152,11 @@
"type": "boolean",
"default": true,
"description": "Whether to show extra execution message like [Running] ... and [Done] ..."
},
"code-runner.runInTerminal": {
"type": "boolean",
"default": false,
"description": "Whether to run code in Integrated Terminal"
}
}
},
Expand Down
27 changes: 25 additions & 2 deletions src/codeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const TmpDir = os.tmpdir();

export class CodeManager {
private _outputChannel: vscode.OutputChannel;
private _terminal: vscode.Terminal;
private _isRunning: boolean;
private _process;
private _codeFile: string;
Expand All @@ -20,9 +21,14 @@ export class CodeManager {

constructor() {
this._outputChannel = vscode.window.createOutputChannel('Code');
this._terminal = null;
this._appInsightsClient = new AppInsightsClient();
}

public onDidCloseTerminal(): void {
this._terminal = null;
}

public run(languageId: string = null): void {
if (this._isRunning) {
vscode.window.showInformationMessage('Code is already running!');
Expand Down Expand Up @@ -167,18 +173,35 @@ export class CodeManager {
}

private executeCommand(executor: string) {
if (this._config.get<boolean>('runInTerminal')) {
this.executeCommandInTerminal(executor);
} else {
this.executeCommandInOutputChannel(executor);
}
}

private executeCommandInTerminal(executor: string) {
if (this._terminal === null) {
this._terminal = vscode.window.createTerminal('Code');
}
this._terminal.show();
let command = executor + ' \"' + this._codeFile + '\"';
this._terminal.sendText(command);
}

private executeCommandInOutputChannel(executor: string) {
this._isRunning = true;
let clearPreviousOutput = this._config.get<boolean>('clearPreviousOutput');
if (clearPreviousOutput) {
this._outputChannel.clear();
}
let showExecutionMessage = this._config.get<boolean>('showExecutionMessage');
let showExecutionMessage = this._config.get<boolean>('showExecutionMessage');
this._outputChannel.show();
let exec = require('child_process').exec;
let command = executor + ' \"' + this._codeFile + '\"';
if (showExecutionMessage) {
this._outputChannel.appendLine('[Running] ' + command);
}
}
this._appInsightsClient.sendEvent(executor);
let startTime = new Date();
this._process = exec(command, { cwd: this._cwd });
Expand Down
4 changes: 4 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export function activate(context: vscode.ExtensionContext) {

let codeManager = new CodeManager();

vscode.window.onDidCloseTerminal(() => {
codeManager.onDidCloseTerminal();
});

let run = vscode.commands.registerCommand('code-runner.run', () => {
codeManager.run();
});
Expand Down

0 comments on commit aa16c6d

Please sign in to comment.