package com.webcodepro.applecommander.ui; import java.io.IOException; import java.io.UncheckedIOException; import java.util.List; import java.util.stream.Collectors; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.webcodepro.applecommander.storage.Disk; import com.webcodepro.applecommander.storage.DiskUnrecognizedException; import com.webcodepro.applecommander.storage.FormattedDisk; import com.webcodepro.applecommander.storage.FormattedDisk.FileColumnHeader; import com.webcodepro.applecommander.util.TextBundle; import com.webcodepro.applecommander.util.filestreamer.FileStreamer; import com.webcodepro.applecommander.util.filestreamer.FileTuple; public class DirectoryLister { private static TextBundle textBundle = UiBundle.getInstance(); public static DirectoryLister text(int display) { return new DirectoryLister(new TextListingStrategy(display)); } public static DirectoryLister csv(int display) { try { return new DirectoryLister(new CsvListingStrategy(display)); } catch (IOException e) { throw new UncheckedIOException(e); } } public static DirectoryLister json(int display) { return new DirectoryLister(new JsonListingStrategy(display)); } private ListingStrategy strategy; private DirectoryLister(ListingStrategy strategy) { this.strategy = strategy; } public void list(String filename) throws DiskUnrecognizedException, IOException { Disk disk = new Disk(filename); strategy.first(disk); FileStreamer.forDisk(disk) .recursive(true) .includeDeleted(false) .beforeDisk(strategy::beforeDisk) .afterDisk(strategy::afterDisk) .stream() .forEach(strategy::forEach); strategy.last(disk); } private static abstract class ListingStrategy { protected int display; protected ListingStrategy(int display) { this.display = display; } protected void first(Disk d) {}; protected void beforeDisk(FormattedDisk d) {} protected void afterDisk(FormattedDisk d) {} protected void forEach(FileTuple f) {} protected void last(Disk d) {}; } private static class TextListingStrategy extends ListingStrategy { protected TextListingStrategy(int display) { super(display); } protected void beforeDisk(FormattedDisk disk) { System.out.printf("%s %s\n", disk.getFilename(), disk.getDiskName()); } protected void afterDisk(FormattedDisk disk) { System.out.printf("%s\n\n", textBundle.format("CommandLineStatus", disk.getFormat(), disk.getFreeSpace(), disk.getUsedSpace())); } protected void forEach(FileTuple tuple) { System.out.printf("%s%s\n", repeat(" ", tuple.paths.size()), String.join(" ", tuple.fileEntry.getFileColumnData(display))); } private String repeat(String s, int n) { StringBuilder sb = new StringBuilder(); for (int i=0; i headers = tuple.formattedDisk.getFileColumnHeaders(display); List columns = tuple.fileEntry.getFileColumnData(display); for (int i=0; i