Skip to content

Commit bc5a751

Browse files
Filtering
1 parent 69babec commit bc5a751

File tree

33 files changed

+959
-75
lines changed

33 files changed

+959
-75
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [0.0.3] - Unreleased
99

10+
### Added
11+
- Filtering
12+
1013
### Changed
1114
- Sorting: option method `withFileSort` renamed in `sort`
15+
- `PathPredicates` help class now returns instances of `PathPredicate` to make predicate recursive
1216

1317

1418
## [0.0.2] - 2025-09-16

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=ComputerDaddyGuy_JFileTreePrettyPrinter&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=ComputerDaddyGuy_JFileTreePrettyPrinter)
33

44
A lightweight Java library for printing directory structures in a clean, tree-like format.
5-
- Various styles for tree rendering
5+
- Sorting & filtering
66
- Emoji support 🎉
77
- Limit displayed children (fixed value or dynamically)
88
- Compact directory chains
99
- Maximum depth
10-
- Sorting
10+
- Various styles for tree rendering
1111

1212
> [!CAUTION]
1313
> This lib was developed just for fun, and has not been thoroughly tested!
@@ -75,6 +75,7 @@ base/
7575
* [Compact directories](#compact-directories)
7676
* [Max depth](#max-depth)
7777
* [Sorting](#sorting)
78+
* [Filtering](#filtering)
7879

7980
## Tree format
8081
Choose between different tree formats.
@@ -252,6 +253,32 @@ sorting/
252253
└─ y_file
253254
```
254255

256+
## Filtering
257+
Files and directories can be selectively included or excluded using a custom `Predicate<Path>`.
258+
259+
Filtering is **recursive by default**: directory's contents will always be traversed.
260+
However, if a directory does not match and none of its children match, the directory itself will not be displayed.
261+
262+
The `PathPredicates` class provides several ready-to-use `Predicate<Path>` implementations for common cases, as well as a builder for creating more advanced predicates.
263+
264+
```java
265+
// Example: Filtering.java
266+
var filter = PathPredicates.hasExtension("java");
267+
var tree = FileTreePrettyPrinter.createDefault()
268+
.prettyPrint("src/example/resources/filtering", filter);
269+
```
270+
```
271+
filtering/
272+
├─ dir_with_java_files/
273+
│ ├─ file_B.java
274+
│ └─ file_E.java
275+
├─ dir_with_nested_java_files/
276+
│ └─ nested_dir_with_java_files/
277+
│ ├─ file_G.java
278+
│ └─ file_J.java
279+
└─ file_A.java
280+
```
281+
255282
# Changelog
256283
See [CHANGELOG.md](CHANGELOG.md) for released versions.
257284

ROADMAP.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Roadmap
22

33
## Initial version
4-
54
- [x] Option: tree format Unicode box drawing / classic ASCII
65
- [x] Option: use emojis
76
- [x] Option: children limit (static & dynamic)
@@ -14,17 +13,22 @@
1413
- [x] Pre-defined Path predicates
1514
- [x] Publish on Maven Central!
1615

17-
## Next version
16+
## Next version(s)
1817
- [ ] Directory children limitation function helper
1918
- [x] More default emojis
20-
- [ ] Filtering
19+
- [x] Filtering
2120
- [x] Ordering
2221

2322
## Other ideas
23+
- [ ] Use Github wiki to document options instead of readme
2424
- [ ] Custom tree format option
2525
- [ ] Custom emojis option
26+
- [ ] Additional text on lines
2627
- [ ] Color option
2728
- [ ] Follow symlink option
28-
- [ ] Print optional legend for symlink/other file types symbols (at the end of the tree)
29-
- [ ] File attributes LineRenderer (size, author, createAt, etc.)
3029
- [ ] Refactor unit tests (custom assert?)
30+
31+
## Abandoned ideas
32+
These ideas will likely not been implemented because they do not align with JFileTreePrettyPrint vision:
33+
- File attributes LineRenderer (size, author, createAt, etc.)
34+
- Print optional legend for symlink/other file types symbols (at the end of the tree)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package io.github.computerdaddyguy.jfiletreeprettyprinter.example;
22

33
import io.github.computerdaddyguy.jfiletreeprettyprinter.FileTreePrettyPrinter;
4-
import io.github.computerdaddyguy.jfiletreeprettyprinter.PathPredicates;
4+
import io.github.computerdaddyguy.jfiletreeprettyprinter.PathUtils;
55
import java.nio.file.Path;
66
import java.util.function.ToIntFunction;
77

88
public class ChildrenLimitDynamic {
99

1010
public static void main(String[] args) {
11-
ToIntFunction<Path> pathLimitFunction = path -> PathPredicates.hasName(path, "node_modules") ? 0 : -1; // Negative value means "no limit"
11+
ToIntFunction<Path> pathLimitFunction = path -> PathUtils.hasName(path, "node_modules") ? 0 : -1; // Negative value means "no limit"
1212
var prettyPrinter = FileTreePrettyPrinter.builder()
1313
.customizeOptions(options -> options.withChildrenLimitFunction(pathLimitFunction))
1414
.build();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.github.computerdaddyguy.jfiletreeprettyprinter.example;
2+
3+
import io.github.computerdaddyguy.jfiletreeprettyprinter.FileTreePrettyPrinter;
4+
import io.github.computerdaddyguy.jfiletreeprettyprinter.PathPredicates;
5+
6+
public class Filtering {
7+
8+
public static void main(String[] args) {
9+
var filter = PathPredicates.hasExtension("java");
10+
var prettyPrinter = FileTreePrettyPrinter.createDefault();
11+
var tree = prettyPrinter.prettyPrint("src/example/resources/filtering", filter);
12+
System.out.println(tree);
13+
}
14+
15+
}

src/example/resources/filtering/dir_with_java_files/file_B.java

Whitespace-only changes.

src/example/resources/filtering/dir_with_java_files/file_C.cpp

Whitespace-only changes.

src/example/resources/filtering/dir_with_java_files/file_D.ts

Whitespace-only changes.

src/example/resources/filtering/dir_with_java_files/file_E.java

Whitespace-only changes.

src/example/resources/filtering/dir_with_nested_java_files/nested_dir_with_java_files/file_G.java

Whitespace-only changes.

0 commit comments

Comments
 (0)