<commit_before>'use strict';
let _ = require('lodash');
let clear = require('clear');
let errorWithMetadata = require('./util/error-with-metadata');
let read = require('./src/cli-read');
let typeDelay = 50;
let storedTypeDelays = [];
let defaultInterlocutor = "????";
let interlocutor;
exports.clear = function() {
	clear();
	return 'done';
};
exports.ds = function() {
	storedTypeDelays.push(typeDelay);
	return 'done';
};
exports.dr = function() {
	let storedDelay = storedTypeDelays.pop();
	if(storedDelay) {
		typeDelay = storedDelay;
	}
	return 'done';
};
exports.d = function(delay) {
	typeDelay = delay;
	return 'done';
};
exports.id = function(amount) {
	typeDelay += amount;
	return 'done';
};
exports.dd = function(amount) {
	typeDelay -= amount;
	if(typeDelay < 0) {
		typeDelay = 0;
	}
	return 'done';
};
exports['@string'] = function(string) {
	this.typeIndex = this.typeIndex || 0;
	if(this.typeIndex >= string.length) {
		return 'done';
	}
	if(typeDelay < 10) {
		process.stdout.write(string.slice(this.typeIndex));
		return 'done';
	}
	if (
		this.typeIndex === 0
		|| (
			Date.now() - this.lastTypeTimestamp >= typeDelay
		)
	) {
		process.stdout.write(string[this.typeIndex++]);
		this.lastTypeTimestamp = Date.now();
	}
	return 'yield';
};
exports.who = function(who) {
	interlocutor = who;
	return 'done';
};
exports.dh = function() {
	process.stdout.write (
		"  " + (interlocutor || defaultInterlocutor) + ": "
	);
	return 'done';
};
exports.di = function() {
	let whitespace = (
		(interlocutor || defaultInterlocutor).replace(/./g, " ")
	);
	process.stdout.write("  " + whitespace + "  ");
	return 'done';
};
exports.w = function(howLong) {
	if(!this.startTimestamp) {
		this.startTimestamp = Date.now();
	}
	if(Date.now() - this.startTimestamp > howLong) {
		return 'done';
	}
	else {
		return 'yield';
	}
};
exports.read = function(question, command) {
	if(this.done) {
		return 'done';
	}
	else
	if(this.waiting) {
		return 'yield';
	}
	this.waiting = true;
	if(typeof question !== 'string') {
		question = '> ';
	}
	let result = read(question);
	if(command.deferred) {
		command.deferred.resolve(result);
	}
	result.done((function() {
		this.done = true;
	}).bind(this));
	return 'yield';
};
exports.choice = function(options, command) {
	if(this.done) {
		return 'done';
	}
	else
	if(this.waiting) {
		return 'yield';
	}
	this.waiting = true;
	if(!Array.isArray(options)) {
		let optionsObject = options;
		options = [];
		_.each(optionsObject, function(fn, matcher) {
			options.push({ matcher, fn });
		});
	}
	if(!command.question && command.question !== '') {
		command.question = '> ';
	}
	read(command.question).then(function(value) {
		let result;
		if (
			!options.some(function(option) {
				if(option.default) {
					result = option.fn(value);
					return true;
				}
				if(typeof option.matcher === 'string') {
					if(value.trim().toLowerCase() === option.matcher.trim().toLowerCase()) {
						result = option.fn(value);
						return true;
					}
					else {
						return false;
					}
				}
				if(option.matcher instanceof RegExp) {
					let matchResults = option.matcher.exec(value);
					if(!matchResults) {
						return false;
					}
					result = option.fn.apply(option, matchResults);
					return true;
				}
				else {
					throw errorWithMetadata (
						new Error("Bad choice command option"), {
							command: command,<commit_msg>io.choice: Call option functions using command contexts (if set).<commit_after>'use strict';
let _ = require('lodash');
let clear = require('clear');
let errorWithMetadata = require('./util/error-with-metadata');
let read = require('./src/cli-read');
let typeDelay = 50;
let storedTypeDelays = [];
let defaultInterlocutor = "????";
let interlocutor;
exports.clear = function() {
	clear();
	return 'done';
};
exports.ds = function() {
	storedTypeDelays.push(typeDelay);
	return 'done';
};
exports.dr = function() {
	let storedDelay = storedTypeDelays.pop();
	if(storedDelay) {
		typeDelay = storedDelay;
	}
	return 'done';
};
exports.d = function(delay) {
	typeDelay = delay;
	return 'done';
};
exports.id = function(amount) {
	typeDelay += amount;
	return 'done';
};
exports.dd = function(amount) {
	typeDelay -= amount;
	if(typeDelay < 0) {
		typeDelay = 0;
	}
	return 'done';
};
exports['@string'] = function(string) {
	this.typeIndex = this.typeIndex || 0;
	if(this.typeIndex >= string.length) {
		return 'done';
	}
	if(typeDelay < 10) {
		process.stdout.write(string.slice(this.typeIndex));
		return 'done';
	}
	if (
		this.typeIndex === 0
		|| (
			Date.now() - this.lastTypeTimestamp >= typeDelay
		)
	) {
		process.stdout.write(string[this.typeIndex++]);
		this.lastTypeTimestamp = Date.now();
	}
	return 'yield';
};
exports.who = function(who) {
	interlocutor = who;
	return 'done';
};
exports.dh = function() {
	process.stdout.write (
		"  " + (interlocutor || defaultInterlocutor) + ": "
	);
	return 'done';
};
exports.di = function() {
	let whitespace = (
		(interlocutor || defaultInterlocutor).replace(/./g, " ")
	);
	process.stdout.write("  " + whitespace + "  ");
	return 'done';
};
exports.w = function(howLong) {
	if(!this.startTimestamp) {
		this.startTimestamp = Date.now();
	}
	if(Date.now() - this.startTimestamp > howLong) {
		return 'done';
	}
	else {
		return 'yield';
	}
};
exports.read = function(question, command) {
	if(this.done) {
		return 'done';
	}
	else
	if(this.waiting) {
		return 'yield';
	}
	this.waiting = true;
	if(typeof question !== 'string') {
		question = '> ';
	}
	let result = read(question);
	if(command.deferred) {
		command.deferred.resolve(result);
	}
	result.done((function() {
		this.done = true;
	}).bind(this));
	return 'yield';
};
exports.choice = function(options, command) {
	if(this.done) {
		return 'done';
	}
	else
	if(this.waiting) {
		return 'yield';
	}
	this.waiting = true;
	if(!Array.isArray(options)) {
		let optionsObject = options;
		options = [];
		_.each(optionsObject, function(fn, matcher) {
			options.push({ matcher, fn });
		});
	}
	if(!command.question && command.question !== '') {
		command.question = '> ';
	}
	read(command.question).then(function(value) {
		let result;
		if (
			!options.some(function(option) {
				if(option.default) {
					result = option.fn(value);
					return true;
				}
				if(typeof option.matcher === 'string') {
					if(value.trim().toLowerCase() === option.matcher.trim().toLowerCase()) {
						result = option.fn.call(command.context, value);
						return true;
					}
					else {
						return false;
					}
				}
				if(option.matcher instanceof RegExp) {
					let matchResults = option.matcher.exec(value);
					if(!matchResults) {
						return false;
					}
					result = option.fn.apply(command.context, matchResults);
					return true;
				}
				else {
					throw errorWithMetadata (
						new Error("Bad choice command option"), {
							command: command,
{'fraction_non_alphanumeric': 0.17461706783369804, 'fraction_numerical': 0.0023340627279358132, 'mean_word_length': 3.57371581054036, 'pattern_counts': {'":': 2, '<': 7, '<?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': '4338913', 'n_tokens_mistral': 2683, 'n_tokens_neox': 2375, 'n_words': 456}