Skip to content

Commit 4f6abc1

Browse files
committed
Added "rm", "help" commands and fixed empty lists
1 parent e408132 commit 4f6abc1

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

src/components/CommandLine.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FormEvent } from "preact/compat";
1+
import { FormEvent, useRef } from "preact/compat";
22

33
type Props = {
44
command: string;
@@ -7,16 +7,24 @@ type Props = {
77
};
88

99
const CommandLine = ({ command, setCommand, handleCommand }: Props) => {
10+
const inputRef = useRef<HTMLInputElement>(null);
11+
1012
const onSubmit = (e: FormEvent) => {
1113
e.preventDefault();
1214
handleCommand(command);
15+
16+
// keep the input in focus after command submission
17+
if (inputRef.current) {
18+
inputRef.current.focus();
19+
}
1320
};
1421

1522
return (
1623
<div className="terminal__commandline">
1724
<span className="terminal__user">~$</span>
1825
<form onSubmit={onSubmit} className="terminal__form">
1926
<input
27+
ref={inputRef}
2028
type="text"
2129
value={command}
2230
onChange={(e) => setCommand((e.target as HTMLInputElement).value)}

src/hooks/useTerminal.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const useTerminal = () => {
99
const [tasks, dispatch] = useReducer(taskReducer, []);
1010

1111
const executeCommand = (command: string) => {
12+
// "add" command
1213
if (command.startsWith("add")) {
1314
const task = command.match(/"([^"]+)"/)?.[1];
1415
if (!task) {
@@ -18,7 +19,7 @@ export const useTerminal = () => {
1819
return `Task added: ${task}`;
1920
}
2021

21-
if (command.startsWith("mark")) {
22+
if (command.startsWith("check")) {
2223
const match = command.match(/#(\d+)/);
2324
if (!match) {
2425
return `Invalid command format. Use: check #taskId (e.g., check #1)`;
@@ -38,6 +39,28 @@ export const useTerminal = () => {
3839
return `Task marked as done: ${task.name}`;
3940
}
4041

42+
// "rm" command
43+
if (command.startsWith("rm")) {
44+
const match = command.match(/#(\d+)/);
45+
if (!match) {
46+
return `Invalid command format. Use: rm #taskId (e.g., rm #1)`;
47+
}
48+
49+
const taskId = parseInt(match[1], 10);
50+
if (isNaN(taskId)) {
51+
return `Invalid task ID. Use a valid number with the format: rm #taskId (e.g., rm #1)`;
52+
}
53+
54+
const task = tasks.find((task) => task.id === taskId);
55+
if (!task) {
56+
return `Task with ID #${taskId} not found.`;
57+
}
58+
59+
dispatch({ type: "REMOVE_TASK", payload: { id: taskId } });
60+
return `Task removed: ${task.name}`;
61+
}
62+
63+
// "cat" command
4164
if (command.startsWith("cat")) {
4265
const match = command.match(/#(\d+)/);
4366
if (!match) {
@@ -64,15 +87,35 @@ export const useTerminal = () => {
6487
`;
6588
}
6689

90+
// "ls" command
6791
if (command === "ls") {
68-
return tasks
92+
const listedTasks = tasks
6993
.map(
7094
(task) =>
7195
`${task.id}. [${task.status === "done" ? "x" : " "}] ${task.name}`
7296
)
7397
.join("\n");
98+
99+
if (!listedTasks) {
100+
return "<empty list>";
101+
}
102+
103+
return listedTasks;
104+
}
105+
106+
// "help" command
107+
if (command === "help") {
108+
return `
109+
add "task name" - Add a new task\n
110+
rm #taskId - Remove a task\n
111+
cat #taskId - Show info about a task\n
112+
check #taskId - Mark the task as done\n
113+
ls - List all the tasks\n
114+
clear - Clear the terminal display\n
115+
`;
74116
}
75117

118+
// "clear" command
76119
if (command === "clear") {
77120
setHistory([]);
78121
setCommand("");

0 commit comments

Comments
 (0)