Skip to content

Commit adfe267

Browse files
LineExtensions and ChildLimits
1 parent 5053309 commit adfe267

File tree

13 files changed

+159
-62
lines changed

13 files changed

+159
-62
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ Use the `ChildLimitBuilder` and `PathMatchers` classes to help you build the lim
219219

220220
```java
221221
// Example: ChildLimitDynamic.java
222-
var childLimit = ChildLimitBuilder.newInstance()
223-
.setDefault(ChildLimitBuilder.UNLIMITED) // Unlimited children by default
222+
var childLimit = ChildLimits.builder()
223+
.setDefault(ChildLimits.UNLIMITED) // Unlimited children by default
224224
.add(PathMatchers.hasName("node_modules"), 0) // Do NOT print any children in "node_modules" folder
225225
.build();
226226
var prettyPrinter = FileTreePrettyPrinter.builder()
@@ -251,13 +251,13 @@ This is useful to annotate your tree with comments, display file sizes, or add d
251251
The function receives the current path and returns an optional string to append (empty string is authorized).
252252
If the function returns `null`, nothing is added.
253253

254-
Use the `LineExtensionBuilder` class to help you build line extension functions.
254+
Use the `LineExtensions` class to help you build line extension functions.
255255

256256
```java
257257
// Example: LineExtension.java
258258
var printedPath = Path.of("src/example/resources/line_extension");
259259

260-
Function<Path, String> lineExtension = LineExtensionBuilder.newInstance()
260+
Function<Path, String> lineExtension = LineExtensions.builder()
261261
.add(PathMatchers.hasRelativePathMatchingGlob(printedPath, "src/main/java/api"), "\t\t\t// All API code: controllers, etc.")
262262
.add(PathMatchers.hasRelativePathMatchingGlob(printedPath, "src/main/java/domain"), "\t\t\t// All domain code: value objects, etc.")
263263
.add(PathMatchers.hasRelativePathMatchingGlob(printedPath, "src/main/java/infra"), "\t\t\t// All infra code: database, email service, etc.")

assets/project-structure.png

19.5 KB
Loading

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

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

3-
import io.github.computerdaddyguy.jfiletreeprettyprinter.ChildLimitBuilder;
3+
import io.github.computerdaddyguy.jfiletreeprettyprinter.ChildLimits;
44
import io.github.computerdaddyguy.jfiletreeprettyprinter.FileTreePrettyPrinter;
55
import io.github.computerdaddyguy.jfiletreeprettyprinter.PathMatchers;
66

77
public class ChildLimitDynamic {
88

99
public static void main(String[] args) {
10-
var childLimit = ChildLimitBuilder.newInstance()
11-
.setDefault(ChildLimitBuilder.UNLIMITED) // Unlimited children by default
10+
var childLimit = ChildLimits.builder()
11+
.setDefault(ChildLimits.UNLIMITED) // Unlimited children by default
1212
.add(PathMatchers.hasName("node_modules"), 0) // Do NOT print any children in "node_modules" folder
1313
.build();
1414
var prettyPrinter = FileTreePrettyPrinter.builder()

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

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

33
import io.github.computerdaddyguy.jfiletreeprettyprinter.FileTreePrettyPrinter;
4-
import io.github.computerdaddyguy.jfiletreeprettyprinter.LineExtensionBuilder;
4+
import io.github.computerdaddyguy.jfiletreeprettyprinter.LineExtensions;
55
import io.github.computerdaddyguy.jfiletreeprettyprinter.PathMatchers;
66
import java.nio.file.Path;
77
import java.util.function.Function;
@@ -11,7 +11,7 @@ public class LineExtension {
1111
public static void main(String[] args) {
1212
var printedPath = Path.of("src/example/resources/line_extension");
1313

14-
Function<Path, String> lineExtension = LineExtensionBuilder.newInstance()
14+
Function<Path, String> lineExtension = LineExtensions.builder()
1515
.add(PathMatchers.hasRelativePathMatchingGlob(printedPath, "src/main/java/api"), "\t\t\t// All API code: controllers, etc.")
1616
.add(PathMatchers.hasRelativePathMatchingGlob(printedPath, "src/main/java/domain"), "\t\t\t// All domain code: value objects, etc.")
1717
.add(PathMatchers.hasRelativePathMatchingGlob(printedPath, "src/main/java/infra"), "\t\t\t// All infra code: database, email service, etc.")

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

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

3-
import io.github.computerdaddyguy.jfiletreeprettyprinter.ChildLimitBuilder;
3+
import io.github.computerdaddyguy.jfiletreeprettyprinter.ChildLimits;
44
import io.github.computerdaddyguy.jfiletreeprettyprinter.FileTreePrettyPrinter;
5-
import io.github.computerdaddyguy.jfiletreeprettyprinter.LineExtensionBuilder;
5+
import io.github.computerdaddyguy.jfiletreeprettyprinter.LineExtensions;
66
import io.github.computerdaddyguy.jfiletreeprettyprinter.PathMatchers;
77
import io.github.computerdaddyguy.jfiletreeprettyprinter.PathSorts;
88
import java.nio.file.Path;
@@ -54,7 +54,7 @@ public static void main(String[] args) {
5454
/*
5555
* Limit the number of displayed children by directory: some content is not relevant and clutters the final result!
5656
*/
57-
var childLimitFunction = ChildLimitBuilder.newInstance()
57+
var childLimitFunction = ChildLimits.builder()
5858
// Hide all files under renderer and scanner packages
5959
.add(PathMatchers.hasAbsolutePathMatchingGlob("**/io/github/computerdaddyguy/jfiletreeprettyprinter/renderer"), 0)
6060
.add(PathMatchers.hasAbsolutePathMatchingGlob("**/io/github/computerdaddyguy/jfiletreeprettyprinter/scanner"), 0)
@@ -64,7 +64,7 @@ public static void main(String[] args) {
6464
/*
6565
* Add some comments on a few files and directories
6666
*/
67-
Function<Path, String> lineExtension = LineExtensionBuilder.newInstance()
67+
Function<Path, String> lineExtension = LineExtensions.builder()
6868
.add(PathMatchers.hasName("project-structure.png"), "\t// This image")
6969
.add(PathMatchers.hasName("FileTreePrettyPrinter.java"), "\t// Main entry point")
7070
.add(PathMatchers.hasName("README.md"), "\t\t// You're reading at this!")
@@ -118,7 +118,7 @@ public static void main(String[] args) {
118118
│ ├─ 📂 scanner/
119119
│ │ └─ ... (4 files skipped)
120120
│ ├─ ☕ FileTreePrettyPrinter.java // Main entry point
121-
│ └─ ... (8 files skipped)
121+
│ └─ ... (10 files skipped)
122122
├─ 🗺️ CHANGELOG.md
123123
├─ 📖 CONTRIBUTING.md
124124
├─ 📄 LICENSE

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,14 @@
3838
@NullMarked
3939
public class ChildLimitBuilder {
4040

41-
/**
42-
* Special value indicating unlimited children ({@code -1}).
43-
*/
44-
public static final int UNLIMITED = -1;
45-
4641
private List<ToIntFunction<Path>> limits;
4742
private int defaultLimit;
4843

49-
private ChildLimitBuilder(int defaultLimit) {
44+
/* package */ ChildLimitBuilder(int defaultLimit) {
5045
this.limits = new ArrayList<>();
5146
this.defaultLimit = defaultLimit;
5247
}
5348

54-
/**
55-
* Returns a new {@link ChildLimitBuilder}.
56-
*
57-
* @return a fresh builder instance
58-
*/
59-
public static ChildLimitBuilder newInstance() {
60-
return newInstance(UNLIMITED);
61-
}
62-
63-
public static ChildLimitBuilder newInstance(int defaultLimit) {
64-
return new ChildLimitBuilder(defaultLimit);
65-
}
66-
6749
/**
6850
* Builds the final child limit function based on the configured rules.
6951
* <p>
@@ -134,7 +116,7 @@ public ChildLimitBuilder add(ToIntFunction<Path> childLimit) {
134116
*/
135117
public ChildLimitBuilder add(PathMatcher pathMatcher, int limit) {
136118
Objects.requireNonNull(pathMatcher, "pathMatcher is null");
137-
return add(path -> pathMatcher.matches(path) ? limit : UNLIMITED);
119+
return add(path -> pathMatcher.matches(path) ? limit : ChildLimits.UNLIMITED);
138120
}
139121

140122
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package io.github.computerdaddyguy.jfiletreeprettyprinter;
2+
3+
/**
4+
* Utility class providing constants and factory methods for creating
5+
* {@link ChildLimitBuilder} instances.
6+
* <p>
7+
* A child limit defines how many entries (files or directories) are allowed
8+
* under a given directory path when building or rendering a file tree.
9+
* </p>
10+
*
11+
* <p>
12+
* This class also exposes the {@link #UNLIMITED} constant, representing
13+
* an unrestricted number of children.
14+
* </p>
15+
*
16+
* <h2>Example usage:</h2>
17+
* <pre>{@code
18+
* var limits = ChildLimits.builder()
19+
* .add(PathMatchers.hasName("bin"), 0) // Hide "bin" folder contents
20+
* .add(PathMatchers.hasName("src"), 20) // Limit to 20 children
21+
* .build();
22+
* }</pre>
23+
*
24+
* @see ChildLimitBuilder
25+
*/
26+
public final class ChildLimits {
27+
28+
/**
29+
* Special value indicating unlimited children ({@code -1}).
30+
*/
31+
public static final int UNLIMITED = -1;
32+
33+
private ChildLimits() {
34+
// Helper class
35+
}
36+
37+
/**
38+
* Returns a new {@link ChildLimitBuilder} with the default limit
39+
* set to {@link #UNLIMITED}.
40+
*
41+
* @return a fresh builder instance
42+
*/
43+
public static ChildLimitBuilder builder() {
44+
return builder(UNLIMITED);
45+
}
46+
47+
/**
48+
* Returns a new {@link ChildLimitBuilder} with the given default limit.
49+
* <p>
50+
* The default limit applies when no specific rule matches a given path.
51+
* </p>
52+
*
53+
* @param defaultLimit the default limit (use {@link #UNLIMITED} for no restriction)
54+
* @return a fresh builder instance
55+
*/
56+
public static ChildLimitBuilder builder(int defaultLimit) {
57+
return new ChildLimitBuilder(defaultLimit);
58+
}
59+
60+
}

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

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,12 @@
3838
@NullMarked
3939
public class LineExtensionBuilder {
4040

41-
private static final String NO_EXTENSION = null;
42-
private static final String LINE_BREAK_EXTENSION = "";
43-
4441
private List<Function<Path, String>> extensions;
4542

46-
private LineExtensionBuilder() {
43+
/* package */ LineExtensionBuilder() {
4744
this.extensions = new ArrayList<>();
4845
}
4946

50-
/**
51-
* Returns a new {@link LineExtensionBuilder}.
52-
*
53-
* @return a fresh builder instance
54-
*/
55-
public static LineExtensionBuilder newInstance() {
56-
return new LineExtensionBuilder();
57-
}
58-
5947
/**
6048
* Builds the final function mapping a {@link Path} to an extension string.
6149
* <p>
@@ -68,10 +56,10 @@ public static LineExtensionBuilder newInstance() {
6856
public Function<Path, String> build() {
6957
var immutExtensions = List.copyOf(extensions);
7058
return path -> {
71-
String result = NO_EXTENSION;
59+
String result = LineExtensions.NO_EXTENSION;
7260
for (var rule : immutExtensions) {
7361
result = rule.apply(path);
74-
if (!Objects.equals(result, NO_EXTENSION)) {
62+
if (!Objects.equals(result, LineExtensions.NO_EXTENSION)) {
7563
break;
7664
}
7765
}
@@ -111,7 +99,7 @@ public LineExtensionBuilder add(Function<Path, String> lineExtension) {
11199
*/
112100
public LineExtensionBuilder add(PathMatcher pathMatcher, String extension) {
113101
Objects.requireNonNull(pathMatcher, "pathMatcher is null");
114-
return add(path -> pathMatcher.matches(path) ? extension : NO_EXTENSION);
102+
return add(path -> pathMatcher.matches(path) ? extension : LineExtensions.NO_EXTENSION);
115103
}
116104

117105
/**
@@ -122,7 +110,7 @@ public LineExtensionBuilder add(PathMatcher pathMatcher, String extension) {
122110
* @return this builder (for chaining)
123111
*/
124112
public LineExtensionBuilder addLineBreak(PathMatcher pathMatcher) {
125-
return add(pathMatcher, LINE_BREAK_EXTENSION);
113+
return add(pathMatcher, LineExtensions.LINE_BREAK_EXTENSION);
126114
}
127115

128116
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package io.github.computerdaddyguy.jfiletreeprettyprinter;
2+
3+
/**
4+
* Utility class providing constants and factory methods for creating
5+
* {@link LineExtensionBuilder} instances used to define per-path line extensions
6+
* in pretty-printed file trees.
7+
* <p>
8+
* Line extensions are optional text fragments appended to specific lines
9+
* in the printed tree. They can be used to add comments, annotations, or
10+
* formatting cues.
11+
* </p>
12+
*
13+
* <p>
14+
* Two special constants are provided:
15+
* </p>
16+
* <ul>
17+
* <li>{@link #NO_EXTENSION} — indicates no extension for a given path.</li>
18+
* <li>{@link #LINE_BREAK_EXTENSION} — indicates a forced line break,
19+
* useful for splitting compact directory chains.</li>
20+
* </ul>
21+
*
22+
* <h2>Example usage:</h2>
23+
* <pre>{@code
24+
* var lineExt = LineExtensions.builder()
25+
* .add(PathMatchers.hasName("README.md"), "\t// Project documentation")
26+
* .addLineBreak(PathMatchers.hasRelativePathMatchingGlob(".", "src/main/java"))
27+
* .build();
28+
* }</pre>
29+
*
30+
* @see LineExtensionBuilder
31+
*/
32+
public final class LineExtensions {
33+
34+
/** Indicates that no extension should be applied to the line ({@code null}). */
35+
public static final String NO_EXTENSION = null;
36+
37+
/** Indicates a forced line break (empty string). */
38+
public static final String LINE_BREAK_EXTENSION = "";
39+
40+
private LineExtensions() {
41+
// Helper class
42+
}
43+
44+
/**
45+
* Returns a new {@link LineExtensionBuilder}.
46+
*
47+
* @return a fresh builder instance
48+
*/
49+
public static LineExtensionBuilder builder() {
50+
return new LineExtensionBuilder();
51+
}
52+
53+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ private PathSorts() {
2323
* Comparator that sorts paths by file size in ascending order.
2424
* Directories are treated as having a size of {@code 0}, so they appear before regular files.
2525
*/
26-
public static final Comparator<Path> BY_FILE_SIZE = Comparator.comparing((Path path) -> {
27-
return PathMatchers.isDirectory().matches(path) ? 0 : path.toFile().length();
28-
});
26+
public static final Comparator<Path> BY_FILE_SIZE = Comparator.comparing(
27+
(Path path) -> PathMatchers.isDirectory().matches(path) ? 0 : path.toFile().length()
28+
);
2929

3030
/**
3131
* Comparator that places directories before files.

0 commit comments

Comments
 (0)