From 72fd6c1c93123c2e146e54c3dfdf97a8a5b0c596 Mon Sep 17 00:00:00 2001 From: Akos Kitta Date: Wed, 27 May 2020 13:42:05 +0200 Subject: [PATCH] [drop-me]: Added commands to populate Output. Signed-off-by: Akos Kitta --- .../browser/api-samples-frontend-module.ts | 69 ++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/examples/api-samples/src/browser/api-samples-frontend-module.ts b/examples/api-samples/src/browser/api-samples-frontend-module.ts index 2fac6d83ffaeb..4ae631c5a3aa3 100644 --- a/examples/api-samples/src/browser/api-samples-frontend-module.ts +++ b/examples/api-samples/src/browser/api-samples-frontend-module.ts @@ -14,13 +14,80 @@ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ -import { ContainerModule } from 'inversify'; +import { ContainerModule, inject, injectable } from 'inversify'; import { bindDynamicLabelProvider } from './label/sample-dynamic-label-provider-command-contribution'; import { bindSampleUnclosableView } from './view/sample-unclosable-view-contribution'; import { bindSampleOutputChannelWithSeverity } from './output/sample-output-channel-with-severity'; +import { CommandRegistry, CommandContribution, Disposable, DisposableCollection } from '@theia/core'; +import { OutputChannelManager, OutputChannel, OutputChannelSeverity } from '@theia/output/lib/common/output-channel'; export default new ContainerModule(bind => { bindDynamicLabelProvider(bind); bindSampleUnclosableView(bind); bindSampleOutputChannelWithSeverity(bind); + bind(CommandContribution).to(SampleOutputChannelsCommandContribution).inSingletonScope(); }); + +@injectable() +class SampleOutputChannelsCommandContribution implements CommandContribution { + + @inject(OutputChannelManager) + private readonly ocm: OutputChannelManager; + + private toDispose = new Map(); + + registerCommands(commands: CommandRegistry): void { + this.ocm.onChannelDeleted(({ name }) => { + const toDisposePerChannel = this.toDispose.get(name); + if (toDisposePerChannel) { + toDisposePerChannel.dispose(); + } + }); + for (const channelName of ['one', 'two', 'three']) { + const startCommand = { id: `post-date-now-${channelName}`, label: `API Sample: Post Date.now() to the '${channelName}' channel.` }; + commands.registerCommand(startCommand, { + execute: () => { + const channel = this.getChannel(channelName); + channel.setVisibility(true); + const timer = window.setInterval(() => this.appendLineTo(channelName, Date.now()), 200); + this.toDispose.set(channelName, new DisposableCollection( + Disposable.create(() => this.toDispose.delete(channelName)), + Disposable.create(() => window.clearInterval(timer)) + )); + }, + isEnabled: () => !this.toDispose.has(channelName), + isVisible: () => !this.toDispose.has(channelName) + }); + const stopCommand = { id: `stop-date-now-${channelName}`, label: `API Sample: Stop Date.now() on '${channelName}' channel.` }; + commands.registerCommand(stopCommand, { + execute: () => { + this.appendLineTo(channelName, 'User abort.'); + this.toDispose.get(channelName)!.dispose(); + }, + isEnabled: () => this.toDispose.has(channelName), + isVisible: () => this.toDispose.has(channelName) + }); + } + } + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + private appendLineTo(channelName: string, what: any): void { + const severity = Math.floor(Math.random() * 3) + 1; + if (severity === OutputChannelSeverity.Warning) { + what = what + ' [WARNING]'; + } else if (severity === OutputChannelSeverity.Error) { + what = what + ' [ERROR]'; + } + this.getChannel(channelName).appendLine(`[${channelName}]: ${what}`, severity); + } + + private getChannel(channelName: string): OutputChannel { + const channel = this.ocm.getChannel(channelName); + if (channel) { + return channel; + } else { + throw new Error(`Ouch. No channel was found with name: '${channelName}'.`); + } + } + +}