printTree method

void printTree({
  1. String indent = '',
  2. bool isLast = true,
  3. bool showFiles = true,
  4. int? maxDepth,
  5. int currentDepth = 0,
  6. List<AssetLookupFile>? validFiles,
})

Prints a pretty tree structure of this folder and its children

Parameters:

  • indent: Starting indentation (defaults to empty string for root)
  • isLast: Whether this is the last item at its level (for proper tree drawing)
  • showFiles: Whether to include files in the tree (defaults to true)
  • maxDepth: Maximum depth to traverse (null for unlimited)
  • currentDepth: Current depth level (used internally)
  • validFiles: List of valid files to show (if null, shows all children)

Example:

folder.printTree();
 Output:
 assets/
 ├── images/
 │   ├── logo.png
 │   └── icon.jpg
 └── data/
     └── config.json

Implementation

void printTree({
  String indent = '',
  bool isLast = true,
  bool showFiles = true,
  int? maxDepth,
  int currentDepth = 0,
  List<AssetLookupFile>? validFiles,
}) {
  // Check depth limit
  if (maxDepth != null && currentDepth >= maxDepth) {
    return;
  }

  // Print current folder
  final folderName = p.basename(path);
  final prefix = indent + (isLast ? '└── ' : '├── ');
  print('$prefix$folderName/');

  // Prepare next level indent
  final nextIndent = indent + (isLast ? '    ' : '│   ');

  // Get children - filter files by validFiles if provided
  final folders = children.whereType<AssetLookupFolder>().toList();
  final files = showFiles
      ? children.whereType<AssetLookupFile>().where((file) {
          return validFiles == null || validFiles.contains(file);
        }).toList()
      : <AssetLookupFile>[];

  // Filter folders that have no valid descendants
  final validFolders = folders.where((folder) {
    return folder._hasValidDescendants(validFiles);
  }).toList();

  // Sort folders and files alphabetically
  validFolders.sort(
    (a, b) => p.basename(a.path).compareTo(p.basename(b.path)),
  );
  files.sort((a, b) => a.basename.compareTo(b.basename));

  final allItems = [...validFolders, ...files];

  // Print children
  for (int i = 0; i < allItems.length; i++) {
    final child = allItems[i];
    final isLastChild = i == allItems.length - 1;

    if (child is AssetLookupFolder) {
      child.printTree(
        indent: nextIndent,
        isLast: isLastChild,
        showFiles: showFiles,
        maxDepth: maxDepth,
        currentDepth: currentDepth + 1,
        validFiles: validFiles,
      );
    } else if (child is AssetLookupFile) {
      final filePrefix = nextIndent + (isLastChild ? '└── ' : '├── ');
      print('$filePrefix${child.basename}');
    }
  }
}