xterm
[VSoRC/.git] / node_modules / xterm / src / common / services / CoreService.ts
1 /**
2  * Copyright (c) 2019 The xterm.js authors. All rights reserved.
3  * @license MIT
4  */
5
6 import { ICoreService, ILogService, IOptionsService, IBufferService } from 'common/services/Services';
7 import { EventEmitter, IEvent } from 'common/EventEmitter';
8 import { IDecPrivateModes } from 'common/Types';
9 import { clone } from 'common/Clone';
10
11 const DEFAULT_DEC_PRIVATE_MODES: IDecPrivateModes = Object.freeze({
12   applicationCursorKeys: false
13 });
14
15 export class CoreService implements ICoreService {
16   serviceBrand: any;
17
18   public decPrivateModes: IDecPrivateModes;
19
20   private _onData = new EventEmitter<string>();
21   public get onData(): IEvent<string> { return this._onData.event; }
22   private _onUserInput = new EventEmitter<void>();
23   public get onUserInput(): IEvent<void> { return this._onUserInput.event; }
24
25   constructor(
26     // TODO: Move this into a service
27     private readonly _scrollToBottom: () => void,
28     @IBufferService private readonly _bufferService: IBufferService,
29     @ILogService private readonly _logService: ILogService,
30     @IOptionsService private readonly _optionsService: IOptionsService
31   ) {
32     this.decPrivateModes = clone(DEFAULT_DEC_PRIVATE_MODES);
33   }
34
35   public reset(): void {
36     this.decPrivateModes = clone(DEFAULT_DEC_PRIVATE_MODES);
37   }
38
39   public triggerDataEvent(data: string, wasUserInput: boolean = false): void {
40     // Prevents all events to pty process if stdin is disabled
41     if (this._optionsService.options.disableStdin) {
42       return;
43     }
44
45     // Input is being sent to the terminal, the terminal should focus the prompt.
46     const buffer = this._bufferService.buffer;
47     if (buffer.ybase !== buffer.ydisp) {
48       this._scrollToBottom();
49     }
50
51     // Fire onUserInput so listeners can react as well (eg. clear selection)
52     if (wasUserInput) {
53       this._onUserInput.fire();
54     }
55
56     // Fire onData API
57     this._logService.debug(`sending data "${data}"`, () => data.split('').map(e => e.charCodeAt(0)));
58     this._onData.fire(data);
59   }
60 }