package com.claudecode.command.impl; import com.claudecode.command.CommandContext; import com.claudecode.command.BaseSlashCommand; import com.claudecode.console.AnsiStyle; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.stream.Stream; /** * /files 命令 —— 列出当前工作目录的文件结构。 *
* 对应 claude-code/src/commands/files.ts。
* 显示项目目录树(默认2层深度)。
*/
public class FilesCommand extends BaseSlashCommand {
@Override
public String name() {
return "files";
}
@Override
public String description() {
return "List project files. Use /files [depth] to control depth (default: 2)";
}
@Override
public String execute(String args, CommandContext context) {
Path projectDir = Path.of(System.getProperty("user.dir"));
int maxDepth = 2;
if (args != null && !args.isBlank()) {
try {
maxDepth = Integer.parseInt(args.strip());
maxDepth = Math.max(1, Math.min(maxDepth, 5));
} catch (NumberFormatException ignored) {
// Treat as path filter
return listFiltered(projectDir, args.strip());
}
}
StringBuilder sb = new StringBuilder();
sb.append("\n ").append(AnsiStyle.bold("📁 " + projectDir.getFileName())).append("\n");
sb.append(" ").append("─".repeat(50)).append("\n");
try {
int[] counts = {0, 0}; // [files, dirs]
listDirectory(sb, projectDir, "", maxDepth, 0, counts);
sb.append(" ").append("─".repeat(50)).append("\n");
sb.append(" ").append(AnsiStyle.dim(counts[1] + " directories, " + counts[0] + " files")).append("\n");
} catch (IOException e) {
sb.append(" ").append(AnsiStyle.red("Error: " + e.getMessage())).append("\n");
}
return sb.toString();
}
private void listDirectory(StringBuilder sb, Path dir, String indent, int maxDepth, int depth, int[] counts) throws IOException {
if (depth >= maxDepth) return;
try (Stream