frontpage/components/REPL/REPLInput.tsx

71 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-12-14 23:08:18 +01:00
import type { NextPage } from "next";
2021-12-16 01:15:43 +01:00
import React, { MutableRefObject } from "react";
2021-12-16 00:21:14 +01:00
import { commandCompletion, executeCommand } from "../../lib/commands";
import styles from "../../styles/REPL/REPLInput.module.css";
2021-12-14 23:08:18 +01:00
2021-12-16 01:15:43 +01:00
interface REPLInputParams {
historyCallback: CallableFunction;
inputRef: MutableRefObject<HTMLInputElement|undefined>;
}
const REPLInput: NextPage<REPLInputParams> = ({historyCallback, inputRef}) => {
2021-12-14 23:08:18 +01:00
const typed = React.createRef<HTMLSpanElement>();
const completion = React.createRef<HTMLSpanElement>();
2021-12-15 18:56:32 +01:00
const [currentCmd, setCurrentCmd] = React.useState<string[]>([]);
const [justTabbed, setJustTabbed] = React.useState<number>(0);
2021-12-14 23:08:18 +01:00
const replinOnChange = (e: React.FormEvent<HTMLInputElement>) => {
2021-12-17 18:55:00 +01:00
const input = (e.target as HTMLInputElement);
const currentInput = input.value.toLowerCase();
// Force lowercase
input.value = currentInput;
if (currentInput.includes(" ")) {
// Command already typed
input.maxLength = 524288; // Default value
if (typed.current) typed.current.innerHTML = "";
if (completion.current) completion.current.innerHTML = "";
setCurrentCmd([]);
return;
} else {
input.maxLength = 20;
// Get completion hint
const suggest = commandCompletion(currentInput);
setCurrentCmd(suggest);
if (suggest.length === 0) suggest.push("");
if (typed.current) typed.current.innerHTML = currentInput;
if (completion.current) completion.current.innerHTML = suggest[0].substring(currentInput.length);
}
2021-12-14 23:45:00 +01:00
};
2021-12-14 23:08:18 +01:00
const tabComplete = (e: React.KeyboardEvent<HTMLInputElement>) => {
2021-12-17 18:55:00 +01:00
const input = (e.target as HTMLInputElement);
2021-12-15 18:56:32 +01:00
if (e.key === "Tab" && currentCmd.length !== 0) {
2021-12-14 23:08:18 +01:00
e.preventDefault();
2021-12-17 18:55:00 +01:00
input.value = currentCmd[justTabbed % currentCmd.length];
2021-12-15 18:56:32 +01:00
if(typed.current) typed.current.innerHTML = currentCmd[justTabbed % currentCmd.length];
2021-12-14 23:08:18 +01:00
if(completion.current) completion.current.innerHTML = "";
2021-12-15 18:56:32 +01:00
setJustTabbed(justTabbed + 1);
2021-12-16 00:21:14 +01:00
} else setJustTabbed(0);
if (e.key === "Enter") {
const result = executeCommand((e.target as HTMLInputElement).value);
2021-12-17 18:55:00 +01:00
input.value = "";
2021-12-16 00:21:14 +01:00
if(typed.current) typed.current.innerHTML = "";
if(completion.current) completion.current.innerHTML = "";
historyCallback(result);
2021-12-14 23:08:18 +01:00
}
2021-12-16 00:21:14 +01:00
2021-12-14 23:08:18 +01:00
return false;
2021-12-14 23:45:00 +01:00
};
2021-12-14 23:08:18 +01:00
2021-12-16 01:03:54 +01:00
return <div className={styles.wrapperwrapper}>
<span className={styles.inputstart}>$&nbsp;</span>
<div className={styles.wrapper}>
2021-12-17 18:55:00 +01:00
<input ref={inputRef as MutableRefObject<HTMLInputElement>} className={styles.in} type={"text"} onChange={replinOnChange} onKeyDown={tabComplete} spellCheck={"false"} autoFocus maxLength={20} />
2021-12-16 01:03:54 +01:00
<span className={styles.completionWrapper}><span ref={typed} className={styles.typed}></span><span ref={completion} className={styles.completion}></span></span>
</div>
2021-12-14 23:45:00 +01:00
</div>;
};
2021-12-14 23:08:18 +01:00
export default REPLInput;