Skip to content

Commit

Permalink
[drop-me]: Added commands to populate Output.
Browse files Browse the repository at this point in the history
Signed-off-by: Akos Kitta <[email protected]>
  • Loading branch information
Akos Kitta committed May 27, 2020
1 parent 262fb06 commit 72fd6c1
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion examples/api-samples/src/browser/api-samples-frontend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Disposable>();

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}'.`);
}
}

}

0 comments on commit 72fd6c1

Please sign in to comment.