Skip to content

Commit

Permalink
Add --color
Browse files Browse the repository at this point in the history
  • Loading branch information
alban-auzeill committed Sep 27, 2020
1 parent 3ac8d7a commit ae779f3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/main/java/com/auzeill/file/StatContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,20 @@ public class StatContext {
private static final String IGNORE = "--ignore";
private static final String SAVE = "--save";
private static final String DIFF = "--diff";
private static final String COLOR = "--color";

public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_RED = "\u001B[31m";

public final Path baseDirectory;
public final Path statsDirectory;
public final Path rootPath;
public final boolean computeSha1;
public final boolean save;
public final boolean diff;
public final boolean color;
public final Set<String> ignoreSet;
public final Map<String, FileAttributes> lastFileAttributesMap;
public final List<String> previousPathsToDiff;
Expand All @@ -52,6 +59,7 @@ public StatContext(Path baseDirectory, Path rootPath, boolean computeSha1) {
this.computeSha1 = computeSha1;
this.save = false;
this.diff = false;
this.color = false;
this.ignoreSet = new HashSet<>();
this.lastFileAttributesMap = new HashMap<>();
this.previousPathsToDiff = Collections.emptyList();
Expand All @@ -62,6 +70,7 @@ public StatContext(String[] args) throws IOException {
this.computeSha1 = !arguments.remove(NO_SHA1);
this.save = arguments.remove(SAVE);
this.diff = arguments.remove(DIFF);
this.color = arguments.remove(COLOR);
this.ignoreSet = new HashSet<>();
int ignorePos = arguments.lastIndexOf(IGNORE);
while (ignorePos != -1 && ignorePos + 1 < arguments.size()) {
Expand Down Expand Up @@ -145,7 +154,11 @@ public void printStats(PrintStream out, FileAttributes attributes) {
FileAttributes prevAttributes = this.lastFileAttributesMap.get(previousIterator.next());
int comp = pathToSort(prevAttributes.relativeLinuxPath).compareTo(pathToSort(attributes.relativeLinuxPath));
if (comp < 0) {
out.println("-del- " + prevAttributes.toString());
if (color) {
out.println(ANSI_RED + "-del- " + prevAttributes.toString() + ANSI_RESET);
} else {
out.println("-del- " + prevAttributes.toString());
}
previousIterator.remove();
} else if (comp == 0) {
compared = true;
Expand Down Expand Up @@ -174,14 +187,22 @@ public void printStats(PrintStream out, FileAttributes attributes) {
if (!prevAttributes.sha1OrSymbolicLink.equals(attributes.sha1OrSymbolicLink)) {
diffLine.append(" sha1OrSymbolicLink ").append(prevAttributes.sha1OrSymbolicLink).append(" -> ").append(attributes.sha1OrSymbolicLink).append(" |");
}
out.println(diffLine.toString());
if (color) {
out.println(ANSI_BLUE + diffLine.toString() + ANSI_RESET);
} else {
out.println(diffLine.toString());
}
}
} else {
break;
}
}
if (!compared) {
out.println("+new+ " + attributes.toString());
if (color) {
out.println(ANSI_GREEN + "+new+ " + attributes.toString() + ANSI_RESET);
} else {
out.println("+new+ " + attributes.toString());
}
}
} else {
out.println(attributes.toString());
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/auzeill/file/StatsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ void diff_from_previous(@TempDir Path tempDir) throws IOException {
"~mod~ f3| size 4 -> 15 | modifiedTime 2020-09-02T15:43:48.680382Z -> 2020-09-02T15:43:48.680382Z | sha1OrSymbolicLink cca5603cae64651971a35bc1488f0d23ddabdff9 -> 1fa817e97796161063e307eac706bb8b06cf956c |" + System.lineSeparator() +
"+new+ f4|f|16|alban|alban|rw-r--r--|2020-09-02T15:43:48.680382Z|17c494d126c27755e2134a4388178d808b139ce9" + System.lineSeparator() +
"~mod~ .| size 16 -> 39 | modifiedTime 2020-09-02T15:43:48.680382Z -> 2020-09-02T15:43:48.680382Z | sha1OrSymbolicLink f81b3e16656c1bd84d7522ed2f83fa996ffd8497 -> 2d8050016ac5619885aaac0402f8c5d88cceb077 |" + System.lineSeparator());

out = new ByteArrayOutputStream();
stream = new PrintStream(out, true, UTF_8);
Stats.stats(stream, new String[] { "--diff", "--color", tempDir.toString() });
assertThat(FileAttributesTest.forceSysFields(new String(out.toByteArray(), UTF_8)))
.isEqualTo("" +
StatContext.ANSI_RED + "-del- f2|f|4|alban|alban|rw-r--r--|2020-09-02T15:43:48.680382Z|2aed8aa9f826c21ef07d5ee15b48eea06e9c8a62" + StatContext.ANSI_RESET + System.lineSeparator() +
StatContext.ANSI_BLUE + "~mod~ f3| size 4 -> 15 | modifiedTime 2020-09-02T15:43:48.680382Z -> 2020-09-02T15:43:48.680382Z | sha1OrSymbolicLink cca5603cae64651971a35bc1488f0d23ddabdff9 -> 1fa817e97796161063e307eac706bb8b06cf956c |" + StatContext.ANSI_RESET + System.lineSeparator() +
StatContext.ANSI_GREEN + "+new+ f4|f|16|alban|alban|rw-r--r--|2020-09-02T15:43:48.680382Z|17c494d126c27755e2134a4388178d808b139ce9" + StatContext.ANSI_RESET + System.lineSeparator() +
StatContext.ANSI_BLUE + "~mod~ .| size 16 -> 39 | modifiedTime 2020-09-02T15:43:48.680382Z -> 2020-09-02T15:43:48.680382Z | sha1OrSymbolicLink f81b3e16656c1bd84d7522ed2f83fa996ffd8497 -> 2d8050016ac5619885aaac0402f8c5d88cceb077 |" + StatContext.ANSI_RESET + System.lineSeparator());
}

}

0 comments on commit ae779f3

Please sign in to comment.