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" ;
2
9
import { twMerge } from "tailwind-merge" ;
3
10
import { getColorfulPrompt } from "../../lib/utils" ;
4
11
import parse from "html-react-parser" ;
@@ -11,12 +18,20 @@ interface TerminalPromptInputProps {
11
18
}
12
19
13
20
const TerminalPromptInput : FC < TerminalPromptInputProps > = ( {
21
+ history,
14
22
onEnter,
15
23
onClear,
16
24
} ) => {
17
25
const [ input , setInput ] = useState ( "" ) ;
18
26
const [ inputFocus , setInputFocus ] = useState ( false ) ;
19
27
const [ caretPosition , setCaretPosition ] = useState ( 0 ) ;
28
+ const [ currentHistoryIndex , setCurrentHistoryIndex ] = useState (
29
+ history . length
30
+ ) ;
31
+
32
+ useEffect ( ( ) => {
33
+ setCurrentHistoryIndex ( history . length ) ;
34
+ } , [ history . length ] ) ;
20
35
21
36
function handleSubmit ( e : FormEvent < HTMLFormElement > ) : void {
22
37
e . preventDefault ( ) ;
@@ -43,6 +58,19 @@ const TerminalPromptInput: FC<TerminalPromptInputProps> = ({
43
58
onClear ( ) ;
44
59
return ;
45
60
}
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
+ }
46
74
}
47
75
48
76
return (
@@ -61,6 +89,7 @@ const TerminalPromptInput: FC<TerminalPromptInputProps> = ({
61
89
onChange = { ( e ) => {
62
90
setInput ( e . target . value ) ;
63
91
setCaretPosition ( e . target . selectionStart ?? 0 ) ;
92
+ setCurrentHistoryIndex ( history . length ) ;
64
93
} }
65
94
className = "w-full cursor-default whitespace-nowrap bg-transparent p-0 text-kali-white text-transparent focus:outline-none"
66
95
/>
0 commit comments