Skip to content

Instantly share code, notes, and snippets.

@fal-works
Created May 23, 2021 15:13
Show Gist options
  • Save fal-works/c1bfe2f1ccd593645bc58275353a415f to your computer and use it in GitHub Desktop.
Save fal-works/c1bfe2f1ccd593645bc58275353a415f to your computer and use it in GitHub Desktop.
Print file tree
import haxe.io.Path;
import sys.FileSystem;
function main()
printFileTree(getFileTree("cwd", "relPath")); // change arguments
typedef FileOrDir = {name:String, children:Array<FileOrDir>};
function getFileTree(cwd:String, relPath:String):FileOrDir {
final path = Path.join([cwd, relPath]);
final children = if (!FileSystem.isDirectory(path)) [] else {
FileSystem.readDirectory(path).map(getFileTree.bind(path));
};
return {name: relPath, children: children};
}
function printFileTree(fileOrDir:FileOrDir, depth:Int = 0) {
final indent = [for (_ in 0...depth) "\t"].join("");
Sys.println('${indent}${fileOrDir.name}');
for (child in fileOrDir.children) printFileTree(child, depth + 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment