
DirectoryTree
A tiny Kotlin DSL to pretty-print hierarchies of directories and files.
val hierarchy = directory {
directory("featureOne") {
file()
}
directory() {
file()
}
directory() {
file()
directory(, , ) {
directory(, , ) {
file(, )
file(, )
file(, )
}
}
}
}
println(hierarchy)
├── featureOne
│ ╰── build.gradle.kts
├── featureTwo
│ ╰── build.gradle.kts
╰── gradle-common
├── build.gradle.kts
╰── src
╰── main
╰── kotlin
╰── com
╰── sample
╰── gradle
├── android
│ ╰── library.gradle.kts
├── jacoco
│ ╰── android
╰── sonarqube
╰── android
Configuration
Additional configuration can be specified by passing a DirectoryContext to the top-level directory function.
directory(
context = DirectoryContext(
separators = Separators(
empty = " ",
pipe = "│ ",
split = "├── ",
corner = "╰── ",
),
sortMode = SortMode.NONE,
directoriesFirst = false,
compactMode = CompactMode.NONE,
)
) {
file("file.txt")
}
Separators
Customize separators.
Sort mode
NONE - Do not sort.
ASC - Sort ascending by file/directory names.
DESC - Sort descending by file/directory names.
Directories first
false - Do not reorganize files/directories.
true - Place directories before files.
Compact mode
NONE - No directories and files will be merged and each will display on a separate line.
EXACT - Directories and files defined in a single call to directory or file will be merged.
DIRECTORIES - Directories with no sibling directories or files will be merged with their parents.
directory(
DirectoryContext(compactMode = compactMode)
) {
directory("one") {
file("two", "file.txt")
}
}
Download
The library is available from the MavenCentral repository as a Kotlin Multiplatform library targeting JVM, JS and
native:
implementation("io.github.antimonit:directory-tree:1.3.0")