<commit_before>'use strict';
const keypress = require('keypress');
const { getUiState } = require('../store');

keypress(process.stdin);

let buffer = '';
const history = [];
let currentSelectedHistory = 0;

/**
 * @param {Function<string>} onCommandEntered - the callback function
 * @returns {void}
 **/
function commandListener(onCommandEntered) {
	const uiState = getUiState();

	process.stdin.setEncoding('utf8');
	process.stdin.setRawMode(true);
	process.stdin.on('keypress', (letter, key) => {
		if (key && key.ctrl && key.name === 'c') {
			buffer = '';
			process.exit();
		} else if (key && key.name === 'backspace') {
			buffer = buffer.substring(0, buffer.length - 1);
			uiState.onChange(buffer);
		} else if (key && key.name === 'up') {
			if (history[currentSelectedHistory]) {
				buffer = history[currentSelectedHistory];
				uiState.onChange(buffer);
			} else {
				buffer = '';
				uiState.onChange(buffer);
			}

			if (currentSelectedHistory >= history.length) {
				currentSelectedHistory = 0;
			} else {
				currentSelectedHistory += 1;
			}
		} else if (key && key.name === 'down') {
			if (currentSelectedHistory >= history.length - 1) {
				currentSelectedHistory = history.length - 2;
			} else if (currentSelectedHistory <= -1) {
				currentSelectedHistory = -1;
			} else {
				currentSelectedHistory -= 1;
			}

			if (history[currentSelectedHistory]) {
				buffer = history[currentSelectedHistory];
				uiState.onChange(buffer);
			} else {
				buffer = '';
				uiState.onChange(buffer);
			}
		} else if (key && key.name === 'return') {
			if (buffer.length > 0) {
				onCommandEntered(
					buffer
						.trim()
						.replace(/\n/g, '')
						.replace(/\t/g, '')
				);
				history.reverse();
				history.push(buffer);
				history.reverse();
			}
			buffer = '';
			currentSelectedHistory = 0;
			uiState.onChange('');
		} else if (letter) {
			buffer += letter;
			uiState.onChange(buffer);
		}
	});
	process.stdin.resume();
}

module.exports = commandListener;<commit_msg>refactor(command-listener): optimize stdin keypress handling<commit_after>'use strict';
const keypress = require('keypress');
const { getUiState } = require('../store');

keypress(process.stdin);

let buffer = '';
const history = [];
let currentSelectedHistory = 0;

const onCtrlC = () => {
	buffer = '';
	process.exit();
};

const onBackspace = () => {
	buffer = buffer.substring(0, buffer.length - 1);
	getUiState().onChange(buffer);
};

const onUp = () => {
	const uiState = getUiState();
	if (history[currentSelectedHistory]) {
		buffer = history[currentSelectedHistory];
		uiState.onChange(buffer);
	} else {
		buffer = '';
		uiState.onChange(buffer);
	}

	if (currentSelectedHistory >= history.length) {
		currentSelectedHistory = 0;
	} else {
		currentSelectedHistory += 1;
	}
};

const onDown = () => {
	const uiState = getUiState();
	if (currentSelectedHistory >= history.length - 1) {
		currentSelectedHistory = history.length - 2;
	} else if (currentSelectedHistory <= -1) {
		currentSelectedHistory = -1;
	} else {
		currentSelectedHistory -= 1;
	}

	if (history[currentSelectedHistory]) {
		buffer = history[currentSelectedHistory];
		uiState.onChange(buffer);
	} else {
		buffer = '';
		uiState.onChange(buffer);
	}
};

const onReturn = onCommandEntered => {
	if (buffer.length > 0) {
		onCommandEntered(
			buffer
				.trim()
				.replace(/\n/g, '')
				.replace(/\t/g, '')
		);
		history.reverse();
		history.push(buffer);
		history.reverse();
	}
	buffer = '';
	currentSelectedHistory = 0;
	getUiState().onChange('');
};

const onRecieveLetter = letter => {
	buffer += letter;
	getUiState().onChange(buffer);
};

const keyNameMap = {
	backspace: onBackspace,
	up: onUp,
	down: onDown,
	return: onReturn,
};

/**
 * @param {Function<string>} onCommandEntered - the callback function
 * @returns {void}
 **/
function commandListener(onCommandEntered) {
	process.stdin.setEncoding('utf8');
	process.stdin.setRawMode(true);
	process.stdin.on('keypress', (letter, key) => {
		if (!key && letter) {
			onRecieveLetter(letter);
			return;
		}

		if (key) {
			if (key.ctrl && key.name) {
				onCtrlC();
				return;
			}

			if (keyNameMap[key.name]) {
				keyNameMap[key.name](onCommandEntered);
				return;
			}
		}
	});
	process.stdin.resume();
}

module.exports = commandListener;
{'fraction_non_alphanumeric': 0.16550441655044165, 'fraction_numerical': 0.00604370060437006, 'mean_word_length': 3.6219119226638026, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '7363994', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1416, 'n_words': 278}