Skip to content

Commit 9188107

Browse files
Refined and commented project structure example
1 parent 06a1027 commit 9188107

File tree

5 files changed

+157
-89
lines changed

5 files changed

+157
-89
lines changed

CHANGELOG.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
## [0.0.5] - Unreleased
1010

1111
### Added
12-
- New path matchers:
13-
`hasParentMatching`, `hasAncestorMatching`, `hasDirectChildMatching`, `hasDescendantMatching`, `hasSiblingMatching`,
14-
`hasAbsolutePathMatchingGlob`, `hasAbsolutePathMatching`,
15-
`hasRelativePathMatchingGlob`, `hasRelativePathMatching`,
16-
`hasNameMatchingGlob`, `hasNameStartingWith`
12+
- New various path matchers
1713

1814
### Changed
19-
- Filtering: now using `PathMatcher` instead of `Predicate<Path>`
15+
- Filtering: now using `PathMatcher` interface instead of `Predicate<Path>`
2016
- Filtering: split into distinct directories and files filters for better control
2117
- `PathUtils` and `PathPredicates` removed, use `PathMatchers` instead
22-
- Line extension: empty string is permitted to force line break in compact paths
18+
- Line extension: empty string is now permitted
2319

2420
### Fixed
2521
- The folder name is properly displayed at root when calling `prettyPrint(".")` (instead of "./")

README.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@
99
[![Javadoc](https://javadoc.io/badge2/io.github.computerdaddyguy/jfiletreeprettyprinter/javadoc.svg?color=blue)](https://javadoc.io/doc/io.github.computerdaddyguy/jfiletreeprettyprinter)
1010
[![Apache License 2.0](https://img.shields.io/:license-Apache%20License%202.0-blue.svg)](https://github.com/computerdaddyguy/jfiletreeprettyprinter/blob/main/LICENSE)
1111

12-
**A lightweight Java library for printing directory structures in a clean, tree-like format.**
12+
**A lightweight Java library for printing directory structures in a clean, tree-like format.**
1313

1414
Supports various [options](#options) to customize the directories scanning and rendering:
15-
- Filtering & sorting
16-
- Emoji support 🎉
17-
- Limit displayed children (fixed value or dynamically)
15+
- Filtering & sorting files and folders
16+
- Emojis as file icons 🎉
17+
- Limit displayed children of a folder (fixed value or dynamically)
1818
- Custom line extension (comment, file details, etc.)
1919
- Compact directory chains
20-
- Maximum depth
20+
- Maximum scanning depth
2121
- Various styles for tree rendering
2222

23+
> [!NOTE]
24+
> JFileTreePrettyPrinter is perfect to explain your project structure!
25+
> See <a href="https://github.com/ComputerDaddyGuy/JFileTreePrettyPrinter/blob/develop/src/example/java/io/github/computerdaddyguy/jfiletreeprettyprinter/example/ProjectStructure.java">ProjectStructure.java</a> to read the code that generate the below tree.
26+
2327
<p align="center">
24-
JFileTreePrettyPrint project structure, pretty printed using JFileTreePrettyPrint (see <a href="https://github.com/ComputerDaddyGuy/JFileTreePrettyPrinter/blob/develop/src/example/java/io/github/computerdaddyguy/jfiletreeprettyprinter/example/CompleteExample.java">CompleteExample.java</a>)
2528
<img src="https://raw.githubusercontent.com/ComputerDaddyGuy/JFileTreePrettyPrinter/refs/heads/develop/assets/project-structure.png" alt="JFileTreePrettyPrint project structure, using JFileTreePrettyPrint"/>
2629
</p>
2730

28-
> [!CAUTION]
29-
> This lib was developed just for fun, and has not been thoroughly tested!
30-
> May not be suitable for production code 😊
31-
3231
> [!IMPORTANT]
3332
> Complete documentation available in [wiki](https://github.com/ComputerDaddyGuy/JFileTreePrettyPrinter/wiki).
3433
3534
* [Import dependency](#import-dependency)
36-
* [Usage](#usage)
35+
* [Basic usage](#basic-usage)
3736
* [Options](#options)
3837
* [Changelog](#changelog)
3938
* [Roadmap](#roadmap)
@@ -56,7 +55,7 @@ For Gradle:
5655
implementation "io.github.computerdaddyguy:jfiletreeprettyprinter:0.0.4"
5756
```
5857

59-
# Usage
58+
# Basic Usage
6059
```java
6160
// Example: BasicUsage.java
6261
var printer = FileTreePrettyPrinter.createDefault();

ROADMAP.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [x] **Documentation**
1414
- [x] Add examples & README
1515
- [x] Use Github wiki to document options instead of readme
16+
- [x] Complete example with JFileTreePrettyPrint own project
1617
- [x] **Code style**
1718
- [x] Use JSpecify annotations
1819
- [x] **Testing**
@@ -27,7 +28,9 @@
2728
## To do
2829
- [x] More `PathMatchers` functions!
2930
- [ ] Helper class for line extension
31+
- [ ] Helper class for sorting
3032
- [ ] Option: custom emojis
33+
- [ ] Option: hide number of skipped files and folders for child limit
3134
- [ ] Rework/fix Github wiki to be up to date
3235

3336
## Backlog / To analyze / To implement if requested

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

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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

Comments
 (0)