Fixed empty terminal input bug

This commit is contained in:
Prozilla 2023-07-23 15:06:34 +02:00
parent fbbbd9b14b
commit 3d14013eea
No known key found for this signature in database
GPG key ID: 5858DFE71CAF31EE

View file

@ -1,7 +1,7 @@
import { useState } from "react"; import { useState } from "react";
import styles from "./Terminal.module.css"; import styles from "./Terminal.module.css";
import { Command } from "./commands.js";
import { useVirtualRoot } from "../../../hooks/VirtualRootContext.js"; import { useVirtualRoot } from "../../../hooks/VirtualRootContext.js";
import { Command } from "../../../features/applications/terminal/commands.js";
const USERNAME = "user"; const USERNAME = "user";
const HOSTNAME = "prozilla-os"; const HOSTNAME = "prozilla-os";
@ -32,11 +32,14 @@ function InputLine({ value, prefix, onChange, onKeyUp, onKeyDown }) {
} }
export function Terminal() { export function Terminal() {
const [inputKey, setInputKey] = useState(0);
const [inputValue, setInputValue] = useState(""); const [inputValue, setInputValue] = useState("");
const [history, setHistory] = useState([]); const [history, setHistory] = useState([]);
const virtualRoot = useVirtualRoot(); const virtualRoot = useVirtualRoot();
const [currentDirectory, setCurrentDirectory] = useState(virtualRoot.navigate("~")); const [currentDirectory, setCurrentDirectory] = useState(virtualRoot.navigate("~"));
// console.log(currentDirectory);
const prefix = `${USERNAME}@${HOSTNAME}:${currentDirectory.root ? "/" : currentDirectory.path}$ `; const prefix = `${USERNAME}@${HOSTNAME}:${currentDirectory.root ? "/" : currentDirectory.path}$ `;
const updatedHistory = history; const updatedHistory = history;
@ -53,10 +56,6 @@ export function Terminal() {
}; };
const submitInput = (value) => { const submitInput = (value) => {
// To do: make empty submit go to new line
if (value.trim() === "")
return;
pushHistory({ pushHistory({
text: prefix + value, text: prefix + value,
isInput: true isInput: true
@ -66,6 +65,9 @@ export function Terminal() {
value = value.trim(); value = value.trim();
if (value === "")
return;
const args = value.split(/ +/); const args = value.split(/ +/);
const commandName = args.shift().toLowerCase(); const commandName = args.shift().toLowerCase();
@ -105,6 +107,7 @@ export function Terminal() {
// console.log(event); // console.log(event);
if (event.key === "Enter") { if (event.key === "Enter") {
submitInput(value); submitInput(value);
setInputKey((previousKey) => previousKey + 1);
} }
}; };
@ -131,6 +134,7 @@ export function Terminal() {
<div className={styles.Terminal}> <div className={styles.Terminal}>
{displayHistory()} {displayHistory()}
<InputLine <InputLine
key={inputKey}
value={inputValue} value={inputValue}
prefix={prefix} prefix={prefix}
onKeyDown={onKeyDown} onKeyDown={onKeyDown}