|
| 1 | +package io.github.computerdaddyguy.jfiletreeprettyprinter.example; |
| 2 | + |
| 3 | +import io.github.computerdaddyguy.jfiletreeprettyprinter.ChildLimitBuilder; |
| 4 | +import io.github.computerdaddyguy.jfiletreeprettyprinter.FileTreePrettyPrinter; |
| 5 | +import io.github.computerdaddyguy.jfiletreeprettyprinter.PathMatchers; |
| 6 | +import io.github.computerdaddyguy.jfiletreeprettyprinter.PrettyPrintOptions.Sorts; |
| 7 | +import java.nio.file.Path; |
| 8 | +import java.util.Comparator; |
| 9 | +import java.util.function.Function; |
| 10 | + |
| 11 | +public class ProjectStructure { |
| 12 | + |
| 13 | + public static void main(String[] args) { |
| 14 | + |
| 15 | + /* |
| 16 | + * ========================================================================================== |
| 17 | + * |
| 18 | + * Complete example code that pretty prints JFileTreePrettyPrint own project structure. |
| 19 | + * See the result in image at: https://github.com/ComputerDaddyGuy/JFileTreePrettyPrinter |
| 20 | + * |
| 21 | + * ========================================================================================== |
| 22 | + */ |
| 23 | + |
| 24 | + /* |
| 25 | + * The folder to pretty print (= the JFileTreePrettyPrint project root) |
| 26 | + */ |
| 27 | + var projectFolder = Path.of("."); |
| 28 | + |
| 29 | + /* |
| 30 | + * Filter for directories (visit and display only folders that pass this filter) |
| 31 | + */ |
| 32 | + var dirFilter = PathMatchers.noneOf( |
| 33 | + // Exclude these folders from traversal |
| 34 | + PathMatchers.hasRelativePathMatchingGlob(projectFolder, ".git"), |
| 35 | + PathMatchers.hasRelativePathMatchingGlob(projectFolder, ".github"), |
| 36 | + PathMatchers.hasRelativePathMatchingGlob(projectFolder, ".settings"), |
| 37 | + PathMatchers.hasRelativePathMatchingGlob(projectFolder, "src/example"), |
| 38 | + PathMatchers.hasRelativePathMatchingGlob(projectFolder, "src/test"), |
| 39 | + PathMatchers.hasRelativePathMatchingGlob(projectFolder, "target") |
| 40 | + ); |
| 41 | + |
| 42 | + /* |
| 43 | + * Filter for files (display only files that pass this filter) |
| 44 | + * Note: files for which the parent folder does not match the directory filter |
| 45 | + * are obviously not displayed, even if they pass the file filter. |
| 46 | + */ |
| 47 | + var fileFilter = PathMatchers.allOf( |
| 48 | + |
| 49 | + // Hide files with names starting with "." |
| 50 | + PathMatchers.not(PathMatchers.hasNameStartingWith(".")), |
| 51 | + |
| 52 | + // Inside "jfiletreeprettyprinter" folder, keep only "FileTreePrettyPrinter.java" |
| 53 | + // Files in other folders are not restricted by this rule. |
| 54 | + PathMatchers.ifMatchesThenElse( |
| 55 | + /* if */ PathMatchers.hasDirectParentMatching(PathMatchers.hasName("jfiletreeprettyprinter")), |
| 56 | + /* then */ PathMatchers.hasName("FileTreePrettyPrinter.java"), |
| 57 | + /* else */ path -> true |
| 58 | + ) |
| 59 | + ); |
| 60 | + |
| 61 | + /* |
| 62 | + * Limit the number of displayed children by directory: some content is not relevant and clutters the final result! |
| 63 | + */ |
| 64 | + var childLimitFunction = ChildLimitBuilder.builder() |
| 65 | + // Hide all files under renderer and scanner packages |
| 66 | + .limit(PathMatchers.hasAbsolutePathMatchingGlob("**/io/github/computerdaddyguy/jfiletreeprettyprinter/renderer"), 0) |
| 67 | + .limit(PathMatchers.hasAbsolutePathMatchingGlob("**/io/github/computerdaddyguy/jfiletreeprettyprinter/scanner"), 0) |
| 68 | + .build(); |
| 69 | + |
| 70 | + /* |
| 71 | + * Add some comments on a few files and directories |
| 72 | + */ |
| 73 | + Function<Path, String> lineExtension = path -> { |
| 74 | + if (PathMatchers.hasName("project-structure.png").matches(path)) { |
| 75 | + return "\t// This image"; |
| 76 | + } else if (PathMatchers.hasName("FileTreePrettyPrinter.java").matches(path)) { |
| 77 | + return "\t// Main entry point"; |
| 78 | + } else if (PathMatchers.hasName("README.md").matches(path)) { |
| 79 | + return "\t\t// You're reading at this!"; |
| 80 | + } else if (PathMatchers.hasRelativePathMatchingGlob(projectFolder, "src/main/java").matches(path)) { |
| 81 | + return ""; // Empty string: force line break in compact directory chain |
| 82 | + } |
| 83 | + return null; |
| 84 | + }; |
| 85 | + |
| 86 | + /* |
| 87 | + * Sort all paths by directory first (then alphabetically by default) |
| 88 | + */ |
| 89 | + Comparator<Path> pathComparator = Sorts.DIRECTORY_FIRST; |
| 90 | + |
| 91 | + /* |
| 92 | + * Build the final FileTreePrettyPrinter |
| 93 | + */ |
| 94 | + var prettyPrinter = FileTreePrettyPrinter.builder() |
| 95 | + .customizeOptions( |
| 96 | + options -> options |
| 97 | + .withEmojis(true) // Use emojis! |
| 98 | + .withCompactDirectories(true) // Inline directory chains: "src/main/java/..." |
| 99 | + .filterDirectories(dirFilter) |
| 100 | + .filterFiles(fileFilter) |
| 101 | + .withChildLimit(childLimitFunction) |
| 102 | + .withLineExtension(lineExtension) |
| 103 | + .sort(pathComparator) |
| 104 | + ) |
| 105 | + .build(); |
| 106 | + |
| 107 | + /* |
| 108 | + * Pretty print and display the result! |
| 109 | + */ |
| 110 | + var tree = prettyPrinter.prettyPrint(projectFolder); |
| 111 | + System.out.println(tree); |
| 112 | + |
| 113 | + /* |
| 114 | + ================================ |
| 115 | + Expected result |
| 116 | + ================================ |
| 117 | + |
| 118 | + 📂 JFileTreePrettyPrinter/ |
| 119 | + ├─ 📂 assets/ |
| 120 | + │ └─ 🖼️ project-structure.png // This image |
| 121 | + ├─ 📂 src/main/java/ |
| 122 | + │ └─ 📂 io/github/computerdaddyguy/jfiletreeprettyprinter/ |
| 123 | + │ ├─ 📂 renderer/ |
| 124 | + │ │ └─ ... (5 files and 2 directories skipped) |
| 125 | + │ ├─ 📂 scanner/ |
| 126 | + │ │ └─ ... (4 files skipped) |
| 127 | + │ └─ ☕ FileTreePrettyPrinter.java // Main entry point |
| 128 | + ├─ 🗺️ CHANGELOG.md |
| 129 | + ├─ 📖 CONTRIBUTING.md |
| 130 | + ├─ 📄 LICENSE |
| 131 | + ├─ 📖 README.md // You're reading at this! |
| 132 | + ├─ 🗺️ ROADMAP.md |
| 133 | + ├─ 🛡️ SECURITY.md |
| 134 | + ├─ 🏗️ pom.xml |
| 135 | + ├─ 📖 release_process.md |
| 136 | + └─ 📜 runMutationTests.sh |
| 137 | + */ |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments