-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathextension.ts
199 lines (178 loc) · 6.26 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
'use strict';
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
let NEXT_TERM_ID = 1;
console.log("Terminals: " + (<any>vscode.window).terminals.length);
// vscode.window.onDidOpenTerminal
vscode.window.onDidOpenTerminal(terminal => {
console.log("Terminal opened. Total count: " + (<any>vscode.window).terminals.length);
});
vscode.window.onDidOpenTerminal((terminal: vscode.Terminal) => {
vscode.window.showInformationMessage(`onDidOpenTerminal, name: ${terminal.name}`);
});
// vscode.window.onDidChangeActiveTerminal
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.createTerminalHideFromUser', () => {
vscode.window.createTerminal({
name: `Ext Terminal #${NEXT_TERM_ID++}`,
hideFromUser: true
} as any);
}));
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.createAndSend', () => {
const terminal = vscode.window.createTerminal(`Ext Terminal #${NEXT_TERM_ID++}`);
terminal.sendText("echo 'Sent text immediately after creating'");
}));
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.createZshLoginShell', () => {
vscode.window.createTerminal(`Ext Terminal #${NEXT_TERM_ID++}`, '/bin/zsh', ['-l']);
}));
// Terminal.hide
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.hide', () => {
if (ensureTerminalExists()) {
selectTerminal().then(terminal => {
if (terminal) {
terminal.hide();
}
});
}
}));
// Terminal.show
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.show', () => {
if (ensureTerminalExists()) {
selectTerminal().then(terminal => {
if (terminal) {
terminal.show();
}
});
}
}));
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.showPreserveFocus', () => {
if (ensureTerminalExists()) {
selectTerminal().then(terminal => {
if (terminal) {
terminal.show(true);
}
});
}
}));
// Terminal.sendText
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.sendText', () => {
if (ensureTerminalExists()) {
selectTerminal().then(terminal => {
if (terminal) {
terminal.sendText("echo 'Hello world!'");
}
});
}
}));
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.sendTextNoNewLine', () => {
if (ensureTerminalExists()) {
selectTerminal().then(terminal => {
if (terminal) {
terminal.sendText("echo 'Hello world!'", false);
}
});
}
}));
// Terminal.dispose
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.dispose', () => {
if (ensureTerminalExists()) {
selectTerminal().then(terminal => {
if (terminal) {
terminal.dispose();
}
});
}
}));
// Terminal.processId
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.processId', () => {
selectTerminal().then(terminal => {
if (!terminal) {
return;
}
terminal.processId.then((processId) => {
if (processId) {
vscode.window.showInformationMessage(`Terminal.processId: ${processId}`);
} else {
vscode.window.showInformationMessage('Terminal does not have a process ID');
}
});
});
}));
// vscode.window.onDidCloseTerminal
vscode.window.onDidCloseTerminal((terminal) => {
vscode.window.showInformationMessage(`onDidCloseTerminal, name: ${terminal.name}`);
});
// vscode.window.terminals
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.terminals', () => {
selectTerminal();
}));
// vvv Proposed APIs below vvv
// vscode.window.onDidWriteTerminalData
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.onDidWriteTerminalData', () => {
(<any>vscode.window).onDidWriteTerminalData((e: any) => {
vscode.window.showInformationMessage(`onDidWriteTerminalData listener attached, check the devtools console to see events`);
console.log('onDidWriteData', e);
});
}));
// vscode.window.onDidChangeTerminalDimensions
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.onDidChangeTerminalDimensions', () => {
vscode.window.showInformationMessage(`Listening to onDidChangeTerminalDimensions, check the devtools console to see events`);
(<any>vscode.window).onDidChangeTerminalDimensions((event: any) => {
console.log(`onDidChangeTerminalDimensions: terminal:${event.terminal.name}, columns=${event.dimensions.columns}, rows=${event.dimensions.rows}`);
});
}));
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.updateEnvironment', () => {
const collection = (context as any).environmentVariableCollection;
collection.replace('FOO', 'BAR');
collection.append('PATH', '/test/path');
}));
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.clearEnvironment', () => {
const collection = (context as any).environmentVariableCollection;
collection.clear();
}));
}
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 | undefined> {
interface TerminalQuickPickItem extends vscode.QuickPickItem {
terminal: vscode.Terminal;
}
const terminals = <vscode.Terminal[]>(<any>vscode.window).terminals;
const items: TerminalQuickPickItem[] = terminals.map(t => {
return {
label: `name: ${t.name}`,
terminal: t
};
});
return vscode.window.showQuickPick(items).then(item => {
return item ? item.terminal : undefined;
});
}
function ensureTerminalExists(): boolean {
if ((<any>vscode.window).terminals.length === 0) {
vscode.window.showErrorMessage('No active terminals');
return false;
}
return true;
}