<commit_before>/*
 * Copyright 2017 <NAME> <<EMAIL>>
 *
 * This source code is licensed under the MIT license which is detailed in the LICENSE.txt file.
 */
import * as fs from 'fs';
import * as path from 'path';


abstract class ScriptBuilder {
  constructor(protected _extratermCookieName: string, protected _extratermCookieValue: string) {}

  build(): string {
    return this._buildCookie() + '\n' + this._buildCommands() + '\n' + this._buildShellReporting();
  }

  protected abstract _buildCookie(): string;

  protected abstract _buildShellReporting(): string;

  protected _buildCommands(): string {
    return this._buildScriptForCommand('from', 'exfrom') + this._buildScriptForCommand('show', 'exshow');
  }
    
  protected _buildScriptForCommand(commandName: string, commandPyFile: string): string {
    const flattener = new PythonFileFlattener(path.join(__dirname, '../../../src/commands/'));
    const script = flattener.readAndInlineCommand(commandPyFile);
    return this._formatCommand(commandName, script);
  }

  protected abstract _formatCommand(commandName: string, commandSource: string): string;

  protected _escapeShellChars(source): string {
    return source.replace(/\\/g,'\\\\').replace(/'/g,"\\'");
  }
}

export class FishScriptBuilder extends ScriptBuilder {

  protected _buildCookie(): string {
    return `set -x ${this._extratermCookieName} ${this._extratermCookieValue}`;
  }

  protected _buildShellReporting(): string {
    return `
function extraterm_preexec -e fish_preexec
  echo -n -e -s "\\033&" $${this._extratermCookieName} ";2;fish\\007"
  echo -n $argv[1]
  echo -n -e "\\000"<commit_msg>Inject fish shell commands with help of the source command<commit_after>/*
 * Copyright 2017 <NAME> <<EMAIL>>
 *
 * This source code is licensed under the MIT license which is detailed in the LICENSE.txt file.
 */
import * as fs from 'fs';
import * as path from 'path';


abstract class ScriptBuilder {
  constructor(protected _extratermCookieName: string, protected _extratermCookieValue: string) {}

  build(): string {
    return this._buildCookie() + '\n' + this._buildCommands() + '\n' + this._buildShellReporting();
  }

  protected abstract _buildCookie(): string;

  protected abstract _buildShellReporting(): string;

  protected _buildCommands(): string {
    return this._buildScriptForCommand('from', 'exfrom') + this._buildScriptForCommand('show', 'exshow');
  }
    
  protected _buildScriptForCommand(commandName: string, commandPyFile: string): string {
    const flattener = new PythonFileFlattener(path.join(__dirname, '../../../src/commands/'));
    const script = flattener.readAndInlineCommand(commandPyFile);
    return this._formatCommand(commandName, script);
  }

  protected abstract _formatCommand(commandName: string, commandSource: string): string;

  protected _escapeShellChars(source): string {
    return source.replace(/\\/g,'\\\\').replace(/'/g,"\\'");
  }
}

const EOT = '\x04';

export class FishScriptBuilder extends ScriptBuilder {

  build(): string {
    return `source
${super.build()}
${EOT}`;
  }

  protected _buildCookie(): string {
    return `set -x ${this._extratermCookieName} ${this._extratermCookieValue}`;
  }

  protected _buildShellReporting(): string {
    return `
function extraterm_preexec -e fish_preexec
  echo -n -e -s "\\033&" $${this._extratermCookieName} ";2;fish\\007"
  echo -n $argv[1]
  echo -n -e "\\000"
{'fraction_non_alphanumeric': 0.1365767878077374, 'fraction_numerical': 0.009378663540445486, 'mean_word_length': 4.383280757097792, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '521198', 'n_tokens_mistral': 1042, 'n_tokens_neox': 1000, 'n_words': 273}