|  | 
| 14 | 14 |  * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | 
| 15 | 15 |  ********************************************************************************/ | 
| 16 | 16 | 
 | 
| 17 |  | -import { ContainerModule } from 'inversify'; | 
|  | 17 | +import { ContainerModule, inject, injectable } from 'inversify'; | 
| 18 | 18 | import { bindDynamicLabelProvider } from './label/sample-dynamic-label-provider-command-contribution'; | 
| 19 | 19 | import { bindSampleUnclosableView } from './view/sample-unclosable-view-contribution'; | 
| 20 | 20 | 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'; | 
| 21 | 23 | 
 | 
| 22 | 24 | export default new ContainerModule(bind => { | 
| 23 | 25 |     bindDynamicLabelProvider(bind); | 
| 24 | 26 |     bindSampleUnclosableView(bind); | 
| 25 | 27 |     bindSampleOutputChannelWithSeverity(bind); | 
|  | 28 | +    bind(CommandContribution).to(SampleOutputChannelsCommandContribution).inSingletonScope(); | 
| 26 | 29 | }); | 
|  | 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