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

var c = require('commander'),
    through = require('through'),
    color = require('bash-color'),
    levelup = require('levelup'),
    path = require('path'),
    fs = require('fs'),
    stream = require('stream'),
    dir = path.resolve(process.env.HOME || process.env.USERPROFILE, '.tik'),
    isCli = (require.main === module);

if (!fs.existsSync(dir)) fs.mkdirSync(dir);

function newlineify() {
  var tr = through(write);

  return tr;

  function write(buf) {
    this.queue(buf.toString() + '\n');
  }
}

function Tik(settings) {

  this.settings = settings || {};
  this.settings.db = this.settings.db || dir + '/db';
  this.db = levelup(this.settings.db);

  return this;
}

Tik.prototype.listAll = function () {
  var tr = through(),
      db = this.db;

  process.nextTick(go);
  return tr;

  function go() {
    db.createReadStream()
      .on('data', function (data) {
        if (!data.key || !data.value) return;
        tr.queue([color.green(data.key + ':'), data.value].join(' '));
      })
      .on('end', closeStream)
      .on('close', closeStream);
  }

  function closeStream() { tr.queue(null); }
};

Tik.prototype.keyStream = function () {
  return this.db.createKeyStream();
};

Tik.prototype.readStream = function () {
  var tr = through(write),
      db = this.db;

  return tr;

  function write(buf) {
    var keyName = buf.toString();
    db.get(keyName, function (err, data) {
      if (err) tr.queue(undefined);
      if (data) tr.queue(data);
    });
  }
};

Tik.prototype.writeStream = function () {
  return this.db.createWriteStream({ type: 'put' });
};

Tik.prototype.deleteStream = function () {
  return this.db.createWriteStream({ type: 'del' });
};

if (isCli) {
  c
    .version('0.0.0')
    .option('');
  c
    .command('rm <key>')
    .description('remove key from database')
    .action(function (keyName) {
      if (!keyName) c.help();
      var delStream = new Tik().deleteStream();
      delStream.write({ key: keyName });
      delStream.end();
    });
  c
    .command('ls')
    .description('list all items')
    .action(function () {
      new Tik().listAll().pipe(newlineify()).pipe(process.stdout);
     });
  c
    .command('lskeys')
    .description('list all keys')
    .action(function () {
      new Tik().keyStream().pipe(newlineify()).pipe(process.stdout);
    });
  c
    .command('*')
    .action(function (stuff) {

      if (c.args.length === 1) {

        console.dir(c.args)
        var read = new Tik().readStream(),
            rs = stream.Readable();

        rs.push(c.args[0]);
        rs.push(null);

        rs.pipe(read).pipe(newlineify()).pipe(process.stdout);

      } else {

        c.args.pop();

        var keyName = c.args.shift(),
            keyValue = c.args.join(' '),<commit_msg>weeee<commit_after>#!/usr/bin/env node

var c = require('commander'),
    through = require('through'),
    color = require('bash-color'),
    levelup = require('levelup'),
    path = require('path'),
    fs = require('fs'),
    stream = require('stream'),
    dir = path.resolve(process.env.HOME || process.env.USERPROFILE, '.tik'),
    isCli = (require.main === module);

if (!fs.existsSync(dir)) fs.mkdirSync(dir);

function newlineify() {
  var tr = through(write);

  return tr;

  function write(buf) {
    this.queue(buf.toString() + '\n');
  }
}

function Tik(settings) {

  this.settings = settings || {};
  this.settings.db = this.settings.db || dir + '/db';
  this.db = levelup(this.settings.db);

  return this;
}

Tik.prototype.listAll = function () {
  var tr = through(),
      db = this.db;

  process.nextTick(go);
  return tr;

  function go() {
    db.createReadStream()
      .on('data', function (data) {
        if (!data.key || !data.value) return;
        tr.queue([color.green(data.key + ':'), data.value].join(' '));
      })
      .on('end', closeStream)
      .on('close', closeStream);
  }

  function closeStream() { tr.queue(null); }
};

Tik.prototype.keyStream = function () {
  return this.db.createKeyStream();
};

Tik.prototype.readStream = function () {
  var tr = through(write, function () {}),
      db = this.db;

  return tr;

  function write(buf) {
    var keyName = buf.toString();
    db.get(keyName, function (err, data) {
      if (err) tr.queue(undefined);
      if (data) tr.queue(data);
    });
  }
};

Tik.prototype.writeStream = function () {
  return this.db.createWriteStream({ type: 'put' });
};

Tik.prototype.deleteStream = function () {
  return this.db.createWriteStream({ type: 'del' });
};

if (isCli) {
  c
    .version('0.0.0')
    .option('');
  c
    .command('rm <key>')
    .description('remove key from database')
    .action(function (keyName) {
      if (!keyName) c.help();
      var delStream = new Tik().deleteStream();
      delStream.write({ key: keyName });
      delStream.end();
    });
  c
    .command('ls')
    .description('list all items')
    .action(function () {
      new Tik().listAll().pipe(newlineify()).pipe(process.stdout);
     });
  c
    .command('lskeys')
    .description('list all keys')
    .action(function () {
      new Tik().keyStream().pipe(newlineify()).pipe(process.stdout);
    });
  c
    .command('*')
    .action(function (stuff) {

      if (c.args.length === 2) {

        var read = new Tik().readStream(),
            rs = stream.Readable();

        rs.push(c.args[0]);
        rs.push(null);

        rs.pipe(read).pipe(newlineify()).pipe(process.stdout);

      } else {

        c.args.pop();

        var keyName = c.args.shift(),
            keyValue = c.args.join(' '),
{'fraction_non_alphanumeric': 0.18946428571428572, 'fraction_numerical': 0.0017857142857142857, 'mean_word_length': 2.6824457593688362, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6751503', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1864, 'n_words': 368}