<commit_before>const debug = require('debug')('tabtab');
const inquirer = require('inquirer');
const prompt = require('./prompt');
const installer = require('./installer');

const tabtab = () => 'tabtab';

/**
 * Utility to figure out the shell used on the system.
 *
 * Sadly, we can't use `echo $0` in node, maybe with more work. So we rely on process.env.SHELL.
 *
 * TODO: More work on this, namely to detect Git bash on Windows (bash.exe)
 */
tabtab.shell = (env = process.env) => {
  const shell = (env.SHELL || '').split('/').slice(-1)[0];
  return shell;
};

/**
 * Install and enable completion on user system. It'll ask for:
 *
 * - SHELL (bash, zsh or fish)
 * - Path to shell script (with sensible defaults)
 *
 * @param {Object} Options to use with namely `name` and `completer`
 *
 */
tabtab.install = (options = { name: '', completer: '' }) => {
  const { name, completer } = options;
  if (!name) throw new TypeError('options.name is required');
  if (!completer) throw new TypeError('options.completer is required');

  return prompt().then(({ location, shell }) =>
    installer.install({
      name,
      completer,
      location
    })
  );
};

/**
 * Public: Main utility to extract information from command line arguments and
 * Environment variables, namely COMP args in "plumbing" mode.
 *
 * options -  The options hash as parsed by minimist, plus an env property
 *            representing user environment (default: { env: process.env })
 *    :_      - The arguments Array parsed by minimist (positional arguments)
 *    :env    - The environment Object that holds COMP args (default: process.env)
 *
 * Examples
 *
 *   var env = tabtab.parseEnv();
 *   // env:
 *   // args        the positional arguments used
 *   // complete    A Boolean indicating whether we act in "plumbing mode" or not
 *   // words       The Number of words in the completed line
 *   // point       A Number indicating cursor position
 *   // line        The String input line
 *   // partial     The String part of line preceding cursor position
 *   // last        The last String word of the line
 *   // lastPartial The last word String of partial
 *   // prev        The String word preceding last
 *
 * Returns the data env object.
 */
tabtab.parseEnv = (options = {}, env) => {
  const args = options._;

  let cword = Number(env.COMP_CWORD);
  let point = Number(env.COMP_POINT);
  const line = env.COMP_LINE || '';

  if (isNaN(cword)) cword = 0;
  if (isNaN(point)) point = 0;

  const partial = line.slice(0, point);

  const parts = line.split(' ');
  const prev = parts.slice(0, -1).slice(-1)[0];

  const last = parts.slice(-1).join('');
  const lastPartial = partial
    .split(' ')
    .slice(-1)
    .join('');

  let complete = args[0] === 'completion';

  if (
    !env.COMP_CWORD ||
    typeof env.COMP_POINT === 'undefined' ||
    typeof env.COMP_LINE === 'undefined'
  ) {
    complete = false;
  }

  return {
    args,
    complete,
    words: cword,
    point,
    line,
    partial,
    last,
    lastPartial,
    prev
  };
};

/**
 * Main logging utility to pass completion items.
 *
 * This is simply an helper to log to stdout with each item separated by a new
 * line.
 *
 * Bash needs in addition to filter out the args for the completion to work
 * (zsh, fish don't need this).
 *
 * @param {Array} Arguments to log
 * @param {Object} Optional environment object as parsed by tabtab
 **/
tabtab.log = (args, env) => {
  if (!env) {
    env = tabtab.parseEnv({ _: [] }, process.env);
  }

  const shell = tabtab.shell();
  if (shell === 'bash') {
    args = args.filter(arg => arg.indexOf(env.last) === 0);
  }

  for (const arg of args) {
    console.log(`\n${arg}`);
  }

  return {
    args,
    env
  };
};

module.exports = tabtab;
<commit_msg>parseEnv: change signature to only take environment<commit_after>const debug = require('debug')('tabtab');
const inquirer = require('inquirer');
const prompt = require('./prompt');
const installer = require('./installer');

const tabtab = () => 'tabtab';

/**
 * Utility to figure out the shell used on the system.
 *
 * Sadly, we can't use `echo $0` in node, maybe with more work. So we rely on process.env.SHELL.
 *
 * TODO: More work on this, namely to detect Git bash on Windows (bash.exe)
 */
tabtab.shell = (env = process.env) => {
  const shell = (env.SHELL || '').split('/').slice(-1)[0];
  return shell;
};

/**
 * Install and enable completion on user system. It'll ask for:
 *
 * - SHELL (bash, zsh or fish)
 * - Path to shell script (with sensible defaults)
 *
 * @param {Object} Options to use with namely `name` and `completer`
 *
 */
tabtab.install = (options = { name: '', completer: '' }) => {
  const { name, completer } = options;
  if (!name) throw new TypeError('options.name is required');
  if (!completer) throw new TypeError('options.completer is required');

  return prompt().then(({ location, shell }) =>
    installer.install({
      name,
      completer,
      location
    })
  );
};

/**
 * Public: Main utility to extract information from command line arguments and
 * Environment variables, namely COMP args in "plumbing" mode.
 *
 * options -  The options hash as parsed by minimist, plus an env property
 *            representing user environment (default: { env: process.env })
 *    :_      - The arguments Array parsed by minimist (positional arguments)
 *    :env    - The environment Object that holds COMP args (default: process.env)
 *
 * Examples
 *
 *   var env = tabtab.parseEnv();
 *   // env:
 *   // args        the positional arguments used
 *   // complete    A Boolean indicating whether we act in "plumbing mode" or not
 *   // words       The Number of words in the completed line
 *   // point       A Number indicating cursor position
 *   // line        The String input line
 *   // partial     The String part of line preceding cursor position
 *   // last        The last String word of the line
 *   // lastPartial The last word String of partial
 *   // prev        The String word preceding last
 *
 * Returns the data env object.
 */
tabtab.parseEnv = (env, options = { _: [] }) => {
  const args = options._;

  let cword = Number(env.COMP_CWORD);
  let point = Number(env.COMP_POINT);
  const line = env.COMP_LINE || '';

  if (isNaN(cword)) cword = 0;
  if (isNaN(point)) point = 0;

  const partial = line.slice(0, point);

  const parts = line.split(' ');
  const prev = parts.slice(0, -1).slice(-1)[0];

  const last = parts.slice(-1).join('');
  const lastPartial = partial
    .split(' ')
    .slice(-1)
    .join('');

  let complete = args[0] === 'completion';

  if (
    !env.COMP_CWORD ||
    typeof env.COMP_POINT === 'undefined' ||
    typeof env.COMP_LINE === 'undefined'
  ) {
    complete = false;
  }

  return {
    args,
    complete,
    words: cword,
    point,
    line,
    partial,
    last,
    lastPartial,
    prev
  };
};

/**
 * Main logging utility to pass completion items.
 *
 * This is simply an helper to log to stdout with each item separated by a new
 * line.
 *
 * Bash needs in addition to filter out the args for the completion to work
 * (zsh, fish don't need this).
 *
 * @param {Array} Arguments to log
 * @param {Object} Optional environment object as parsed by tabtab
 **/
tabtab.log = (args, env) => {
  if (!env) {
    env = tabtab.parseEnv(process.env);
  }

  const shell = tabtab.shell();
  if (shell === 'bash') {
    args = args.filter(arg => arg.indexOf(env.last) === 0);
  }

  for (const arg of args) {
    console.log(`\n${arg}`);
  }

  return {
    args,
    env
  };
};

module.exports = tabtab;

{'fraction_non_alphanumeric': 0.1369250985545335, 'fraction_numerical': 0.0036793692509855453, 'mean_word_length': 2.782803180914513, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '3770558', 'n_tokens_mistral': 2373, 'n_tokens_neox': 2298, 'n_words': 829}