merge master

This commit is contained in:
Olivier Gagnon 2021-09-18 02:38:15 -04:00
commit 23e8bc2e71
2 changed files with 21 additions and 10 deletions

File diff suppressed because one or more lines are too long

@ -42,16 +42,23 @@ interface IProps {
router: IRouter; router: IRouter;
player: IPlayer; player: IPlayer;
} }
// Save command in case we de-load this screen.
let command = "";
export function TerminalInput({ terminal, router, player }: IProps): React.ReactElement { export function TerminalInput({ terminal, router, player }: IProps): React.ReactElement {
const terminalInput = useRef<HTMLInputElement>(null); const terminalInput = useRef<HTMLInputElement>(null);
const [value, setValue] = useState(""); const [value, setValue] = useState(command);
const [possibilities, setPossibilities] = useState<string[]>([]); const [possibilities, setPossibilities] = useState<string[]>([]);
const classes = useStyles(); const classes = useStyles();
function saveValue(value: string): void {
command = value;
setValue(value);
}
function handleValueChange(event: React.ChangeEvent<HTMLInputElement>): void { function handleValueChange(event: React.ChangeEvent<HTMLInputElement>): void {
setValue(event.target.value); saveValue(event.target.value);
setPossibilities([]); setPossibilities([]);
} }
@ -66,13 +73,13 @@ export function TerminalInput({ terminal, router, player }: IProps): React.React
switch (mod.toLowerCase()) { switch (mod.toLowerCase()) {
case "backspace": case "backspace":
if (start > 0 && start <= inputLength + 1) { if (start > 0 && start <= inputLength + 1) {
setValue(inputText.substr(0, start - 1) + inputText.substr(start)); saveValue(inputText.substr(0, start - 1) + inputText.substr(start));
} }
break; break;
case "deletewordbefore": // Delete rest of word before the cursor case "deletewordbefore": // Delete rest of word before the cursor
for (let delStart = start - 1; delStart > 0; --delStart) { for (let delStart = start - 1; delStart > 0; --delStart) {
if (inputText.charAt(delStart) === " ") { if (inputText.charAt(delStart) === " ") {
setValue(inputText.substr(0, delStart) + inputText.substr(start)); saveValue(inputText.substr(0, delStart) + inputText.substr(start));
return; return;
} }
} }
@ -80,7 +87,7 @@ export function TerminalInput({ terminal, router, player }: IProps): React.React
case "deletewordafter": // Delete rest of word after the cursor case "deletewordafter": // Delete rest of word after the cursor
for (let delStart = start + 1; delStart <= value.length + 1; ++delStart) { for (let delStart = start + 1; delStart <= value.length + 1; ++delStart) {
if (inputText.charAt(delStart) === " ") { if (inputText.charAt(delStart) === " ") {
setValue(inputText.substr(0, start) + inputText.substr(delStart)); saveValue(inputText.substr(0, start) + inputText.substr(delStart));
return; return;
} }
} }
@ -160,7 +167,7 @@ export function TerminalInput({ terminal, router, player }: IProps): React.React
event.preventDefault(); event.preventDefault();
terminal.print(`[${player.getCurrentServer().hostname} ~${terminal.cwd()}]> ${value}`); terminal.print(`[${player.getCurrentServer().hostname} ~${terminal.cwd()}]> ${value}`);
terminal.executeCommands(router, player, value); terminal.executeCommands(router, player, value);
setValue(""); saveValue("");
return; return;
} }
@ -207,7 +214,7 @@ export function TerminalInput({ terminal, router, player }: IProps): React.React
const newValue = tabCompletion(command, arg, allPos, value); const newValue = tabCompletion(command, arg, allPos, value);
if (typeof newValue === "string" && newValue !== "") { if (typeof newValue === "string" && newValue !== "") {
setValue(newValue); saveValue(newValue);
} }
if (Array.isArray(newValue)) { if (Array.isArray(newValue)) {
setPossibilities(newValue); setPossibilities(newValue);
@ -242,7 +249,7 @@ export function TerminalInput({ terminal, router, player }: IProps): React.React
--terminal.commandHistoryIndex; --terminal.commandHistoryIndex;
} }
const prevCommand = terminal.commandHistory[terminal.commandHistoryIndex]; const prevCommand = terminal.commandHistory[terminal.commandHistoryIndex];
setValue(prevCommand); saveValue(prevCommand);
const ref = terminalInput.current; const ref = terminalInput.current;
if (ref) { if (ref) {
setTimeout(function () { setTimeout(function () {
@ -272,11 +279,11 @@ export function TerminalInput({ terminal, router, player }: IProps): React.React
// Latest command, put nothing // Latest command, put nothing
if (i == len || i == len - 1) { if (i == len || i == len - 1) {
terminal.commandHistoryIndex = len; terminal.commandHistoryIndex = len;
setValue(""); saveValue("");
} else { } else {
++terminal.commandHistoryIndex; ++terminal.commandHistoryIndex;
const prevCommand = terminal.commandHistory[terminal.commandHistoryIndex]; const prevCommand = terminal.commandHistory[terminal.commandHistoryIndex];
setValue(prevCommand); saveValue(prevCommand);
} }
} }