Skip to content

Commit 9abaa41

Browse files
Merge branch 'main' into feat/help-command
2 parents 86658e3 + bea6704 commit 9abaa41

28 files changed

+396
-19
lines changed

src/main/java/com/mycmd/App.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public static void main(String[] args) {
2929
if (command != null) {
3030
try {
3131
command.execute(cmdArgs, context);
32+
// Add to history after successful execution
33+
context.addToHistory(input);
3234
} catch (Exception e) {
3335
System.out.println("Error: " + e.getMessage());
3436
}
@@ -59,5 +61,6 @@ private static void registerCommands(Map<String, Command> commands) {
5961
commands.put("time", new TimeCommand());
6062
commands.put("tree", new TreeCommand());
6163
commands.put("date", new DateCommand());
64+
commands.put("history", new HistoryCommand());
6265
}
6366
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
package com.mycmd;
22

3+
/**
4+
* Represents a shell command that can be executed inside the MyCMD shell.
5+
* Implementations perform their operation when {@link #execute(String[], ShellContext)} is called.
6+
*/
37
public interface Command {
8+
/**
9+
* Execute the command.
10+
*
11+
* <p>Implementations should perform all output to standard output or use the provided
12+
* {@link ShellContext} for interactions that depend on the shell state (for example,
13+
* changing the current directory).</p>
14+
*
15+
* @param args command-line style arguments passed to the command. May be empty but
16+
* will not be null.
17+
* @param context current shell context containing state such as the current working
18+
* directory. Implementations may read and/or modify this context.
19+
* @throws Exception if the command cannot complete successfully. The caller (shell)
20+
* is responsible for catching and reporting exceptions.
21+
*
22+
* @implNote Common concrete commands include `date` and `time` which simply print the
23+
* current date/time to standard output. See their implementations for formatting details.
24+
*/
425
void execute(String[] args, ShellContext context);
526
}

src/main/java/com/mycmd/ShellContext.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package com.mycmd;
22

33
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.List;
46

57
public class ShellContext {
68
private File currentDir;
9+
private List<String> commandHistory;
10+
private static final int MAX_HISTORY = 10;
711

812
public ShellContext() {
913
this.currentDir = new File(System.getProperty("user.dir"));
14+
this.commandHistory = new ArrayList<>();
1015
}
1116

1217
public File getCurrentDir() {
@@ -16,4 +21,17 @@ public File getCurrentDir() {
1621
public void setCurrentDir(File dir) {
1722
this.currentDir = dir;
1823
}
24+
25+
public List<String> getCommandHistory() {
26+
return commandHistory;
27+
}
28+
29+
public void addToHistory(String command) {
30+
if (command != null && !command.trim().isEmpty()) {
31+
commandHistory.add(command.trim());
32+
if (commandHistory.size() > MAX_HISTORY) {
33+
commandHistory.remove(0);
34+
}
35+
}
36+
}
1937
}

src/main/java/com/mycmd/commands/CdCommand.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@
44
import com.mycmd.ShellContext;
55
import java.io.File;
66

7+
/**
8+
* Changes the current working directory or displays the current directory path.
9+
*
10+
* This command allows navigation through the file system hierarchy. When called
11+
* without arguments, it prints the absolute path of the current directory. When
12+
* provided with a path argument, it attempts to change to that directory.
13+
*
14+
* Usage:
15+
* - cd : Display current directory
16+
* - cd path : Change to specified path (absolute or relative)
17+
* - cd .. : Navigate to parent directory
18+
*
19+
* The command handles both absolute and relative paths. Relative paths are
20+
* resolved against the current working directory stored in ShellContext.
21+
*/
722
public class CdCommand implements Command {
823
@Override
924
public void execute(String[] args, ShellContext context) {

src/main/java/com/mycmd/commands/ClsCommand.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
import com.mycmd.Command;
44
import com.mycmd.ShellContext;
55

6+
/**
7+
* Clears the console screen by printing multiple blank lines.
8+
*
9+
* This command simulates clearing the terminal screen by outputting 50 blank
10+
* lines, pushing previous content out of view. This is a simple cross-platform
11+
* approach that doesn't rely on terminal-specific control sequences.
12+
*
13+
* Usage: cls
14+
*
15+
* Note: This command does not accept any arguments.
16+
*/
617
public class ClsCommand implements Command {
718
@Override
819
public void execute(String[] args, ShellContext context) {

src/main/java/com/mycmd/commands/ColorCommand.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@
33
import com.mycmd.Command;
44
import com.mycmd.ShellContext;
55

6+
/**
7+
* Changes the console text and background colors using ANSI escape codes.
8+
*
9+
* This command accepts a two-character hexadecimal code where the first digit
10+
* represents the background color and the second digit represents the text color.
11+
* Valid color codes are 0-9 and A-F (hexadecimal). When called without arguments,
12+
* it resets colors to terminal defaults.
13+
*
14+
* Usage:
15+
* - color : Reset to default colors
16+
* - color XY : Set background to X and text to Y (hex digits 0-F)
17+
*
18+
* Example: color 0A sets black background with bright green text.
19+
*
20+
* Note: Background and text colors cannot be the same value.
21+
*/
622
public class ColorCommand implements Command {
723
@Override
824
public void execute(String[] args, ShellContext context) {

src/main/java/com/mycmd/commands/CopyCommand.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44
import com.mycmd.ShellContext;
55
import java.io.*;
66

7+
/**
8+
* Copies a file from source to destination location.
9+
*
10+
* This command reads the contents of a source file and writes it to a
11+
* destination file using buffered I/O streams for efficient copying. Both
12+
* source and destination paths are resolved relative to the current working
13+
* directory unless absolute paths are provided.
14+
*
15+
* Usage: copy source destination
16+
*
17+
* The command verifies that the source exists and is a regular file before
18+
* attempting the copy operation. If the destination file already exists, it
19+
* will be overwritten.
20+
*/
721
public class CopyCommand implements Command {
822
@Override
923
public void execute(String[] args, ShellContext context) {

src/main/java/com/mycmd/commands/DateCommand.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@
44
import com.mycmd.ShellContext;
55
import java.time.LocalDate;
66

7+
/**
8+
* Command that prints the current date to standard output.
9+
*
10+
* <p>This command does not use the provided {@link ShellContext} but keeps the
11+
* parameter to conform to the {@link Command} contract. The output is produced
12+
* using {@link LocalDate#now()} and printed in ISO-8601 format (yyyy-MM-dd).</p>
13+
*/
714
public class DateCommand implements Command {
15+
/**
16+
* Print the current date.
17+
*
18+
* @param args ignored for this command; may be empty or contain unused tokens.
19+
* @param context the current shell context; not used by this command.
20+
*/
821
@Override
922
public void execute(String[] args, ShellContext context) {
1023
System.out.println("The current date is: " + java.time.LocalDate.now());

src/main/java/com/mycmd/commands/DelCommand.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44
import com.mycmd.ShellContext;
55
import java.io.File;
66

7+
/**
8+
* Deletes one or more files from the file system.
9+
*
10+
* This command accepts one or more file names as arguments and attempts to
11+
* delete each file. File paths are resolved relative to the current working
12+
* directory. The command provides feedback for each file indicating whether
13+
* the deletion was successful or if the file was not found.
14+
*
15+
* Usage: del file1 [file2 file3 ...]
16+
*
17+
* Note: This command only deletes files, not directories. Use rmdir for
18+
* directory removal.
19+
*/
720
public class DelCommand implements Command {
821
@Override
922
public void execute(String[] args, ShellContext context) {

src/main/java/com/mycmd/commands/DirCommand.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@
44
import com.mycmd.ShellContext;
55
import java.io.File;
66

7+
/**
8+
* Lists files and directories in the current working directory.
9+
*
10+
* This command displays all files and subdirectories within the current
11+
* directory. Directories are prefixed with angle brackets and "DIR" label,
12+
* while files are displayed with spacing for alignment.
13+
*
14+
* Usage: dir
15+
*
16+
* Output format:
17+
* - Directories: angle-bracket-DIR-angle-bracket followed by directory name
18+
* - Files: Six spaces followed by file name
19+
*
20+
* If the directory is empty or cannot be read, an appropriate message is displayed.
21+
*/
722
public class DirCommand implements Command {
823
@Override
924
public void execute(String[] args, ShellContext context) {

0 commit comments

Comments
 (0)