import type { NextPage } from "next"; import React, { MutableRefObject } from "react"; import { commandCompletion, executeCommand } from "../../lib/commands"; import styles from "../../styles/REPL/REPLInput.module.css"; interface REPLInputParams { historyCallback: CallableFunction; inputRef: MutableRefObject; } const REPLInput: NextPage = ({historyCallback, inputRef}) => { const typed = React.createRef(); const completion = React.createRef(); const [currentCmd, setCurrentCmd] = React.useState([]); const [justTabbed, setJustTabbed] = React.useState(0); const replinOnChange = (e: React.FormEvent) => { 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); } }; const tabComplete = (e: React.KeyboardEvent) => { const input = (e.target as HTMLInputElement); if (e.key === "Tab" && currentCmd.length !== 0) { e.preventDefault(); input.value = currentCmd[justTabbed % currentCmd.length]; if(typed.current) typed.current.innerHTML = currentCmd[justTabbed % currentCmd.length]; if(completion.current) completion.current.innerHTML = ""; setJustTabbed(justTabbed + 1); } else setJustTabbed(0); if (e.key === "Enter") { const result = executeCommand((e.target as HTMLInputElement).value); input.value = ""; if(typed.current) typed.current.innerHTML = ""; if(completion.current) completion.current.innerHTML = ""; historyCallback(result); } return false; }; return
} className={styles.in} type={"text"} onChange={replinOnChange} onKeyDown={tabComplete} spellCheck={"false"} autoFocus maxLength={20} />
; }; export default REPLInput;