Skip to content

Commit a94e465

Browse files
author
Akos Kitta
committed
[drop-me]: Added commands to populate Output.
Signed-off-by: Akos Kitta <[email protected]>
1 parent 5591cad commit a94e465

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

examples/api-samples/src/browser/api-samples-frontend-module.ts

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,80 @@
1414
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
1515
********************************************************************************/
1616

17-
import { ContainerModule } from 'inversify';
17+
import { ContainerModule, inject, injectable } from 'inversify';
1818
import { bindDynamicLabelProvider } from './label/sample-dynamic-label-provider-command-contribution';
1919
import { bindSampleUnclosableView } from './view/sample-unclosable-view-contribution';
2020
import { bindSampleOutputChannelWithSeverity } from './output/sample-output-channel-with-severity';
21+
import { CommandRegistry, CommandContribution, Disposable, DisposableCollection } from '@theia/core';
22+
import { OutputChannelManager, OutputChannel, OutputChannelSeverity } from '@theia/output/lib/common/output-channel';
2123

2224
export default new ContainerModule(bind => {
2325
bindDynamicLabelProvider(bind);
2426
bindSampleUnclosableView(bind);
2527
bindSampleOutputChannelWithSeverity(bind);
28+
bind(CommandContribution).to(SampleOutputChannelsCommandContribution).inSingletonScope();
2629
});
30+
31+
@injectable()
32+
class SampleOutputChannelsCommandContribution implements CommandContribution {
33+
34+
@inject(OutputChannelManager)
35+
private readonly ocm: OutputChannelManager;
36+
37+
private toDispose = new Map<string, Disposable>();
38+
39+
registerCommands(commands: CommandRegistry): void {
40+
this.ocm.onChannelDeleted(({ name }) => {
41+
const toDisposePerChannel = this.toDispose.get(name);
42+
if (toDisposePerChannel) {
43+
toDisposePerChannel.dispose();
44+
}
45+
});
46+
for (const channelName of ['one', 'two', 'three']) {
47+
const startCommand = { id: `post-date-now-${channelName}`, label: `API Sample: Post Date.now() to the '${channelName}' channel.` };
48+
commands.registerCommand(startCommand, {
49+
execute: () => {
50+
const channel = this.getChannel(channelName);
51+
channel.setVisibility(true);
52+
const timer = window.setInterval(() => this.appendLineTo(channelName, Date.now()), 200);
53+
this.toDispose.set(channelName, new DisposableCollection(
54+
Disposable.create(() => this.toDispose.delete(channelName)),
55+
Disposable.create(() => window.clearInterval(timer))
56+
));
57+
},
58+
isEnabled: () => !this.toDispose.has(channelName),
59+
isVisible: () => !this.toDispose.has(channelName)
60+
});
61+
const stopCommand = { id: `stop-date-now-${channelName}`, label: `API Sample: Stop Date.now() on '${channelName}' channel.` };
62+
commands.registerCommand(stopCommand, {
63+
execute: () => {
64+
this.appendLineTo(channelName, 'User abort.');
65+
this.toDispose.get(channelName)!.dispose();
66+
},
67+
isEnabled: () => this.toDispose.has(channelName),
68+
isVisible: () => this.toDispose.has(channelName)
69+
});
70+
}
71+
}
72+
73+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
74+
private appendLineTo(channelName: string, what: any): void {
75+
const severity = Math.floor(Math.random() * 3) + 1;
76+
if (severity === OutputChannelSeverity.Warning) {
77+
what = what + ' [WARNING]';
78+
} else if (severity === OutputChannelSeverity.Error) {
79+
what = what + ' [ERROR]';
80+
}
81+
this.getChannel(channelName).appendLine(`[${channelName}]: ${what}`, severity);
82+
}
83+
84+
private getChannel(channelName: string): OutputChannel {
85+
const channel = this.ocm.getChannel(channelName);
86+
if (channel) {
87+
return channel;
88+
} else {
89+
throw new Error(`Ouch. No channel was found with name: '${channelName}'.`);
90+
}
91+
}
92+
93+
}

0 commit comments

Comments
 (0)