Skip to content

Commit

Permalink
Record and expose command history
Browse files Browse the repository at this point in the history
Closes #1
  • Loading branch information
bbenoist committed May 17, 2016
1 parent 2104254 commit f20ecf9
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 14 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ Press `F1` and select the command with title: `Shell: Run Command at Active File

This will open a new input box where you will just have to type youd shell command and press `Enter`.

### Show Command History
Press `F1` and select the command with title: `Shell: Show Command History`.

This will open a list of previous commands which can be re-executed when pressing `Enter`.

### Terminate Running Command
Press `F1` and select the command with title: `Shell: Terminate Running command`.

Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"activationEvents": [
"onCommand:extension.shellCMD",
"onCommand:extension.cwdShellCMD",
"onCommand:shell.showHistory",
"onCommand:extension.shellTerm",
"onCommand:extension.shellLog"
],
Expand All @@ -40,6 +41,11 @@
"title": "Run Command at Active File Location",
"category": "Shell"
},
{
"command": "shell.showHistory",
"title": "Show Command History",
"category": "Shell"
},
{
"command": "extension.shellTerm",
"title": "Terminate Running Command",
Expand Down
4 changes: 4 additions & 0 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Command {
cmd:string;
cwd:string;
}
57 changes: 43 additions & 14 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import * as vscode from 'vscode';
import * as path from 'path';
var spawnCMD = require('spawn-command');
var treeKill = require('tree-kill');
import * as history from './history';

var process = null;
var commandOutput = null;
var commandHistory : history.CommandHistory = null;

function run(cmd:string, cwd:string) {
return new Promise((accept, reject) => {
Expand Down Expand Up @@ -40,6 +42,20 @@ function term() {
});
}

function exec(cmd:string, cwd:string) {
if (!cmd) { return; }
commandHistory.enqueue(cmd, cwd);
commandOutput.clear();
commandOutput.appendLine(`> Running command \`${cmd}\`...`)
run(cmd, cwd).then(() => {
commandOutput.appendLine(`> Command \`${cmd}\` ran successfully.`);
}).catch((reason) => {
commandOutput.appendLine(`> ERROR: ${reason}`);
vscode.window.showErrorMessage(reason, 'Show Output')
.then((action) => { commandOutput.show(); });
});
}

function execShellCMD(cwd:string) {
if (process) {
const msg = 'There is an active running shell command right now. Terminate it before executing another shell command.';
Expand All @@ -50,24 +66,34 @@ function execShellCMD(cwd:string) {
}
});
} else {
vscode.window.showInputBox({placeHolder: 'Type your shell command here.'}).then(
(cmd) => {
if (!cmd) { return; }
commandOutput.clear();
commandOutput.appendLine(`> Running command \`${cmd}\`...`)
run(cmd, cwd).then(() => {
commandOutput.appendLine(`> Command ${cmd} ran successfully.`);
}).catch((reason) => {
commandOutput.appendLine(`> ERROR: ${reason}`);
vscode.window.showErrorMessage(reason, 'Show Output')
.then((action) => { commandOutput.show(); });
});
}
);
var lastCmd = commandHistory.last();
var options = {
placeHolder: 'Type your shell command here.',
value: lastCmd ? lastCmd.cmd : undefined
};
vscode.window.showInputBox(options).then((cmd) => {
exec(cmd, cwd);
});
}
}

function showHistory() {
return new Promise((accept, reject) => {
let items: vscode.QuickPickItem[] = commandHistory.commands().map((cmd) => {
return { label: cmd.cmd, detail: cmd.cwd, cmd: cmd, description: undefined };
});
vscode.window.showQuickPick(items).then((value:any) => {
if (value) {
exec(value.cmd.cmd, value.cmd.cwd);
}
})
});
}

export function activate(context: vscode.ExtensionContext) {
commandHistory = new history.CommandHistory();
context.subscriptions.push(commandHistory);

commandOutput = vscode.window.createOutputChannel('Shell');
context.subscriptions.push(commandOutput);

Expand All @@ -85,6 +111,9 @@ export function activate(context: vscode.ExtensionContext) {
});
context.subscriptions.push(cwdShellCMD);

let shellHistory = vscode.commands.registerCommand('shell.showHistory', showHistory)
context.subscriptions.push(shellHistory);

let shellTerm = vscode.commands.registerCommand('extension.shellTerm', () => {
if (process) {
term();
Expand Down
26 changes: 26 additions & 0 deletions src/history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as command from './command';

export class CommandHistory {
private history : command.Command[] = [];

public dispose() {
}

public enqueue(cmd:string, cwd:string) {
var last = this.last();
if (last == undefined || (last.cmd !== cmd || last.cwd !== cwd)) {
this.history.push({cmd:cmd, cwd:cwd});
}
}

public commands() {
return this.history;
}

public last() {
if (this.history.length == 0) {
return undefined;
}
return this.history[this.history.length - 1];
}
}

0 comments on commit f20ecf9

Please sign in to comment.