<commit_before>var bus = require('./event-bus')
var localStore = require('./local-storage')
var TerminalEmulator = require('xterm')
var {clipboard} = require('electron')

TerminalEmulator.loadAddon('fit')
TerminalEmulator.loadAddon('fullscreen')

class PopoutEmulator {
  constructor() {
    this.style = document.createElement('style')
    this.emulator = new TerminalEmulator({cursorBlink: true})

    this.attach()
    this.subscribe()
    this.clearScreen()
  }

  attach() {
    this.style.innerHTML = localStore.remove('popout-emulator:css')
    document.head.appendChild(this.style)

    document.body.style.height = `${window.innerHeight}px`
    document.body.style.fontSize = `${localStore.get('popout-emulator:font-size')}px`
    this.emulator.open(document.body)

    this.emulator.toggleFullscreen(true)
    this.emulator.fit()
  }

  get container() {
    return this.emulator.parent
  }

  subscribe() {
    this.emulator.attachCustomKeydownHandler((e) => {
      if (this.shortcutHandled(e)) {
        e.preventDefault()
        return false
      }
    })

    this.emulator.on('data', (data) => {
      this.sendToTerminal(data)
    })

    bus.on('popout-emulator:write', (text) => {
      this.emulator.write(text)
    })

    window.onresize = () => {
      this.container.style.height = `${window.innerHeight}px`
      this.emulator.fit()
    }
  }

  clearScreen() {
    this.sendToTerminal('
')
  }

  sendToTerminal(data) {
    bus.emit('popout-emulator:data', data)
  }

  shortcutHandled({keyCode, metaKey, shiftKey, ctrlKey}) {
    var wasHandled = false;

    if ((process.platform === 'darwin') && metaKey) {
      wasHandled = this.handleCommandKey(keyCode)
    }

    if ((process.platform !== 'darwin') && ctrlKey) {
      wasHandled = this.handleCtrlKey(keyCode, shiftKey)
    }

    return wasHandled
  }

  handleCommandKey(keyCode) {
    if (keyCode === 187) {
      // cmd-=
      this.increaseFontSize()
      return true
    }

    if (keyCode === 189) {
      // cmd--
      this.decreaseFontSize()
      return true
    }

    if (keyCode === 48) {
      // cmd-0
      this.resetFontSize()
      return true
    }

    if (keyCode === 38) {
      // cmd-up
      this.scrollUp()
      return true
    }

    if (keyCode === 40) {
      // cmd-down
      this.scrollDown()
      return true
    }

    if (keyCode === 67) {
      // cmd-c
      this.clipboardCopy()
      return true
    }

    if (keyCode === 86) {
      // cmd-v
      this.clipboardPaste()
      return true
    }

    return false
  }

  handleCtrlKey(keyCode, shiftKey) {
    if (keyCode === 187) {
      // ctrl-=
      this.increaseFontSize()
      return true
    }

    if (keyCode === 189) {
      // ctrl--
      this.decreaseFontSize()
      return true
    }

    if (keyCode === 48) {
      // ctrl-0
      this.resetFontSize()
      return true
    }

    if (keyCode === 38) {
      // ctrl-up
      this.scrollUp()
      return true
    }

    if (keyCode === 40) {
      // ctrl-down
      this.scrollDown()
      return true
    }

    if (shiftKey && (keyCode === 67)) {
      // ctrl-C
      this.clipboardCopy()
      return true
    }

    if (shiftKey && (keyCode === 86)) {
      // ctrl-V
      this.clipboardPaste()
      return true
    }

    return false
  }

  scrollUp() {
    this.emulator.scrollDisp(-1);
  }

  scrollDown() {
    this.emulator.scrollDisp(1);
  }

  clipboardCopy() {
    var selection = document.getSelection();
    var rawText = selection.toString();
    var preparedText = rawText.replace(/\u00A0/g, ' ').replace(/\s+(\n)?$/gm, '$1');

    clipboard.writeText(preparedText);
  }

  clipboardPaste() {
    var rawText = clipboard.readText();
    var preparedText = rawText.replace(/\n/g, '\r');

    this.sendToTerminal(preparedText)
  }

  fontSize() {<commit_msg>Cleanup shortcut handling in popout emulator<commit_after>var bus = require('./event-bus')
var localStore = require('./local-storage')
var TerminalEmulator = require('xterm')
var {clipboard} = require('electron')

TerminalEmulator.loadAddon('fit')
TerminalEmulator.loadAddon('fullscreen')

class PopoutEmulator {
  constructor() {
    this.style = document.createElement('style')
    this.emulator = new TerminalEmulator({cursorBlink: true})

    this.attach()
    this.subscribe()
    this.clearScreen()
  }

  attach() {
    this.style.innerHTML = localStore.remove('popout-emulator:css')
    document.head.appendChild(this.style)

    document.body.style.height = `${window.innerHeight}px`
    document.body.style.fontSize = `${localStore.get('popout-emulator:font-size')}px`
    this.emulator.open(document.body, true)

    this.emulator.toggleFullscreen(true)
    this.emulator.fit()
  }

  get container() {
    return this.emulator.parent
  }

  subscribe() {
    this.emulator.attachCustomKeydownHandler((e) => {
      var callbackName = this.callbackNameForKeyEvent(e);

      if (callbackName != null) {
        e.preventDefault()
        this[callbackName](e)
        return false
      }
    })

    this.emulator.on('data', (data) => {
      this.sendToTerminal(data)
    })

    bus.on('popout-emulator:write', (text) => {
      this.emulator.write(text)
    })

    window.onresize = () => {
      this.container.style.height = `${window.innerHeight}px`
      this.emulator.fit()
    }
  }

  clearScreen() {
    this.sendToTerminal('
')
  }

  sendToTerminal(data) {
    bus.emit('popout-emulator:data', data)
  }

  callbackNameForKeyEvent({keyCode, metaKey, shiftKey, ctrlKey}) {
    var keyCodeCallbacks = {
      // Mac only
        cmd: {
          187: 'increaseFontSize', // cmd-=
          189: 'decreaseFontSize', // cmd--
          48: 'resetFontSize',     // cmd-0
          38: 'scrollUp',          // cmd-up
          40: 'scrollDown',        // cmd-down
          67: 'clipboardCopy',     // cmd-c
          86: 'clipboardPaste'     // cmd-v
        },
      // Windows & Linux only
        ctrl: {
          187: 'increaseFontSize', // ctrl-=
          189: 'decreaseFontSize', // ctrl--
          48: 'resetFontSize',     // ctrl-0
          38: 'scrollUp',          // ctrl-up
          40: 'scrollDown'         // ctrl-down
        },
        ctrlShift: {
          67: 'clipboardCopy',     // ctrl-C
          86: 'clipboardPaste'     // ctrl-V
        }
      }

    var isMac = process.platform === 'darwin';

    var callbackGroup;
    if (isMac && metaKey) {
      callbackGroup = keyCodeCallbacks.cmd
    } else if (!isMac && ctrlKey) {
      callbackGroup = shiftKey ? keyCodeCallbacks.ctrlShift : keyCodeHandlers.ctrl
    }

    if (!callbackGroup) { return }

    return callbackGroup[keyCode]
  }

  scrollUp() {
    this.emulator.scrollDisp(-1);
  }

  scrollDown() {
    this.emulator.scrollDisp(1);
  }

  clipboardCopy() {
    var selection = document.getSelection();
    var rawText = selection.toString();
    var preparedText = rawText.replace(/\u00A0/g, ' ').replace(/\s+(\n)?$/gm, '$1');

    clipboard.writeText(preparedText);
  }

  clipboardPaste() {
    var rawText = clipboard.readText();
    var preparedText = rawText.replace(/\n/g, '\r');

    this.sendToTerminal(preparedText)
  }

  fontSize() {
{'fraction_non_alphanumeric': 0.1496380846325167, 'fraction_numerical': 0.011135857461024499, 'mean_word_length': 2.3156437471158284, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '4855288', 'n_tokens_mistral': 2467, 'n_tokens_neox': 2335, 'n_words': 400}