Skip to content

Commit f4791b2

Browse files
committed
feat: add arrow up/down movements
1 parent d42dfe9 commit f4791b2

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/components/terminal/TerminalBody.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ const TerminalBody: FC<TerminalBodyProps> = () => {
1616

1717
const [history, setHistory] = useState<History[]>([]);
1818
const [motdVisible, setMotdVisible] = useState(true);
19-
const [commandHistory, setCommandHistory] = useLocalStorage<string[]>(
20-
"commandHistory",
19+
const [promptHistory, setPromptHistory] = useLocalStorage<string[]>(
20+
"promptHistory",
2121
[]
2222
);
2323

@@ -32,7 +32,7 @@ const TerminalBody: FC<TerminalBodyProps> = () => {
3232
const { command, args, sudo } = processPrompt(prompt);
3333

3434
if (command) {
35-
setCommandHistory((prev) => [...prev, command]);
35+
setPromptHistory((prev) => [...prev, prompt]);
3636
}
3737

3838
if (command === "clear") {
@@ -53,7 +53,7 @@ const TerminalBody: FC<TerminalBodyProps> = () => {
5353
response: getCommandResponse(
5454
{ command, args, sudo },
5555
username,
56-
commandHistory
56+
promptHistory
5757
),
5858
},
5959
]);
@@ -82,7 +82,7 @@ const TerminalBody: FC<TerminalBodyProps> = () => {
8282

8383
<TerminalPrompt username={username}>
8484
<TerminalPromptInput
85-
history={commandHistory}
85+
history={promptHistory}
8686
onEnter={handlePromptEnter}
8787
onClear={handleClear}
8888
/>

src/components/terminal/TerminalPromptInput.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { FC, FormEvent, Fragment, KeyboardEvent, useState } from "react";
1+
import {
2+
FC,
3+
FormEvent,
4+
Fragment,
5+
KeyboardEvent,
6+
useEffect,
7+
useState,
8+
} from "react";
29
import { twMerge } from "tailwind-merge";
310
import { getColorfulPrompt } from "../../lib/utils";
411
import parse from "html-react-parser";
@@ -11,12 +18,20 @@ interface TerminalPromptInputProps {
1118
}
1219

1320
const TerminalPromptInput: FC<TerminalPromptInputProps> = ({
21+
history,
1422
onEnter,
1523
onClear,
1624
}) => {
1725
const [input, setInput] = useState("");
1826
const [inputFocus, setInputFocus] = useState(false);
1927
const [caretPosition, setCaretPosition] = useState(0);
28+
const [currentHistoryIndex, setCurrentHistoryIndex] = useState(
29+
history.length
30+
);
31+
32+
useEffect(() => {
33+
setCurrentHistoryIndex(history.length);
34+
}, [history.length]);
2035

2136
function handleSubmit(e: FormEvent<HTMLFormElement>): void {
2237
e.preventDefault();
@@ -43,6 +58,19 @@ const TerminalPromptInput: FC<TerminalPromptInputProps> = ({
4358
onClear();
4459
return;
4560
}
61+
62+
if (["ArrowUp", "ArrowDown"].includes(e.key)) {
63+
e.preventDefault();
64+
65+
const newHistoryIndex =
66+
e.key === "ArrowUp"
67+
? Math.max(0, currentHistoryIndex - 1)
68+
: Math.min(history.length, currentHistoryIndex + 1);
69+
70+
setCurrentHistoryIndex(newHistoryIndex);
71+
setInput(history[newHistoryIndex] ?? "");
72+
setCaretPosition(history[newHistoryIndex]?.length ?? 0);
73+
}
4674
}
4775

4876
return (
@@ -61,6 +89,7 @@ const TerminalPromptInput: FC<TerminalPromptInputProps> = ({
6189
onChange={(e) => {
6290
setInput(e.target.value);
6391
setCaretPosition(e.target.selectionStart ?? 0);
92+
setCurrentHistoryIndex(history.length);
6493
}}
6594
className="w-full cursor-default whitespace-nowrap bg-transparent p-0 text-kali-white text-transparent focus:outline-none"
6695
/>

0 commit comments

Comments
 (0)