Skip to content

Commit 1fe8087

Browse files
committed
feat: add history command
1 parent 1c0b7e4 commit 1fe8087

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

src/components/terminal/TerminalBody.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const TerminalBody: FC<TerminalBodyProps> = () => {
1515

1616
const [history, setHistory] = useState<History[]>([]);
1717
const [motdVisible, setMotdVisible] = useState(true);
18+
const [commandHistory, setCommandHistory] = useState<string[]>([]);
1819

1920
const scrollToRef = useRef<HTMLDivElement>(null);
2021

@@ -26,6 +27,10 @@ const TerminalBody: FC<TerminalBodyProps> = () => {
2627
function handlePromptEnter(prompt: string) {
2728
const { command, args, sudo } = processPrompt(prompt);
2829

30+
if (command) {
31+
setCommandHistory((prev) => [...prev, command]);
32+
}
33+
2934
if (command === "clear") {
3035
handleClear();
3136
return;
@@ -40,8 +45,12 @@ const TerminalBody: FC<TerminalBodyProps> = () => {
4045
{
4146
id: uuidv4(),
4247
prompt,
43-
response: getCommandResponse({ command, args, sudo }, username),
4448
username,
49+
response: getCommandResponse(
50+
{ command, args, sudo },
51+
username,
52+
commandHistory
53+
),
4554
},
4655
]);
4756
}
@@ -69,6 +78,7 @@ const TerminalBody: FC<TerminalBodyProps> = () => {
6978

7079
<TerminalPrompt username={username}>
7180
<TerminalPromptInput
81+
history={commandHistory}
7282
onEnter={handlePromptEnter}
7383
onClear={handleClear}
7484
/>

src/components/terminal/TerminalPromptInput.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import parse from "html-react-parser";
55
import { COMMAND_NAMES } from "../../lib/commands";
66

77
interface TerminalPromptInputProps {
8+
history: string[];
89
onEnter(prompt: string): void;
910
onClear(): void;
1011
}

src/lib/commands.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ const KALI_LOGO = `
8181

8282
const TECH_STACK = `<a href="https://github.com/0l1v3rr/github-readme-tech-stack" target="_blank"><img src="https://github-readme-tech-stack.vercel.app/api/cards?title=Tech+Stack&width=420&align=center&titleAlign=center&fontSize=20&lineHeight=10&lineCount=2&theme=0l1v3rr&line1=node.js%2Cnode.js%2Cauto%3Bexpress%2Cexpress%2Cffffff%3Bnestjs%2Cnestjs%2Ce12a54%3B&line2=react%2Creact%2Cauto%3Btailwindcss%2Ctailwind%2Cauto%3Btypescript%2Ctypescript%2Cauto%3B" alt="Tech Stack" /></a>`;
8383

84-
const COMMANDS: Record<string, (username: string, args: string[]) => string> = {
84+
const COMMANDS: Record<
85+
string,
86+
(username: string, args: string[], history: string[]) => string
87+
> = {
8588
su: () => "",
8689
whoami: (username) => username,
8790
motd: () => MOTD,
@@ -103,6 +106,7 @@ const COMMANDS: Record<string, (username: string, args: string[]) => string> = {
103106
104107
I also enjoy showing off my skills in <b>various competitions</b>. My most notable achievement is my participation in the prestigious <b>WorldSkills</b> competitions, where I won a <b>gold medal in Web Technologies category</b>, making me a <b>world champion</b>.`,
105108
echo: (_, args) => args.join("&nbsp;"),
109+
history: (_, __, history) => history.join("<br/>"),
106110
};
107111

108112
export const COMMAND_NAMES = [...Object.keys(COMMANDS), "clear", "help"].sort(
@@ -111,13 +115,14 @@ export const COMMAND_NAMES = [...Object.keys(COMMANDS), "clear", "help"].sort(
111115

112116
export function getCommandResponse(
113117
{ command, sudo, args }: Prompt,
114-
username: string
118+
username: string,
119+
history: string[]
115120
) {
116121
if (sudo && !command) return "Usage: sudo [command] [args]";
117122
if (!command) return "";
118123

119124
if (command in COMMANDS) {
120-
let result = COMMANDS[command](username, args);
125+
let result = COMMANDS[command](username, args, history);
121126
if (command !== "kali") {
122127
result = result.replace(/\n/g, "<br/>");
123128
}

0 commit comments

Comments
 (0)