Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.

Commit 0b35263

Browse files
Add terminal api plugin sample. (#28)
* Add terminal api plugin sample. Signed-off-by: Oleksandr Andriienko <[email protected]> * Use async timeOut function. Improve some labels of the commands Signed-off-by: Oleksandr Andriienko <[email protected]>
1 parent 9c62db9 commit 0b35263

File tree

7 files changed

+1539
-0
lines changed

7 files changed

+1539
-0
lines changed

samples/terminal-plugin/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dist/
2+
node_modules/
3+
*.theia
4+
lib

samples/terminal-plugin/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Terminal plugin
2+
Terminal Plugin example demonstrates using Theia terminal plugin api.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "terminal-plugin",
3+
"publisher": "theia",
4+
"keywords": [
5+
"theia-plugin"
6+
],
7+
"version": "0.0.1",
8+
"license": "EPL-2.0",
9+
"files": [
10+
"src"
11+
],
12+
"devDependencies": {
13+
"@theia/plugin": "next",
14+
"@theia/plugin-packager": "latest",
15+
"rimraf": "2.6.2",
16+
"typescript-formatter": "7.2.2",
17+
"typescript": "2.9.2"
18+
},
19+
"scripts": {
20+
"prepare": "yarn run clean && yarn run build",
21+
"clean": "rimraf lib",
22+
"format-code": "tsfmt -r",
23+
"watch": "tsc -watch",
24+
"compile": "tsc",
25+
"build": "yarn run format-code && yarn run compile && theia:plugin pack"
26+
},
27+
"engines": {
28+
"theiaPlugin": "next"
29+
},
30+
"theiaPlugin": {
31+
"backend": "lib/new-terminal-backend.js"
32+
}
33+
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
2+
/**
3+
* Sample demonstrates using terminal api.
4+
*/
5+
6+
import * as theia from '@theia/plugin';
7+
8+
export namespace Commands {
9+
export const CreateTerminalWithHelpArgs: theia.Command = {
10+
id: 'terminal-created-with-help-of-args',
11+
label: 'Create terminal with help of arguments.'
12+
}
13+
14+
export const CreateTerminalWithHelpOptions: theia.Command = {
15+
id: 'terminal-created-with-help-of-options',
16+
label: 'Create terminal with help of options.'
17+
}
18+
19+
export const SendTextToTheTerminal: theia.Command = {
20+
id: 'send-text-to-the-terminal',
21+
label: 'Send text to the terminal.'
22+
}
23+
24+
export const HidePanelWithTerminal: theia.Command = {
25+
id: 'hide-terminal-panel',
26+
label: 'Hide terminal panel after 3 sec.'
27+
}
28+
29+
export const CreateTerminalWithDelay: theia.Command = {
30+
id: 'create-terminal-with-delay',
31+
label: 'Create terminal after 3 sec.'
32+
}
33+
34+
export const DisposeTerminal: theia.Command = {
35+
id: 'dispose-terminal',
36+
label: 'Dispose terminal after 3 sec.'
37+
}
38+
39+
export const SubscribeToOnDidCloseTerminalEvent: theia.Command = {
40+
id: 'subscribe-on-did-close-terminal-event',
41+
label: 'Subscribe to onDidCloseTerminal event.'
42+
}
43+
44+
export const SubcribeToOnDidOpenTerminalEvent: theia.Command = {
45+
id: 'subscribe-on-did-open-terminal-even',
46+
label: 'Subscribe to onDidOpenTerminal event.'
47+
}
48+
49+
export const SubscribeToOnDidChangeActiveTerminal: theia.Command = {
50+
id: 'subscribe-to-on-did-change-active-terminal',
51+
label: 'Subscribe to onDidChangeActiveTerminal event.'
52+
}
53+
54+
export const ShowAmountOfOpenedTerminals: theia.Command = {
55+
id: 'show-amount-of-opened-terminals',
56+
label: 'Show amount of opened terminals.'
57+
}
58+
59+
export const TrackActiveTerminal: theia.Command = {
60+
id: 'track-active-terminal',
61+
label: 'Track current active terminal during 30c.',
62+
}
63+
}
64+
65+
export function start(context: theia.PluginContext) {
66+
context.subscriptions.push(theia.commands.registerCommand(Commands.CreateTerminalWithHelpArgs, () => {
67+
const terminal = theia.window.createTerminal('Sh Terminal', 'sh', ['-l']);
68+
terminal.show();
69+
}));
70+
71+
context.subscriptions.push(theia.commands.registerCommand(Commands.CreateTerminalWithHelpOptions, () => {
72+
const termOptions: theia.TerminalOptions = {
73+
name: 'Test terminal',
74+
shellPath: '/bin/bash',
75+
shellArgs: ['-l'],
76+
env: { 'HELLO': 'Hello Theia.' },
77+
// cwd: '/home/user/projects/che' // any existed absolute path or url to the folder
78+
}
79+
const terminal = theia.window.createTerminal(termOptions);
80+
terminal.show();
81+
}));
82+
83+
context.subscriptions.push(theia.commands.registerCommand(Commands.SendTextToTheTerminal, () => {
84+
const terminal = createTerminalWithOptions();
85+
terminal.show();
86+
terminal.sendText('clear && echo Theia plugin terminal.\n');
87+
}));
88+
89+
context.subscriptions.push(theia.commands.registerCommand(Commands.HidePanelWithTerminal, async () => {
90+
const terminal = createTerminalWithOptions();
91+
terminal.show();
92+
await sleep(3000);
93+
terminal.hide();
94+
}));
95+
96+
context.subscriptions.push(theia.commands.registerCommand(Commands.CreateTerminalWithDelay, async () => {
97+
const terminal = createTerminalWithOptions();
98+
await sleep(3000);
99+
terminal.show();
100+
}));
101+
102+
context.subscriptions.push(theia.commands.registerCommand(Commands.DisposeTerminal, async () => {
103+
const terminal = createTerminalWithOptions();
104+
terminal.show();
105+
await sleep(3000);
106+
terminal.dispose();
107+
}));
108+
109+
context.subscriptions.push(theia.commands.registerCommand(Commands.SubscribeToOnDidCloseTerminalEvent, async () => {
110+
const terminal = createTerminalWithOptions();
111+
terminal.show();
112+
const processId = await terminal.processId;
113+
114+
context.subscriptions.push(theia.window.onDidCloseTerminal(async (closedTerminal: theia.Terminal) => {
115+
const closedTerminalId = await closedTerminal.processId;
116+
if (closedTerminalId === processId) {
117+
console.log('Terminal closed, id: ', processId);
118+
}
119+
}, processId));
120+
}));
121+
122+
context.subscriptions.push(theia.commands.registerCommand(Commands.SubcribeToOnDidOpenTerminalEvent, async () => {
123+
console.log('Track onOpenTerminal event.');
124+
context.subscriptions.push(theia.window.onDidOpenTerminal(async (openedTerminal: theia.Terminal) => {
125+
const openedTerminalId = await openedTerminal.processId;
126+
console.log('Opened terminal with id: ', openedTerminalId);
127+
}));
128+
}));
129+
130+
context.subscriptions.push(theia.commands.registerCommand(Commands.ShowAmountOfOpenedTerminals, () => {
131+
console.log('Amount opened terminals: ', theia.window.terminals.length);
132+
}));
133+
134+
context.subscriptions.push(theia.commands.registerCommand(Commands.TrackActiveTerminal, async () => {
135+
console.log('Begin track active terminal during 10 sec.');
136+
const trackActiveTerminalInterval = setInterval(async () => {
137+
const active = theia.window.activeTerminal;
138+
if (active) {
139+
const id = await active.processId;
140+
console.log('Active terminal: ', id);
141+
}
142+
}, 500);
143+
144+
context.subscriptions.push(theia.Disposable.create(() => { clearInterval(trackActiveTerminalInterval); }));
145+
146+
await sleep(30000);
147+
console.log('Timeout. Complete track active terminal.');
148+
clearInterval(trackActiveTerminalInterval)
149+
}));
150+
151+
context.subscriptions.push(theia.commands.registerCommand(Commands.SubscribeToOnDidChangeActiveTerminal, () => {
152+
theia.window.onDidChangeActiveTerminal(async (active: theia.Terminal | undefined) => {
153+
if (active) {
154+
const id = await active.processId;
155+
console.log('Active terminal changed: ', id);
156+
}
157+
});
158+
}));
159+
}
160+
161+
function createTerminalWithOptions(): theia.Terminal {
162+
const termOptions: theia.TerminalOptions = {
163+
name: 'Test terminal',
164+
shellPath: '/bin/bash'
165+
}
166+
return theia.window.createTerminal(termOptions);
167+
}
168+
169+
function sleep(milliseconds: number = 0): Promise<void> {
170+
return new Promise(resolve => setTimeout(resolve, milliseconds));;
171+
}
172+
173+
export function stop() {
174+
175+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
"experimentalDecorators": true,
5+
"noUnusedLocals": true,
6+
"emitDecoratorMetadata": true,
7+
"downlevelIteration": true,
8+
"module": "commonjs",
9+
"moduleResolution": "node",
10+
"target": "es5",
11+
"lib": [
12+
"es6",
13+
"webworker"
14+
],
15+
"sourceMap": true,
16+
"rootDir": "src",
17+
"outDir": "lib"
18+
},
19+
"include": [
20+
"src"
21+
]
22+
}

samples/terminal-plugin/tsfmt.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"baseIndentSize": 0,
3+
"newLineCharacter": "\n",
4+
"indentSize": 4,
5+
"tabSize": 4,
6+
"indentStyle": 4,
7+
"convertTabsToSpaces": true,
8+
"insertSpaceAfterCommaDelimiter": true,
9+
"insertSpaceAfterSemicolonInForStatements": true,
10+
"insertSpaceBeforeAndAfterBinaryOperators": true,
11+
"insertSpaceAfterKeywordsInControlFlowStatements": true,
12+
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
13+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
14+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
15+
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
16+
"placeOpenBraceOnNewLineForFunctions": false,
17+
"placeOpenBraceOnNewLineForControlBlocks": false
18+
}

0 commit comments

Comments
 (0)