{output.map(({kind, text}, key) =>
{ kind === 'command' ? '> ' : ''} {text}
)}
{
autocomplete.length > 0 &&
{autocomplete.slice(0, LIMIT).map(variant => {variant})}
{autocomplete.length > LIMIT && ... and {autocomplete.length - LIMIT} more}
}
{
setHistoryPtr(history.length);
setInput(e.target.value)
}}
onKeyDown={e => {
function consumeEvent() {
e.preventDefault();
e.stopPropagation();
}
if (e.keyCode === 9) {
const text = e.target.value;
let variants = variantsSupplier().filter(v => v.startsWith(text));
variants.sort();
if (variants.length !== 0) {
const shared = sharedStartOfSortedArray(variants);
if (shared.length !== text.length) {
setInput(shared);
}
}
setAutocomplete(variants);
consumeEvent();
} else if (e.keyCode === 38) {
setHistoryPtr(ptr => Math.max(ptr - 1, 0));
consumeEvent();
} else if (e.keyCode === 40) {
setHistoryPtr(ptr => {
if (ptr !== history.length) {
Math.min(ptr + 1, history.length - 1)
}
});
consumeEvent();
}
}}
onKeyUp={e => {
if (e.keyCode === 13) {
const command = e.target.value;
setAutocomplete([]);
setInput('');
addToOutput(
{
kind: 'command',
text: command
}
);
const commandStr = command.trim();
if (commandStr) {
commandProcessor(commandStr, addToOutput);
if (history.length === 0 || command !== history[history.length - 1]) {
setHistory(history => [...history, command]);
}
}
setHistoryPtr(history.length);
}
}}
/>