forked from Drive-for-Java/MyCMD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirCommand.java
More file actions
44 lines (40 loc) · 1.25 KB
/
Copy pathDirCommand.java
File metadata and controls
44 lines (40 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.mycmd.commands;
import com.mycmd.Command;
import com.mycmd.ShellContext;
import java.io.File;
/**
* Lists files and directories in the current working directory.
*
* This command displays all files and subdirectories within the current
* directory. Directories are prefixed with angle brackets and "DIR" label,
* while files are displayed with spacing for alignment.
*
* Usage: dir
*
* Output format:
* - Directories: angle-bracket-DIR-angle-bracket followed by directory name
* - Files: Six spaces followed by file name
*
* If the directory is empty or cannot be read, an appropriate message is displayed.
*/
public class DirCommand implements Command {
@Override
public void execute(String[] args, ShellContext context) {
File[] files = context.getCurrentDir().listFiles();
if (files == null || files.length == 0) {
System.out.println("No files found.");
return;
}
for (File f : files) {
System.out.println((f.isDirectory() ? "<DIR> " : " ") + f.getName());
}
}
@Override
public String description() {
return "Display the contents of working directory.";
}
@Override
public String usage() {
return "dir";
}
}