<commit_before>import {app, globalShortcut, clipboard} from 'electron';
import fs from 'fs';
import menubar from 'menubar';
import setting from '../setting.json';
import googleTranslate from './google-translate';
import glosbeTranslate from './glosbe-translate';

const dir = process.cwd();
const logPath = `${dir}/log.txt`;
const indexPath = `file://${dir}/app/view/popup.html`;
const SERVICE = {
  GOOGLE: 'google',
  GLOSBE: 'glosbe'
};

const mb = menubar({index: indexPath});

function saveContents() {
  const clip = clipboard.readText();
  console.log(clip);

  if (setting.enableServiceHook) {
    let service;
    switch (setting.service) {
    case SERVICE.GOOGLE:
      service = googleTranslate;
      break;
    case SERVICE.GLOSBE:
      service = glosbeTranslate;
      break;
    default:
      // TODO: ERROR handle
    }
    service(clip)
      .then(translated => `${clip} => ${translated}\n`)
      .then(contents => {
        fs.appendFileSync(logPath, contents);
      });
    return;
  }
  fs.appendFileSync(logPath, clip);
}

mb.on('ready', () => {
  globalShortcut.register('Control+Command+S', saveContents);
  if (!globalShortcut.isRegistered('Control+Command+S')) {
    // TODO: alert and end process
  }
});

mb.on('show', () => {
  mb.window.reload();
});<commit_msg>Refactoring service selection<commit_after>import {app, globalShortcut, clipboard} from 'electron';
import fs from 'fs';
import menubar from 'menubar';
import setting from '../setting.json';
import googleTranslate from './google-translate';
import glosbeTranslate from './glosbe-translate';

const dir = process.cwd();
const logPath = `${dir}/log.txt`;
const indexPath = `file://${dir}/app/view/popup.html`;
const SERVICE = {
  GOOGLE: 'google',
  GLOSBE: 'glosbe'
};

const mb = menubar({index: indexPath});

function saveContents() {
  const clip = clipboard.readText();
  console.log(clip);

  if (setting.enableServiceHook) {
    let service;
    if (setting.service === SERVICE.GOOGLE) service = googleTranslate;
    else if (setting.service === SERVICE.GLOSBE) service = glosbeTranslate;
    else {
      // TODO: ERROR handle
    }
    service(clip)
      .then(translated => `${clip} => ${translated}\n`)
      .then(contents => {
        fs.appendFileSync(logPath, contents);
      });
    return;
  }
  fs.appendFileSync(logPath, clip);
}

mb.on('ready', () => {
  globalShortcut.register('Control+Command+S', saveContents);
  if (!globalShortcut.isRegistered('Control+Command+S')) {
    // TODO: alert and end process
  }
});

mb.on('show', () => {
  mb.window.reload();
});
{'fraction_non_alphanumeric': 0.16498838109992253, 'fraction_numerical': 0.0, 'mean_word_length': 3.7221206581352835, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '3256095', 'n_tokens_mistral': 851, 'n_tokens_neox': 838, 'n_words': 187}