<commit_before>/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import DOM = require('vs/base/browser/dom');
import lifecycle = require('vs/base/common/lifecycle');
import nls = require('vs/nls');
import os = require('os');
import xterm = require('xterm');
import {Dimension} from 'vs/base/browser/builder';
import {IContextMenuService} from 'vs/platform/contextview/browser/contextView';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {IKeybindingService, IKeybindingContextKey} from 'vs/platform/keybinding/common/keybinding';
import {IMessageService, Severity} from 'vs/platform/message/common/message';
import {ITerminalFont} from 'vs/workbench/parts/terminal/electron-browser/terminalConfigHelper';
import {ITerminalProcess, ITerminalService} from 'vs/workbench/parts/terminal/electron-browser/terminal';
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
import {Keybinding} from 'vs/base/common/keyCodes';
import {TabFocus} from 'vs/editor/common/config/commonEditorConfig';
import {ToggleTabFocusModeAction} from 'vs/editor/contrib/toggleTabFocusMode/common/toggleTabFocusMode';

export class TerminalInstance {

	private static eolRegex = /\r?\n/g;

	private isExiting: boolean = false;

	private toDispose: lifecycle.IDisposable[];
	private xterm;
	private terminalDomElement: HTMLDivElement;
	private wrapperElement: HTMLDivElement;
	private font: ITerminalFont;

	public constructor(
		private terminalProcess: ITerminalProcess,
		private parentDomElement: HTMLElement,
		private contextMenuService: IContextMenuService,
		private contextService: IWorkspaceContextService,
		private instantiationService: IInstantiationService,
		private keybindingService: IKeybindingService,
		private terminalService: ITerminalService,
		private messageService: IMessageService,
		private terminalFocusContextKey: IKeybindingContextKey<boolean>,
		private onExitCallback: (TerminalInstance) => void
	) {
		let self = this;
		this.toDispose = [];
		this.wrapperElement = document.createElement('div');
		DOM.addClass(this.wrapperElement, 'terminal-wrapper');
		this.terminalDomElement = document.createElement('div');
		this.xterm = xterm();

		this.terminalProcess.process.on('message', (message) => {
			if (message.type === 'data') {
				this.xterm.write(message.content);
			}
		});
		this.xterm.on('data', (data) => {
			this.terminalProcess.process.send({
				event: 'input',
				data: this.sanitizeInput(data)
			});
			return false;
		});
		this.xterm.attachCustomKeydownHandler(function (event: KeyboardEvent) {
			// HACK: Only listen to the tab focus mode keybinding if it's the default since
			// there doesn't seem to be a convenient way to convert vscode keycodes to regular
			// DOM keycodes
			const opts: Keybinding[] = self.keybindingService.lookupKeybindings(ToggleTabFocusModeAction.ID);
			if (opts.length > 0) {
				const opt = opts[0];
				if (opt.hasCtrlCmd() && !opt.hasAlt() && !opt.hasShift() && opts[0].extractKeyCode() === 43) {
					if (event.ctrlKey && event.keyCode === 77) {
						event.preventDefault();
						return false;
					}
				}
			}

			// If tab focus mode is on, tab is not passed to the terminal
			if (TabFocus.getTabFocusMode() && event.keyCode === 9) {
				return false;
			}
		});
		this.terminalProcess.process.on('exit', (exitCode) => {
			// Prevent dispose functions being triggered multiple times<commit_msg>Dynamically listen to tab focus mode keybindings<commit_after>/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import DOM = require('vs/base/browser/dom');
import lifecycle = require('vs/base/common/lifecycle');
import nls = require('vs/nls');
import os = require('os');
import xterm = require('xterm');
import {Dimension} from 'vs/base/browser/builder';
import {IContextMenuService} from 'vs/platform/contextview/browser/contextView';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {IKeybindingService, IKeybindingContextKey} from 'vs/platform/keybinding/common/keybinding';
import {IMessageService, Severity} from 'vs/platform/message/common/message';
import {ITerminalFont} from 'vs/workbench/parts/terminal/electron-browser/terminalConfigHelper';
import {ITerminalProcess, ITerminalService} from 'vs/workbench/parts/terminal/electron-browser/terminal';
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
import {Keybinding} from 'vs/base/common/keyCodes';
import {StandardKeyboardEvent} from 'vs/base/browser/keyboardEvent';
import {TabFocus} from 'vs/editor/common/config/commonEditorConfig';
import {ToggleTabFocusModeAction} from 'vs/editor/contrib/toggleTabFocusMode/common/toggleTabFocusMode';

export class TerminalInstance {

	private static eolRegex = /\r?\n/g;

	private isExiting: boolean = false;

	private toDispose: lifecycle.IDisposable[];
	private xterm;
	private terminalDomElement: HTMLDivElement;
	private wrapperElement: HTMLDivElement;
	private font: ITerminalFont;
	private toggleTabFocusModeKeybindings: Keybinding[];

	public constructor(
		private terminalProcess: ITerminalProcess,
		private parentDomElement: HTMLElement,
		private contextMenuService: IContextMenuService,
		private contextService: IWorkspaceContextService,
		private instantiationService: IInstantiationService,
		private keybindingService: IKeybindingService,
		private terminalService: ITerminalService,
		private messageService: IMessageService,
		private terminalFocusContextKey: IKeybindingContextKey<boolean>,
		private onExitCallback: (TerminalInstance) => void
	) {
		let self = this;
		this.toDispose = [];
		this.toggleTabFocusModeKeybindings = self.keybindingService.lookupKeybindings(ToggleTabFocusModeAction.ID);
		this.wrapperElement = document.createElement('div');
		DOM.addClass(this.wrapperElement, 'terminal-wrapper');
		this.terminalDomElement = document.createElement('div');
		this.xterm = xterm();

		this.terminalProcess.process.on('message', (message) => {
			if (message.type === 'data') {
				this.xterm.write(message.content);
			}
		});
		this.xterm.on('data', (data) => {
			this.terminalProcess.process.send({
				event: 'input',
				data: this.sanitizeInput(data)
			});
			return false;
		});
		this.xterm.attachCustomKeydownHandler(function (event: KeyboardEvent) {
			// Allow the toggle tab mode keybinding to pass through the terminal so that focus can
			// be escaped
			let standardKeyboardEvent = new StandardKeyboardEvent(event);
			if (self.toggleTabFocusModeKeybindings.some((k) => standardKeyboardEvent.equals(k.value))) {
				event.preventDefault();
				return false;
			}

			// If tab focus mode is on, tab is not passed to the terminal
			if (TabFocus.getTabFocusMode() && event.keyCode === 9) {
				return false;
			}
		});
		this.terminalProcess.process.on('exit', (exitCode) => {
			// Prevent dispose functions being triggered multiple times
{'fraction_non_alphanumeric': 0.15877029544849613, 'fraction_numerical': 0.0011977641735427202, 'mean_word_length': 4.01, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1373777', 'n_tokens_mistral': 2256, 'n_tokens_neox': 2059, 'n_words': 494}