<commit_before>/*
 Copyright 2017 <NAME>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

const View = require('../base/View');
const TextAnimation = require('./TextAnimation');
const textTools = require('../../TextTools');

const elementCreator = require('../../ElementCreator');

class Terminal extends View {
  constructor() {
    super({ isFullscreen: true, viewId: 'terminal' });
    this.element.classList.add('fullHeight');

    this.terminalInput = elementCreator.createInput({ inputName: 'terminalInput', placeholder: 'Input command', classes: ['terminalInput'] });
    this.element.appendChild(elementCreator.createList({ elementId: 'terminalFeed' }));
    this.element.appendChild(this.terminalInput);
    this.queue = [];
    this.printing = false;
    this.shortQueue = [];
    this.timeout = 50;
    this.firstRun = true;
    this.commands = [];
  }

  addCommand(command) {
    this.commands.push(command);
  }

  clearInput() {
    this.terminalInput.value = '';
  }

  autoCompleteCommand() {
    const commands = this.getCommandNames();
    const matched = [];
    const inputValue = this.terminalInput.value.toLowerCase();
    let matches;

    commands.forEach((commandName) => {
      const lowerCommand = commandName.toLowerCase();
      matches = false;

      for (let j = 0; j < inputValue.length; j += 1) {
        if (inputValue.charAt(j) === lowerCommand.charAt(j)) {
          matches = true;
        } else {
          matches = false;

          break;
        }
      }

      if (matches) {
        matched.push(commandName);
      }
    });

    if (matched.length === 1) {
      this.terminalInput.value = matched[0];
    } else if (matched.length > 0) {
      this.queueMessage({
        message: {
          text: ['$ Multiple matched commands:'],
          elements: matched.map((commandName) => {
            return elementCreator.createSpan({
              text: commandName,
              classes: ['clickable'],
              func: () => {
                this.triggerCommand(commandName);
              },
            });
          }),
        },
      });
    }
  }

  triggerCommand(value) {
    const inputValue = value || this.terminalInput.value;

    if (inputValue === '') {
      this.queueMessage({ message: { text: ['$', 'Commands:'], elements: this.getClickableCommandNames() } });
    } else {
      const sentCommandName = textTools.trimSpace(inputValue.toLowerCase());
      const command = this.commands.find(({ commandName }) => sentCommandName === commandName.toLowerCase());

      if (command) {
        this.queueMessage({
          message: {
            text: [
              `$ ${inputValue}`,
              `Running command ${command.commandName}:`,
            ],
          },
        });
        command.startFunc();
      } else {
        this.queueMessage({ message: { text: [`$ ${inputValue}: command not found`, 'Commands:'], elements: this.getClickableCommandNames() } });
      }
    }

    this.clearInput();
  }

  addRow(message) {
    const currentText = message.text;

    if (currentText && currentText.length > 0) {
      const text = currentText.shift();
      const row = elementCreator.createListItem({ element: elementCreator.createSpan({ text, classes: message.classes }) });

      this.element.firstElementChild.appendChild(row);
      row.scrollIntoView();
      setTimeout(() => { this.addRow(message); }, this.timeout);
    } else {
      if (message.elements) {
        this.appendRow({ elements: message.elements });
      }
      this.consumeShortQueue();
    }
  }

  consumeShortQueue() {
    if (this.shortQueue.length > 0) {
      const message = this.shortQueue.shift();

      this.addRow(message);
    } else {
      this.printing = false;
      this.consumeQueue();
    }
  }

  consumeQueue() {
    if (this.element.parentNode && !this.printing && this.queue.length > 0) {
      this.shortQueue = this.queue.splice(0, 10);
      this.printing = true;
      this.consumeShortQueue();
    }
  }

  queueMessage({ message }) {
    this.queue.push(message);
    this.consumeQueue(this.queue);
  }

  appendTo(parentElement) {
    if (!this.firstRun) {
      super.appendTo(parentElement);
      this.enableKeyTriggers();
      this.terminalInput.focus();
      this.consumeQueue();
    } else {
      this.addKeyTrigger({
        charCode: 13, // Enter
        triggerless: true,
        func: () => {
          this.triggerCommand();
        },
      });
      this.addKeyTrigger({
        charCode: 9, // Tab
        triggerless: true,
        func: () => {
          this.autoCompleteCommand();
        },
      });
      this.startBootSequence(parentElement);
    }
  }

  removeView() {
    this.disableKeyTriggers();
    super.removeView();
  }

  getCommandNames() {
    return this.commands.map(({ commandName}) => commandName);
  }

  getClickableCommandNames() {
    return this.commands.map(({ commandName }) => {
      return elementCreator.createSpan({
        text: commandName,
        classes: ['clickable'],
        func: () => {
          this.triggerCommand(commandName);
        },
      });
    });
  }

  appendRow({ elements }) {
    const listItem = elementCreator.createListItem({});

    elements.forEach(element => listItem.appendChild(element));

    this.element.firstElementChild.appendChild(listItem);
    listItem.scrollIntoView();
  }<commit_msg>Added missing whitespace<commit_after>/*
 Copyright 2017 <NAME>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

const View = require('../base/View');
const TextAnimation = require('./TextAnimation');
const textTools = require('../../TextTools');

const elementCreator = require('../../ElementCreator');

class Terminal extends View {
  constructor() {
    super({ isFullscreen: true, viewId: 'terminal' });
    this.element.classList.add('fullHeight');

    this.terminalInput = elementCreator.createInput({ inputName: 'terminalInput', placeholder: 'Input command', classes: ['terminalInput'] });
    this.element.appendChild(elementCreator.createList({ elementId: 'terminalFeed' }));
    this.element.appendChild(this.terminalInput);
    this.queue = [];
    this.printing = false;
    this.shortQueue = [];
    this.timeout = 50;
    this.firstRun = true;
    this.commands = [];
  }

  addCommand(command) {
    this.commands.push(command);
  }

  clearInput() {
    this.terminalInput.value = '';
  }

  autoCompleteCommand() {
    const commands = this.getCommandNames();
    const matched = [];
    const inputValue = this.terminalInput.value.toLowerCase();
    let matches;

    commands.forEach((commandName) => {
      const lowerCommand = commandName.toLowerCase();
      matches = false;

      for (let j = 0; j < inputValue.length; j += 1) {
        if (inputValue.charAt(j) === lowerCommand.charAt(j)) {
          matches = true;
        } else {
          matches = false;

          break;
        }
      }

      if (matches) {
        matched.push(commandName);
      }
    });

    if (matched.length === 1) {
      this.terminalInput.value = matched[0];
    } else if (matched.length > 0) {
      this.queueMessage({
        message: {
          text: ['$ Multiple matched commands:'],
          elements: matched.map((commandName) => {
            return elementCreator.createSpan({
              text: commandName,
              classes: ['clickable'],
              func: () => {
                this.triggerCommand(commandName);
              },
            });
          }),
        },
      });
    }
  }

  triggerCommand(value) {
    const inputValue = value || this.terminalInput.value;

    if (inputValue === '') {
      this.queueMessage({ message: { text: ['$', 'Commands:'], elements: this.getClickableCommandNames() } });
    } else {
      const sentCommandName = textTools.trimSpace(inputValue.toLowerCase());
      const command = this.commands.find(({ commandName }) => sentCommandName === commandName.toLowerCase());

      if (command) {
        this.queueMessage({
          message: {
            text: [
              `$ ${inputValue}`,
              `Running command ${command.commandName}:`,
            ],
          },
        });
        command.startFunc();
      } else {
        this.queueMessage({ message: { text: [`$ ${inputValue}: command not found`, 'Commands:'], elements: this.getClickableCommandNames() } });
      }
    }

    this.clearInput();
  }

  addRow(message) {
    const currentText = message.text;

    if (currentText && currentText.length > 0) {
      const text = currentText.shift();
      const row = elementCreator.createListItem({ element: elementCreator.createSpan({ text, classes: message.classes }) });

      this.element.firstElementChild.appendChild(row);
      row.scrollIntoView();
      setTimeout(() => { this.addRow(message); }, this.timeout);
    } else {
      if (message.elements) {
        this.appendRow({ elements: message.elements });
      }
      this.consumeShortQueue();
    }
  }

  consumeShortQueue() {
    if (this.shortQueue.length > 0) {
      const message = this.shortQueue.shift();

      this.addRow(message);
    } else {
      this.printing = false;
      this.consumeQueue();
    }
  }

  consumeQueue() {
    if (this.element.parentNode && !this.printing && this.queue.length > 0) {
      this.shortQueue = this.queue.splice(0, 10);
      this.printing = true;
      this.consumeShortQueue();
    }
  }

  queueMessage({ message }) {
    this.queue.push(message);
    this.consumeQueue(this.queue);
  }

  appendTo(parentElement) {
    if (!this.firstRun) {
      super.appendTo(parentElement);
      this.enableKeyTriggers();
      this.terminalInput.focus();
      this.consumeQueue();
    } else {
      this.addKeyTrigger({
        charCode: 13, // Enter
        triggerless: true,
        func: () => {
          this.triggerCommand();
        },
      });
      this.addKeyTrigger({
        charCode: 9, // Tab
        triggerless: true,
        func: () => {
          this.autoCompleteCommand();
        },
      });
      this.startBootSequence(parentElement);
    }
  }

  removeView() {
    this.disableKeyTriggers();
    super.removeView();
  }

  getCommandNames() {
    return this.commands.map(({ commandName }) => commandName);
  }

  getClickableCommandNames() {
    return this.commands.map(({ commandName }) => {
      return elementCreator.createSpan({
        text: commandName,
        classes: ['clickable'],
        func: () => {
          this.triggerCommand(commandName);
        },
      });
    });
  }

  appendRow({ elements }) {
    const listItem = elementCreator.createListItem({});

    elements.forEach(element => listItem.appendChild(element));

    this.element.firstElementChild.appendChild(listItem);
    listItem.scrollIntoView();
  }
{'fraction_non_alphanumeric': 0.13932890478623744, 'fraction_numerical': 0.004087889626980072, 'mean_word_length': 2.5032816229116945, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1678927', 'n_tokens_mistral': 3348, 'n_tokens_neox': 3256, 'n_words': 704}