Skip to content

Commit 0827cc2

Browse files
committed
🐛 Fix Up & Down arrows features
1 parent e038060 commit 0827cc2

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/components/Terminal.tsx

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const Terminal = () => {
8080
setInputVal("");
8181
setRerender(true);
8282
setHints([]);
83-
setPointer(0);
83+
setPointer(-1);
8484
};
8585

8686
const clearHistory = () => {
@@ -101,7 +101,7 @@ const Terminal = () => {
101101

102102
// Keyboard Press
103103
const [hints, setHints] = useState<string[]>([]);
104-
const [pointer, setPointer] = useState(0);
104+
const [pointer, setPointer] = useState(-1);
105105
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
106106
setRerender(false);
107107
const ctrlI = e.ctrlKey && e.key.toLowerCase() === "i";
@@ -144,21 +144,28 @@ const Terminal = () => {
144144

145145
// Go previous cmd
146146
if (e.key === "ArrowUp") {
147-
if (pointer < cmdHistory.length) {
148-
setInputVal(cmdHistory[pointer]);
149-
pointer + 1 !== cmdHistory.length &&
150-
setPointer((prevState) => prevState + 1);
151-
pointer + 1 !== cmdHistory.length && inputRef?.current?.blur();
152-
}
147+
if (pointer >= cmdHistory.length) return;
148+
149+
if (pointer + 1 === cmdHistory.length) return;
150+
151+
setInputVal(cmdHistory[pointer + 1]);
152+
setPointer((prevState) => prevState + 1);
153+
inputRef?.current?.blur();
153154
}
154155

155156
// Go next cmd
156157
if (e.key === "ArrowDown") {
157-
if (pointer > 0) {
158-
setInputVal(cmdHistory[pointer - 1]);
159-
pointer - 1 !== 0 && setPointer((prevState) => prevState - 1);
160-
pointer - 1 !== 0 && inputRef?.current?.blur();
158+
if (pointer < 0) return;
159+
160+
if (pointer === 0) {
161+
setInputVal("");
162+
setPointer(-1);
163+
return;
161164
}
165+
166+
setInputVal(cmdHistory[pointer - 1]);
167+
setPointer((prevState) => prevState - 1);
168+
inputRef?.current?.blur();
162169
}
163170
};
164171
// For caret position at the end

0 commit comments

Comments
 (0)