Add exit command (fix #3)

This commit is contained in:
Daniel Kluge 2021-12-26 14:07:46 +01:00
parent eb49cfdd68
commit 3dea76b80f
2 changed files with 30 additions and 3 deletions

View File

@ -38,7 +38,7 @@ const REPLInput: NextPage<REPLInputParams> = ({historyCallback, inputRef}) => {
} }
}; };
const tabComplete = (e: React.KeyboardEvent<HTMLInputElement>) => { const keyEvent = (e: React.KeyboardEvent<HTMLInputElement>) => {
const input = (e.target as HTMLInputElement); const input = (e.target as HTMLInputElement);
if (e.key === "Tab" && currentCmd.length !== 0) { if (e.key === "Tab" && currentCmd.length !== 0) {
e.preventDefault(); e.preventDefault();
@ -49,6 +49,7 @@ const REPLInput: NextPage<REPLInputParams> = ({historyCallback, inputRef}) => {
} else setJustTabbed(0); } else setJustTabbed(0);
if (e.key === "Enter") { if (e.key === "Enter") {
e.preventDefault();
const result = executeCommand((e.target as HTMLInputElement).value); const result = executeCommand((e.target as HTMLInputElement).value);
input.value = ""; input.value = "";
if(typed.current) typed.current.innerHTML = ""; if(typed.current) typed.current.innerHTML = "";
@ -56,13 +57,22 @@ const REPLInput: NextPage<REPLInputParams> = ({historyCallback, inputRef}) => {
historyCallback(result); historyCallback(result);
} }
if (e.key === "d" && e.ctrlKey) {
e.preventDefault();
const result = executeCommand("exit");
input.value = "";
if(typed.current) typed.current.innerHTML = "";
if(completion.current) completion.current.innerHTML = "";
historyCallback(result);
}
return false; return false;
}; };
return <div className={styles.wrapperwrapper}> return <div className={styles.wrapperwrapper}>
<span className={styles.inputstart}>$&nbsp;</span> <span className={styles.inputstart}>$&nbsp;</span>
<div className={styles.wrapper}> <div className={styles.wrapper}>
<input ref={inputRef as MutableRefObject<HTMLInputElement>} className={styles.in} type={"text"} onChange={replinOnChange} onKeyDown={tabComplete} spellCheck={"false"} autoFocus maxLength={20} /> <input ref={inputRef as MutableRefObject<HTMLInputElement>} className={styles.in} type={"text"} onChange={replinOnChange} onKeyDown={keyEvent} spellCheck={"false"} autoFocus maxLength={20} />
<span className={styles.completionWrapper}><span ref={typed} className={styles.typed}></span><span ref={completion} className={styles.completion}></span></span> <span className={styles.completionWrapper}><span ref={typed} className={styles.typed}></span><span ref={completion} className={styles.completion}></span></span>
</div> </div>
</div>; </div>;

View File

@ -178,4 +178,21 @@ const project: Command = {
} }
}; };
export const commandList = [about, help, man, project]; const exitCmd: Command = {
name: "exit",
desc: "Tries to close this tab. Mostly fails because of restrictions.",
execute: () => {
if (typeof window !== undefined) {
window.opener = null;
window.open('', '_self');
window.close();
}
return [
"If you can read this, closing the window did not work.",
"This is because of restriction in JavaScript.",
"Read more here: https://developer.mozilla.org/en-US/docs/Web/API/Window/close"
]
}
}
export const commandList = [about, help, man, project, exitCmd];