|
| 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 | +} |
0 commit comments