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

'use strict';

const _         = require('lodash');
const vorpal    = require('vorpal')();
const chalk     = vorpal.chalk;
const homeOrTmp = require('home-or-tmp');
const lifxsh    = require('./lib/lifxsh');
const mapper    = require('./lib/mapper');

/**
 * Constants
 */
const STORAGE_PATH = homeOrTmp + '/.lifxsh'; // settings, command history
const PROMPT = chalk.magenta.bold('LIFX>'); // prompt

// connect LIFX client
lifxsh.connect();

vorpal
  .command('list', 'List connected lights.')
  .action((args, cb) => {
    lifxsh.list();
    cb();
  });

vorpal
  .command('on <names...>', 'Turn light(s) on.')
  .option('-d, --duration [ms]', 'Duration (ms)')
  .autocompletion(lightNameAutocompletion)
  .action((args, cb) => {
    let opts = args.options;
    lifxsh.on(args.names, opts.duration);
    cb();
  });

vorpal
  .command('off <names...>', 'Turn light(s) off.')
  .option('-d, --duration [ms]', 'Duration (ms)')
  .autocompletion(lightNameAutocompletion)
  .action((args, cb) => {
    let opts = args.options;
    lifxsh.off(args.names, opts.duration);
    cb();
  });

vorpal
  .command('color <names...>', 'Change color of light(s).')
  .option('-h, --hue [value]', 'Hue (0-360)')
  .option('-s, --saturation [value]', 'Saturation (0-100)')
  .option('-b, --brightness [value]', 'Brightness (0-100)')
  .option('-k, --kelvin [value]', 'Kelvin (2500-9500)')
  .option('-d, --duration [ms]', 'Duration (ms)')
  .autocompletion(lightNameAutocompletion)
  .action((args, cb) => {
    let opts = args.options;
    let hasKelvin = _.isNumber(opts.kelvin);
    let color = {
      hue: opts.hue,
      saturation: hasKelvin ? 0 : opts.saturation,
      brightness: opts.brightness,
      kelvin: opts.kelvin
    };
    lifxsh.color(args.names, color, opts.duration);
    cb();
  });

vorpal
  .find('exit')
  .description('Exit lifxsh.')
  .action((args, cb) => {
    lifxsh.disconnect();
    process.exit(0);
  });

/**
 * Vorpal command light name autocompletion.
 */
function lightNameAutocompletion(text, iteration, cb) {
  let lastToken = text.split(' ').pop();
  let lightNames = mapper.getNames();
  let match = this.match(lastToken, lightNames);
<commit_msg>Set saturation 0% if kelvin, 100% if hue<commit_after>#!/usr/bin/env node

'use strict';

const _         = require('lodash');
const vorpal    = require('vorpal')();
const chalk     = vorpal.chalk;
const homeOrTmp = require('home-or-tmp');
const lifxsh    = require('./lib/lifxsh');
const mapper    = require('./lib/mapper');

/**
 * Constants
 */
const STORAGE_PATH = homeOrTmp + '/.lifxsh'; // settings, command history
const PROMPT = chalk.magenta.bold('LIFX>'); // prompt

// connect LIFX client
lifxsh.connect();

vorpal
  .command('list', 'List connected lights.')
  .action((args, cb) => {
    lifxsh.list();
    cb();
  });

vorpal
  .command('on <names...>', 'Turn light(s) on.')
  .option('-d, --duration [ms]', 'Duration (ms)')
  .autocompletion(lightNameAutocompletion)
  .action((args, cb) => {
    let opts = args.options;
    lifxsh.on(args.names, opts.duration);
    cb();
  });

vorpal
  .command('off <names...>', 'Turn light(s) off.')
  .option('-d, --duration [ms]', 'Duration (ms)')
  .autocompletion(lightNameAutocompletion)
  .action((args, cb) => {
    let opts = args.options;
    lifxsh.off(args.names, opts.duration);
    cb();
  });

vorpal
  .command('color <names...>', 'Change color of light(s).')
  .option('-h, --hue [value]', 'Hue (0-360)')
  .option('-s, --saturation [value]', 'Saturation (0-100)')
  .option('-b, --brightness [value]', 'Brightness (0-100)')
  .option('-k, --kelvin [value]', 'Kelvin (2500-9500)')
  .option('-d, --duration [ms]', 'Duration (ms)')
  .autocompletion(lightNameAutocompletion)
  .action((args, cb) => {
    let opts = args.options;
    let color = {
      hue: opts.hue,
      saturation: getSaturation(opts),
      brightness: opts.brightness,
      kelvin: opts.kelvin
    };
    lifxsh.color(args.names, color, opts.duration);
    cb();
  });

vorpal
  .find('exit')
  .description('Exit lifxsh.')
  .action((args, cb) => {
    lifxsh.disconnect();
    process.exit(0);
  });

/**
 * If saturation is omitted, use 0% saturation when kelvin is defined, 100% if not.
 *
 * @param {any} opts
 * @returns Saturation value.
 */
function getSaturation(opts) {
  if (_.isNumber(opts.saturation)) {
    return opts.saturation;
  } else if (_.isNumber(opts.kelvin)) {
    return 0;
  } else if (_.isNumber(opts.hue)) {
    return 100;
  }
  return;
}

/**
 * Vorpal command light name autocompletion.
 */
function lightNameAutocompletion(text, iteration, cb) {
  let lastToken = text.split(' ').pop();
  let lightNames = mapper.getNames();
  let match = this.match(lastToken, lightNames);

{'fraction_non_alphanumeric': 0.19731318219983207, 'fraction_numerical': 0.011544920235096557, 'mean_word_length': 3.3044263775971094, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, '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': '282724', 'n_tokens_mistral': 1788, 'n_tokens_neox': 1737, 'n_words': 381}