Skip to content

Commit 3692d69

Browse files
Work in progress
1 parent 60fe8a8 commit 3692d69

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

src/example/java/io/github/computerdaddyguy/jfiletreeprettyprinter/example/CompleteExample.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public static void main(String[] args) {
1313

1414
var filterDir = PathPredicates.builder()
1515
.pathTest(path -> !PathPredicates.hasName(path, ".git"))
16-
.pathTest(path -> !PathPredicates.hasFullPathMatchingGlob(path, "**/.git"))
17-
.pathTest(path -> !PathPredicates.hasFullPathMatchingGlob(path, "**/.github"))
18-
.pathTest(path -> !PathPredicates.hasFullPathMatchingGlob(path, "**/.settings"))
19-
.pathTest(path -> !PathPredicates.hasFullPathMatchingGlob(path, "**/src/example"))
20-
.pathTest(path -> !PathPredicates.hasFullPathMatchingGlob(path, "**/src/test"))
21-
.pathTest(path -> !PathPredicates.hasFullPathMatchingGlob(path, "**/target"))
16+
.pathTest(path -> !PathPredicates.hasFullPathMatchingGlob(path, "./.git"))
17+
.pathTest(path -> !PathPredicates.hasFullPathMatchingGlob(path, "./.github"))
18+
.pathTest(path -> !PathPredicates.hasFullPathMatchingGlob(path, "./.settings"))
19+
.pathTest(path -> !PathPredicates.hasFullPathMatchingGlob(path, "./src/example"))
20+
.pathTest(path -> !PathPredicates.hasFullPathMatchingGlob(path, "./src/test"))
21+
.pathTest(path -> !PathPredicates.hasFullPathMatchingGlob(path, "./target"))
2222
.build();
2323

2424
var filterFiles = PathPredicates.builder()

src/main/java/io/github/computerdaddyguy/jfiletreeprettyprinter/PathPredicates.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public static boolean hasFullPathMatchingGlob(Path path, String glob) {
6666
return true;
6767
}
6868
var matcher = path.getFileSystem().getPathMatcher("glob:" + glob);
69-
return matcher.matches(path);
69+
var result = matcher.matches(path);
70+
return result;
7071
}
7172

7273
/**

src/main/java/io/github/computerdaddyguy/jfiletreeprettyprinter/scanner/DefaultPathToTreeScanner.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.computerdaddyguy.jfiletreeprettyprinter.scanner;
22

3+
import io.github.computerdaddyguy.jfiletreeprettyprinter.PathPredicates;
34
import io.github.computerdaddyguy.jfiletreeprettyprinter.scanner.TreeEntry.DirectoryEntry;
45
import io.github.computerdaddyguy.jfiletreeprettyprinter.scanner.TreeEntry.FileEntry;
56
import io.github.computerdaddyguy.jfiletreeprettyprinter.scanner.TreeEntry.MaxDepthReachEntry;
@@ -30,18 +31,18 @@ public DefaultPathToTreeScanner(ScanningOptions options) {
3031

3132
@Override
3233
public TreeEntry scan(Path fileOrDir) {
33-
return handle(0, fileOrDir, options.pathFilter());
34+
return handle(fileOrDir, 0, fileOrDir.relativize(fileOrDir).resolve("."), options.pathFilter());
3435
}
3536

3637
@Nullable
37-
private TreeEntry handle(int depth, Path fileOrDir, Predicate<Path> filter) {
38-
return fileOrDir.toFile().isDirectory()
39-
? handleDirectory(depth, fileOrDir, filter)
38+
private TreeEntry handle(Path root, int depth, Path fileOrDir, Predicate<Path> filter) {
39+
return PathPredicates.isDirectory(fileOrDir)
40+
? handleDirectory(root, depth, fileOrDir, filter)
4041
: handleFile(fileOrDir);
4142
}
4243

4344
@Nullable
44-
private TreeEntry handleDirectory(int depth, Path dir, Predicate<Path> filter) {
45+
private TreeEntry handleDirectory(Path root, int depth, Path dir, Predicate<Path> filter) {
4546

4647
if (depth >= options.getMaxDepth()) {
4748
var maxDepthEntry = new MaxDepthReachEntry(depth);
@@ -50,17 +51,17 @@ private TreeEntry handleDirectory(int depth, Path dir, Predicate<Path> filter) {
5051

5152
List<TreeEntry> childEntries;
5253

53-
try (var childrenStream = Files.newDirectoryStream(dir)) {
54+
try (var childrenStream = Files.newDirectoryStream(dir, path -> filter.test(path))) {
5455
var it = directoryStreamToIterator(childrenStream, filter);
55-
childEntries = handleDirectoryChildren(depth, dir, it, filter);
56+
childEntries = handleDirectoryChildren(root, depth, dir, it, filter);
5657
} catch (IOException e) {
5758
throw new UncheckedIOException("Unable to list files for directory: " + dir, e);
5859
}
5960

6061
return new DirectoryEntry(dir, childEntries);
6162
}
6263

63-
private List<TreeEntry> handleDirectoryChildren(int depth, Path dir, Iterator<Path> pathIterator, Predicate<Path> filter) {
64+
private List<TreeEntry> handleDirectoryChildren(Path root, int depth, Path dir, Iterator<Path> pathIterator, Predicate<Path> filter) {
6465

6566
var childEntries = new ArrayList<TreeEntry>();
6667
int maxChildEntries = options.getChildLimit().applyAsInt(dir);
@@ -73,7 +74,7 @@ private List<TreeEntry> handleDirectoryChildren(int depth, Path dir, Iterator<Pa
7374
break;
7475
}
7576
var child = pathIterator.next();
76-
var childEntry = handle(depth + 1, child, filter);
77+
var childEntry = handle(root, depth + 1, child, filter);
7778
if (childEntry == null) {
7879
childCount--; // The child did not pass the filter, so it doesn't count
7980
} else {
@@ -83,19 +84,19 @@ private List<TreeEntry> handleDirectoryChildren(int depth, Path dir, Iterator<Pa
8384

8485
// Loop has early exit?
8586
if (pathIterator.hasNext()) {
86-
childEntries.addAll(handleLeftOverChildren(depth, pathIterator, filter));
87+
childEntries.addAll(handleLeftOverChildren(root, depth, pathIterator, filter));
8788
}
8889

8990
return childEntries;
9091
}
9192

92-
private List<TreeEntry> handleLeftOverChildren(int depth, Iterator<Path> pathIterator, Predicate<Path> filter) {
93+
private List<TreeEntry> handleLeftOverChildren(Path root, int depth, Iterator<Path> pathIterator, Predicate<Path> filter) {
9394
var childEntries = new ArrayList<TreeEntry>();
9495

9596
var skippedChildren = new ArrayList<Path>();
9697
while (pathIterator.hasNext()) {
9798
var child = pathIterator.next();
98-
var childEntry = handle(depth + 1, child, filter);
99+
var childEntry = handle(root, depth + 1, child, filter);
99100
if (childEntry != null) { // Is null if no children file is retained by filter
100101
skippedChildren.add(child);
101102
}
@@ -111,7 +112,6 @@ private List<TreeEntry> handleLeftOverChildren(int depth, Iterator<Path> pathIte
111112
private Iterator<Path> directoryStreamToIterator(DirectoryStream<Path> childrenStream, Predicate<Path> filter) {
112113
return StreamSupport
113114
.stream(childrenStream.spliterator(), false)
114-
.filter(filter)
115115
.sorted(options.pathComparator())
116116
.iterator();
117117
}

0 commit comments

Comments
 (0)