Skip to content

Commit

Permalink
Add terminal renderer and active terminal examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyriar committed Jun 18, 2018
1 parent e3b5538 commit 5f23d63
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 1 deletion.
32 changes: 31 additions & 1 deletion terminal-sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@
],
"activationEvents": [
"onCommand:terminalTest.createAndSend",
"onCommand:terminalTest.createFakeShell",
"onCommand:terminalTest.createTerminal",
"onCommand:terminalTest.createZshLoginShell",
"onCommand:terminalTest.dimensions",
"onCommand:terminalTest.dispose",
"onCommand:terminalTest.hide",
"onCommand:terminalTest.onData",
"onCommand:terminalTest.maximumDimensions",
"onCommand:terminalTest.processId",
"onCommand:terminalTest.sendText",
"onCommand:terminalTest.sendTextNoNewLine",
"onCommand:terminalTest.show",
"onCommand:terminalTest.showPreserveFocus",
"onCommand:terminalTest.terminalRendererCreate",
"onCommand:terminalTest.terminalRendererName",
"onCommand:terminalTest.terminalTest.terminalRendererWrite",
"onCommand:terminalTest.terminals"
],
"main": "./out/extension",
Expand All @@ -32,6 +38,10 @@
"command": "terminalTest.createAndSend",
"title": "Terminal API: Create Terminal and Immediately Send"
},
{
"command": "terminalTest.createFakeShell",
"title": "Terminal API: Create Fake Shell (with Terminal Renderer)"
},
{
"command": "terminalTest.createTerminal",
"title": "Terminal API: Create Terminal"
Expand All @@ -40,6 +50,10 @@
"command": "terminalTest.createZshLoginShell",
"title": "Terminal API: Create Terminal (zsh login shell)"
},
{
"command": "terminalTest.dimensions",
"title": "Terminal API: Set dimensions"
},
{
"command": "terminalTest.dispose",
"title": "Terminal API: Dispose"
Expand All @@ -52,6 +66,10 @@
"command": "terminalTest.onData",
"title": "Terminal API: Attach data listener"
},
{
"command": "terminalTest.maximumDimensions",
"title": "Terminal API: Get maximum dimensions"
},
{
"command": "terminalTest.processId",
"title": "Terminal API: Get process ID"
Expand All @@ -72,6 +90,18 @@
"command": "terminalTest.showPreserveFocus",
"title": "Terminal API: Show (preserving focus)"
},
{
"command": "terminalTest.terminalRendererCreate",
"title": "Terminal API: Create Terminal Renderer"
},
{
"command": "terminalTest.terminalRendererName",
"title": "Terminal API: Set Terminal Renderer Name"
},
{
"command": "terminalTest.terminalRendererWrite",
"title": "Terminal API: Write to Terminal Renderer"
},
{
"command": "terminalTest.terminals",
"title": "Terminal API: View terminals"
Expand All @@ -94,4 +124,4 @@
"typescript": "^2.0.3",
"vscode": "^1.1.17"
}
}
}
100 changes: 100 additions & 0 deletions terminal-sample/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,26 @@ import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
let NEXT_TERM_ID = 1;

vscode.window.showInformationMessage('Hello World!');

console.log("Terminals: " + (<any>vscode.window).terminals.length);

(<any>vscode.window).onDidOpenTerminal(e => {
console.log("Terminal opened. Total count: " + (<any>vscode.window).terminals.length);

e.onData(data => {
console.log("Terminal data: ", data);
});
});

(<any>vscode.window).onDidChangeActiveTerminal(e => {
console.log(`Active terminal changed, name=${e ? e.name : 'undefined'}`);
});

// vscode.window.createTerminal
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.createTerminal', () => {
vscode.window.createTerminal(`Ext Terminal #${NEXT_TERM_ID++}`);
vscode.window.showInformationMessage('Hello World 2!');
}));
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.createAndSend', () => {
const terminal = vscode.window.createTerminal(`Ext Terminal #${NEXT_TERM_ID++}`);
Expand Down Expand Up @@ -100,6 +117,89 @@ export function activate(context: vscode.ExtensionContext) {
});
});
}));

// vvv Proposed APIs in 1.25 below vvv
let renderer;
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.terminalRendererCreate', () => {
renderer = (<any>vscode.window).createTerminalRenderer('renderer');
renderer.write(colorText('~~~ Hello world! ~~~'));
renderer.onDidChangeMaximumDimensions(dim => {
console.log(`Dimensions for renderer changed: cols=${dim.cols}, rows=${dim.rows}`);
});
}));
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.terminalRendererName', () => {
if (!renderer) {
return;
}
vscode.window.showInputBox({ placeHolder: "Enter a new name" }).then(value => {
if (!value) {
return;
}
renderer.name = value;
});
}));
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.terminalRendererWrite', () => {
if (!renderer) {
return;
}
vscode.window.showInputBox({ placeHolder: "Enter text to write" }).then(value => {
if (!value) {
return;
}
// Note that entering characters like `\r` in the input box will result in `\\r` being written
renderer.write(value);
});
}));
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.createFakeShell', () => {
const shell = (<any>vscode.window).createTerminalRenderer('fake shell');
shell.write('Type and press enter to echo the text\r\n\r\n');
let line = '';
shell.onInput(data => {
if (data === '\r') {
shell.write(`\r\necho: "${colorText(line)}"\r\n\n`);
line = '';
return;
}
line += data;
shell.write(data);
});
shell.terminal.then(t => t.show());
}));
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.maximumDimensions', () => {
renderer.maximumDimensions.then(dimensions => {
vscode.window.showInformationMessage(`TerminalRenderer.maximumDimensions: cols=${dimensions.cols}, rows=${dimensions.rows}`);
});
}));
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.dimensions', () => {
vscode.window.showInputBox({ placeHolder: "Enter cols" }).then(cols => {
if (!cols) {
return;
}
vscode.window.showInputBox({ placeHolder: "Enter rows" }).then(rows => {
if (!rows) {
return;
}
renderer.dimensions = { cols: parseInt(cols, 10), rows: parseInt(rows, 10) };
});
});
}));
}

function colorText(text: string): string {
let output = '';
let colorIndex = 1;
for (let i = 0; i < text.length; i++) {
const char = text.charAt(i);
if (char === ' ' || char === '\r' || char === '\n') {
output += char;
} else {
output += `\x1b[3${colorIndex++}m${text.charAt(i)}\x1b[0m`;
if (colorIndex > 6) {
colorIndex = 1;
}
}
}
return output;
}

function selectTerminal(): Thenable<vscode.Terminal> {
Expand Down

0 comments on commit 5f23d63

Please sign in to comment.