Skip to content

Commit b63e1fb

Browse files
More CLI external features
1 parent 5f05047 commit b63e1fb

File tree

15 files changed

+314
-29
lines changed

15 files changed

+314
-29
lines changed

src/main/java/io/github/computerdaddyguy/jfiletreeprettyprinter/cli/RecordUtils.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,15 @@ public static String toTextBlock(@Nullable Record rec) {
2525
return String.join(System.lineSeparator(), lines);
2626
}
2727

28-
private static void toTextBlock(List<String> lines, String shift, @Nullable String attrName, Object value, String indent) {
28+
private static void toTextBlock(List<String> lines, String shift, @Nullable String attrName, @Nullable Object value, String indent) {
2929
if (value == null) {
30-
return;
31-
}
32-
var nested = value.getClass();
33-
if (nested.isRecord()) {
30+
return; // do not print null objects, just ignore them
31+
} else if (value.getClass().isRecord()) {
3432
toTextBlockRecord(lines, shift, attrName, (Record) value, indent);
3533
} else if (value instanceof Collection<?> coll) {
3634
toTextBlockCollection(lines, shift, attrName, coll, indent);
3735
} else {
38-
lines.add(String.format("%s%s = %s", shift, attrName, value));
36+
lines.add(String.format("%s%s: %s", shift, attrName, toSingleValue(value)));
3937
}
4038
}
4139

@@ -63,7 +61,7 @@ private static void toTextBlockCollection(List<String> lines, String shift, @Nul
6361
if (attrName == null) {
6462
lines.add(String.format("%s[", shift));
6563
} else {
66-
lines.add(String.format("%s%s = [", shift, attrName));
64+
lines.add(String.format("%s%s: [", shift, attrName));
6765
}
6866
int i = 0;
6967
for (var item : coll) {
@@ -73,4 +71,11 @@ private static void toTextBlockCollection(List<String> lines, String shift, @Nul
7371
lines.add(String.format("%s]", shift));
7472
}
7573

74+
private static String toSingleValue(Object value) {
75+
if (value instanceof String str) {
76+
return String.format("\"%s\"", str);
77+
}
78+
return String.format("%s", value);
79+
}
80+
7681
}

src/main/java/io/github/computerdaddyguy/jfiletreeprettyprinter/cli/exception/ExternalOptionsException.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@
22

33
import java.nio.file.Path;
44
import org.jspecify.annotations.NullMarked;
5+
import org.jspecify.annotations.Nullable;
56

67
@NullMarked
78
public class ExternalOptionsException extends RuntimeException {
89

10+
@Nullable
911
private final transient Path optionsPath;
1012

11-
public ExternalOptionsException(Path optionsPath, String message, Throwable cause) {
13+
public ExternalOptionsException(@Nullable Path optionsPath, String message, Throwable cause) {
1214
super(message, cause);
1315
this.optionsPath = optionsPath;
1416
}
1517

16-
public ExternalOptionsException(Path optionsPath, String message) {
18+
public ExternalOptionsException(@Nullable Path optionsPath, String message) {
1719
super(message);
1820
this.optionsPath = optionsPath;
1921
}
2022

23+
@Nullable
2124
public Path getOptionsPath() {
2225
return optionsPath;
2326
}

src/main/java/io/github/computerdaddyguy/jfiletreeprettyprinter/cli/options/mapper/DefaultExternalOptionsMapper.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.github.computerdaddyguy.jfiletreeprettyprinter.cli.options.model.ExternalOptions;
55
import io.github.computerdaddyguy.jfiletreeprettyprinter.cli.options.model.Matcher;
66
import io.github.computerdaddyguy.jfiletreeprettyprinter.options.ChildLimits;
7+
import io.github.computerdaddyguy.jfiletreeprettyprinter.options.LineExtensions;
78
import io.github.computerdaddyguy.jfiletreeprettyprinter.options.PathMatchers;
89
import io.github.computerdaddyguy.jfiletreeprettyprinter.options.PrettyPrintOptions;
910
import java.nio.file.Path;
@@ -15,7 +16,11 @@ class DefaultExternalOptionsMapper implements ExternalOptionsMapper {
1516
public PrettyPrintOptions mapToOptions(Path targetPath, ExternalOptions externalOptions) {
1617
var options = PrettyPrintOptions.createDefault();
1718
options = mapEmojis(options, externalOptions);
19+
options = mapCompactDirectories(options, externalOptions);
20+
options = mapMaxDepth(options, externalOptions);
1821
options = mapChildLimit(options, externalOptions, targetPath);
22+
options = mapFilter(options, externalOptions, targetPath);
23+
options = mapLineExtensions(options, externalOptions, targetPath);
1924
return options;
2025
}
2126

@@ -26,6 +31,20 @@ private PrettyPrintOptions mapEmojis(PrettyPrintOptions options, ExternalOptions
2631
return options;
2732
}
2833

34+
private PrettyPrintOptions mapCompactDirectories(PrettyPrintOptions options, ExternalOptions externalOptions) {
35+
if (Boolean.TRUE.equals(externalOptions.compactDirectories())) {
36+
return options.withCompactDirectories(true);
37+
}
38+
return options;
39+
}
40+
41+
private PrettyPrintOptions mapMaxDepth(PrettyPrintOptions options, ExternalOptions externalOptions) {
42+
if (externalOptions.maxDepth() != null) {
43+
return options.withMaxDepth(externalOptions.maxDepth());
44+
}
45+
return options;
46+
}
47+
2948
private PrettyPrintOptions mapChildLimit(PrettyPrintOptions options, ExternalOptions externalOptions, Path targetPath) {
3049
if (externalOptions.childLimit() == null) {
3150
return options;
@@ -43,6 +62,30 @@ private PrettyPrintOptions mapChildLimit(PrettyPrintOptions options, ExternalOpt
4362
};
4463
}
4564

65+
private PrettyPrintOptions mapFilter(PrettyPrintOptions options, ExternalOptions externalOptions, Path targetPath) {
66+
if (externalOptions.filter() == null) {
67+
return options;
68+
}
69+
if (externalOptions.filter().dir() != null) {
70+
options = options.filterDirectories(mapMatcher(externalOptions.filter().dir(), targetPath));
71+
}
72+
if (externalOptions.filter().file() != null) {
73+
options = options.filterFiles(mapMatcher(externalOptions.filter().file(), targetPath));
74+
}
75+
return options;
76+
}
77+
78+
private PrettyPrintOptions mapLineExtensions(PrettyPrintOptions options, ExternalOptions externalOptions, Path targetPath) {
79+
if (externalOptions.lineExtensions() == null) {
80+
return options;
81+
}
82+
var builder = LineExtensions.builder();
83+
for (var extension : externalOptions.lineExtensions()) {
84+
builder.add(mapMatcher(extension.matcher(), targetPath), extension.extension());
85+
}
86+
return options.withLineExtension(builder.build());
87+
}
88+
4689
private PathMatcher mapMatcher(Matcher matcher, Path targetPath) {
4790
return switch (matcher) {
4891
case Matcher.AlwaysTrue() -> p -> true;
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package io.github.computerdaddyguy.jfiletreeprettyprinter.cli.options.model;
22

33
import jakarta.validation.Valid;
4+
import jakarta.validation.constraints.NotNull;
5+
import java.util.List;
46
import org.jspecify.annotations.Nullable;
57

68
public record ExternalOptions(
79
@Nullable Boolean emojis,
8-
@Valid @Nullable ChildLimit childLimit
10+
@Nullable Boolean compactDirectories,
11+
@Nullable Integer maxDepth,
12+
@Valid @Nullable ChildLimit childLimit,
13+
@Valid @Nullable Filter filter,
14+
@Valid @Nullable List<@Valid @NotNull LineExtension> lineExtensions
915
) {
1016

1117
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.github.computerdaddyguy.jfiletreeprettyprinter.cli.options.model;
2+
3+
import jakarta.validation.Valid;
4+
import org.jspecify.annotations.Nullable;
5+
6+
public record Filter(
7+
@Valid @Nullable Matcher dir,
8+
@Valid @Nullable Matcher file
9+
) {
10+
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.github.computerdaddyguy.jfiletreeprettyprinter.cli.options.model;
2+
3+
import jakarta.validation.Valid;
4+
import jakarta.validation.constraints.NotNull;
5+
6+
public record LineExtension(
7+
@Valid @NotNull String extension,
8+
@Valid @NotNull Matcher matcher
9+
) {
10+
11+
}

src/main/resources/META-INF/native-image/io.github.computerdaddyguy/jfiletreeprettyprinter/reachability-metadata.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@
4747
"allDeclaredFields": true,
4848
"allPublicFields": true
4949
},
50+
{
51+
"type": "io.github.computerdaddyguy.jfiletreeprettyprinter.cli.options.model.Filter",
52+
"allDeclaredConstructors": true,
53+
"allPublicConstructors": true,
54+
"allDeclaredMethods": true,
55+
"allPublicMethods": true,
56+
"allDeclaredFields": true,
57+
"allPublicFields": true
58+
},
59+
{
60+
"type": "io.github.computerdaddyguy.jfiletreeprettyprinter.cli.options.model.LineExtension",
61+
"allDeclaredConstructors": true,
62+
"allPublicConstructors": true,
63+
"allDeclaredMethods": true,
64+
"allPublicMethods": true,
65+
"allDeclaredFields": true,
66+
"allPublicFields": true
67+
},
5068
{
5169
"type": "io.github.computerdaddyguy.jfiletreeprettyprinter.cli.options.model.ChildLimit$StaticLimit",
5270
"allDeclaredConstructors": true,

0 commit comments

Comments
 (0)