2021-12-14 23:08:18 +01:00
|
|
|
import type { NextPage } from "next";
|
2021-12-14 23:45:00 +01:00
|
|
|
import React from "react";
|
|
|
|
import { commandCompletion } from "../lib/commands";
|
|
|
|
import styles from "../styles/REPLInput.module.css";
|
2021-12-14 23:08:18 +01:00
|
|
|
|
|
|
|
const REPLInput: NextPage = () => {
|
|
|
|
const typed = React.createRef<HTMLSpanElement>();
|
|
|
|
const completion = React.createRef<HTMLSpanElement>();
|
|
|
|
const [currentCmd, setCurrentCmd] = React.useState("");
|
|
|
|
|
|
|
|
const replinOnChange = (e: React.FormEvent<HTMLInputElement>) => {
|
|
|
|
const currentInput = (e.target as HTMLInputElement).value;
|
|
|
|
const suggest = commandCompletion(currentInput);
|
|
|
|
setCurrentCmd(suggest);
|
|
|
|
if (typed.current) typed.current.innerHTML = currentInput;
|
|
|
|
if (completion.current) completion.current.innerHTML = suggest.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>) => {
|
|
|
|
if (e.key === "Tab" && currentCmd !== "") {
|
|
|
|
e.preventDefault();
|
|
|
|
(e.target as HTMLInputElement).value = currentCmd;
|
|
|
|
if(typed.current) typed.current.innerHTML = currentCmd;
|
|
|
|
if(completion.current) completion.current.innerHTML = "";
|
|
|
|
}
|
|
|
|
return false;
|
2021-12-14 23:45:00 +01:00
|
|
|
};
|
2021-12-14 23:08:18 +01:00
|
|
|
|
|
|
|
return <div className={styles.wrapper}>
|
|
|
|
<input className={styles.in} type={"text"} onChange={replinOnChange} onKeyDown={tabComplete} spellCheck={"false"} />
|
|
|
|
<span className={styles.completionWrapper}><span ref={typed} className={styles.typed}></span><span ref={completion} className={styles.completion}></span></span>
|
2021-12-14 23:45:00 +01:00
|
|
|
</div>;
|
|
|
|
};
|
2021-12-14 23:08:18 +01:00
|
|
|
|
|
|
|
export default REPLInput;
|