11package io .github .computerdaddyguy .jfiletreeprettyprinter .cli .options ;
22
33import io .github .computerdaddyguy .jfiletreeprettyprinter .cli .ConsoleOutput ;
4+ import io .github .computerdaddyguy .jfiletreeprettyprinter .options .ChildLimits ;
5+ import io .github .computerdaddyguy .jfiletreeprettyprinter .options .PathMatchers ;
46import io .github .computerdaddyguy .jfiletreeprettyprinter .options .PrettyPrintOptions ;
57import jakarta .validation .ConstraintViolationException ;
68import jakarta .validation .Validation ;
79import java .nio .file .Path ;
10+ import java .nio .file .PathMatcher ;
811import org .hibernate .validator .messageinterpolation .ParameterMessageInterpolator ;
912import org .jspecify .annotations .NullMarked ;
1013import org .jspecify .annotations .Nullable ;
@@ -20,19 +23,19 @@ public ExternalOptionsReader(ConsoleOutput output) {
2023 }
2124
2225 @ Nullable
23- public PrettyPrintOptions readOptions (Path optionsPath ) {
26+ public PrettyPrintOptions readOptions (Path targetPath , Path optionsPath ) {
2427 optionsPath = optionsPath .toAbsolutePath ().normalize ();
25- return tryCandidate (optionsPath , true );
28+ return tryCandidate (targetPath , optionsPath , true );
2629 }
2730
2831 @ Nullable
29- public PrettyPrintOptions readOptions (Iterable <Path > potentialOptions ) {
32+ public PrettyPrintOptions readOptions (Path targetPath , Iterable <Path > potentialOptions ) {
3033
3134 PrettyPrintOptions options = null ;
3235
3336 for (var candidateOptionPath : potentialOptions ) {
3437 candidateOptionPath = candidateOptionPath .toAbsolutePath ().normalize ();
35- options = tryCandidate (candidateOptionPath , false );
38+ options = tryCandidate (targetPath , candidateOptionPath , false );
3639 if (options != null ) {
3740 break ;
3841 }
@@ -51,7 +54,7 @@ private void printFailure(boolean required, String msg, Object... args) {
5154 }
5255
5356 @ Nullable
54- private PrettyPrintOptions tryCandidate (Path optionsPath , boolean required ) {
57+ private PrettyPrintOptions tryCandidate (Path targetPath , Path optionsPath , boolean required ) {
5558
5659 var optionsFile = optionsPath .toFile ();
5760 if (!optionsFile .exists ()) {
@@ -74,7 +77,7 @@ private PrettyPrintOptions tryCandidate(Path optionsPath, boolean required) {
7477 ExternalOptions externalOptions = load (optionsPath , required );
7578 validate (optionsPath , externalOptions , required );
7679
77- return mapToOptions (externalOptions );
80+ return mapToOptions (targetPath , externalOptions );
7881 }
7982
8083 private ExternalOptions load (Path optionsPath , boolean required ) {
@@ -106,14 +109,47 @@ private void validate(Path optionsPath, ExternalOptions externalOptions, boolean
106109 }
107110 }
108111
109- private PrettyPrintOptions mapToOptions (ExternalOptions externalOptions ) {
112+ private PrettyPrintOptions mapToOptions (Path targetPath , ExternalOptions externalOptions ) {
110113 var options = PrettyPrintOptions .createDefault ();
114+ options = mapEmojis (options , externalOptions );
115+ options = mapChildLimit (options , externalOptions , targetPath );
116+ return options ;
117+ }
111118
119+ private PrettyPrintOptions mapEmojis (PrettyPrintOptions options , ExternalOptions externalOptions ) {
112120 if (Boolean .TRUE .equals (externalOptions .emojis ())) {
113- options = options .withDefaultEmojis ();
121+ return options .withDefaultEmojis ();
114122 }
115-
116123 return options ;
117124 }
118125
126+ private PrettyPrintOptions mapChildLimit (PrettyPrintOptions options , ExternalOptions externalOptions , Path targetPath ) {
127+ if (externalOptions .childLimit () == null ) {
128+ return options ;
129+ }
130+ return switch (externalOptions .childLimit ()) {
131+ case ChildLimit .StaticLimit staticLimit -> options .withChildLimit (staticLimit .limit ());
132+ case ChildLimit .DynamicLimit dynLimit -> {
133+ var limitBuilder = ChildLimits .builder ();
134+ for (var limit : dynLimit .limits ()) {
135+ limitBuilder .add (mapMatcher (limit .matcher (), targetPath ), limit .limit ());
136+ }
137+ options .withChildLimit (limitBuilder .build ());
138+ yield options ;
139+ }
140+ };
141+ }
142+
143+ private PathMatcher mapMatcher (Matcher matcher , Path targetPath ) {
144+ return switch (matcher ) {
145+ case Matcher .AlwaysTrue alwaysTrue -> (p ) -> true ;
146+ case Matcher .AlwaysFalse alwaysFalse -> (p ) -> false ;
147+ case Matcher .AllOf allOf -> PathMatchers .allOf (allOf .matchers ().stream ().map (subMatcher -> mapMatcher (subMatcher , targetPath )).toList ());
148+ case Matcher .AnyOf anyOf -> PathMatchers .anyOf (anyOf .matchers ().stream ().map (subMatcher -> mapMatcher (subMatcher , targetPath )).toList ());
149+ case Matcher .NoneOf noneOf -> PathMatchers .noneOf (noneOf .matchers ().stream ().map (subMatcher -> mapMatcher (subMatcher , targetPath )).toList ());
150+ case Matcher .NameGlob nameGlob -> PathMatchers .hasNameMatchingGlob (nameGlob .glob ());
151+ case Matcher .PathGlob pathGlob -> PathMatchers .hasRelativePathMatchingGlob (targetPath , pathGlob .glob ());
152+ };
153+ }
154+
119155}
0 commit comments