<commit_before>#!/usr/bin/env node

import open from 'open';

const ALL_ARGS = 0;
const ALL_EXCEPT_FIRST_ARG = 1;

const searchEngines = {
  google: {
    url: 'https://www.google.com/search?q=',
    args: ALL_ARGS,
    shortHand: 'default',
  },
  twitter: {
    url: 'https://twitter.com/search?src=typd&q=%23',
    args: ALL_EXCEPT_FIRST_ARG,
    shortHand: 't',
  },
  reddit: {
    url: 'https://www.reddit.com/search?q=',
    args: ALL_EXCEPT_FIRST_ARG,
    shortHand: 'r',
  },
  stackoverflow: {
    url: 'http://stackoverflow.com/search?q=',
    args: ALL_EXCEPT_FIRST_ARG,
    shortHand: 's',
  },
  leo: {
    url: 'http://dict.leo.org/ende/index_de.html#/search=',
    args: ALL_EXCEPT_FIRST_ARG,
    shortHand: 'l',
  },
};

/**
 * list all available providers
 * @param  {String} space the space in front of the output
 * @return {void}
 */
function listProviders(space) {
  Object.keys(searchEngines)
    .map(provider => (
        searchEngines[provider].shortHand === 'default'
        ? `${space}${provider} (default)`
        : `${space}${provider}: -${searchEngines[provider].shortHand}/--${provider}`
      ))
    .forEach(line => console.log(line));
}

/**
 * prints the usage and optional an error
 * @param  {string} err the error to print
 * @return {void}
 */
function printUsage(err) {
  if (err) {
    console.error('ERROR:', err);
  }
  console.log('USAGE: web-s [provider] <searchstring>');
  console.log('Available providers:');
  listProviders('  ');

  // return with exit status 1 if an error occured
  if (err) {
    process.exit(1);
  }
}

/**
 * joins the arguments dependent on the provider and opens the default webbrowser
 * @param  {object} provider
 * @param  {array} args the cli arguments
 * @return {void}
 */
function openBrowser(provider, args) {
  let url = 0;

  if (provider.args === ALL_ARGS) {
    url = provider.url + args.join(' ');
  } else if (provider.args === ALL_EXCEPT_FIRST_ARG) {
    url = provider.url + args.slice(1).join(' ');
  }

  if (url) {
    open(url);
  }
}

/**
 * Parses the arguments and invokes the needed functions
 * @param  {Array} args the arguments
 * @return {void}
 */
function parseArguments(args) {
  switch (args[0]) {
    case '--list': listProviders(''); break;
    case '-h':
    case '--help': printUsage(); break;
    case '-l':
    case '--leo': openBrowser(searchEngines.leo, args); break;
    case '-r':
    case '--reddit': openBrowser(searchEngines.reddit, args); break;
    case '-s':
    case '--stackoverflow': openBrowser(searchEngines.stackoverflow, args); break;
    case '-t':
    case '--twitter': openBrowser(searchEngines.twitter, args); break;
    default: openBrowser(searchEngines.google, args);
  }
}

if (process.argv.length >= 3) {
  parseArguments(process.argv.slice(2));
} else {
  printUsage('No searchstring');
}<commit_msg>[TASK] Add a config file for providers<commit_after>#!/usr/bin/env node

import open from 'open';
import fs from 'fs';

const ALL_ARGS = 0;
const ALL_EXCEPT_FIRST_ARG = 1;
const CONFIG_FILE = `${process.env.HOME}/.web-s.conf`;

const sampleConfig = {
  google: {
    url: 'https://www.google.com/search?q=',
    args: ALL_ARGS,
    shortHand: 'default',
  },
  twitter: {
    url: 'https://twitter.com/search?src=typd&q=%23',
    args: ALL_EXCEPT_FIRST_ARG,
    shortHand: 't',
  },
  reddit: {
    url: 'https://www.reddit.com/search?q=',
    args: ALL_EXCEPT_FIRST_ARG,
    shortHand: 'r',
  },
  stackoverflow: {
    url: 'http://stackoverflow.com/search?q=',
    args: ALL_EXCEPT_FIRST_ARG,
    shortHand: 's',
  },
  leo: {
    url: 'http://dict.leo.org/ende/index_de.html#/search=',
    args: ALL_EXCEPT_FIRST_ARG,
    shortHand: 'l',
  },
};

/**
 * list all available providers
 * @param  {String} space the space in front of the output
 * @return {void}
 */
function listProviders(space) {
  Object.keys(searchEngines)
    .map(provider => (
        searchEngines[provider].shortHand === 'default'
        ? `${space}${provider} (default)`
        : `${space}${provider}: -${searchEngines[provider].shortHand}/--${provider}`
      ))
    .forEach(line => console.log(line));
}

/**
 * prints the usage and optional an error
 * @param  {string} err the error to print
 * @return {void}
 */
function printUsage(err) {
  if (err) {
    console.error('ERROR:', err);
  }
  console.log('USAGE: web-s [provider] <searchstring>');
  console.log('Available providers:');
  listProviders('  ');
}

/**
 * joins the arguments dependent on the provider and opens the default webbrowser
 * @param  {object} provider
 * @param  {array} args the cli arguments
 * @return {true}
 */
function openBrowser(provider, args) {
  let url = 0;

  if (provider.args === ALL_ARGS) {
    url = provider.url + args.join(' ');
  } else if (provider.args === ALL_EXCEPT_FIRST_ARG) {
    url = provider.url + args.slice(1).join(' ');
  }

  if (url) {
    open(url);
  }

  return true;
}

/**
 * Parses the arguments and invokes the needed functions
 * @param  {Array} args the arguments
 * @return {void}
 */
function parseArguments(args) {
  switch (args[0]) {
    case '--generate-config': break;
    case '--list': listProviders(''); break;
    case '-h':
    case '--help': printUsage(); break;
    default: detetermineProvider(args);
  }
}

function detetermineProvider(args) {
  let opened = false;

  Object.keys(searchEngines).forEach((provider) => {
    console.log(provider);
    if (`-${searchEngines[provider].shortHand}` === args[0] || `--${provider}` === args[0]) {
      opened = openBrowser(searchEngines[provider], args);
    }
  });

  if (!opened) {
    openBrowser(searchEngines.google, args)
  }
}

/**
 * checks if a config exists
 * @return {Boolean}
 */
function configExist() {
  let fileExist = false
  try {
    fileExist = fs.statSync(CONFIG_FILE).isFile();
  } finally {
    return fileExist;
  }
}

/**
 * creates a sample config
 * @return {void}
 */
function createSampleConfig() {
  const config = JSON.stringify(sampleConfig, null, 2);
  fs.writeFileSync(CONFIG_FILE, config);
}

/**
 * reads the config file
 * @return {Object}
 */
function readConfig() {
  return JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
}

if (!configExist()) {
  createSampleConfig();
}

const searchEngines = readConfig();
if (process.argv.length >= 3) {
  parseArguments(process.argv.slice(2));
} else {
  printUsage('No searchstring');
}
{'fraction_non_alphanumeric': 0.1692018190371648, 'fraction_numerical': 0.0037635251685745647, 'mean_word_length': 3.435326842837274, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 6, 'lorem ipsum': 0, 'www.': 4, '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': '6147203', 'n_tokens_mistral': 2213, 'n_tokens_neox': 2169, 'n_words': 506}