Created
November 26, 2024 19:00
-
-
Save HeySreelal/0e2757cd2ba5dd849c84e7a76a3ee41c to your computer and use it in GitHub Desktop.
A simple Dart program to creates a tree-like visualization of the directory structure! π―
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'dart:async'; | |
void main() async { | |
final rootDirectory = Directory.current; | |
await printDirectoryTree(rootDirectory); | |
} | |
Future<void> printDirectoryTree(Directory directory, {String prefix = ''}) async { | |
// List to store entries, sorted to ensure consistent output | |
List<FileSystemEntity> entries; | |
try { | |
entries = await directory.list().toList(); | |
// Sort entries to make output more readable | |
entries.sort((a, b) => a.path.compareTo(b.path)); | |
} catch (e) { | |
print('Error accessing directory: $e'); | |
return; | |
} | |
for (int i = 0; i < entries.length; i++) { | |
final entry = entries[i]; | |
final isLast = i == entries.length - 1; | |
// Determine the appropriate connector | |
final connector = isLast ? 'βββ ' : 'βββ '; | |
// Skip hidden files and directories | |
if (shouldSkipEntry(entry)) continue; | |
// Get the relative path from the current directory | |
final relativePath = entry.path.replaceFirst(directory.path + Platform.pathSeparator, ''); | |
// Print the current entry | |
print('$prefix$connector$relativePath'); | |
// Recursively process directories | |
if (entry is Directory) { | |
await printDirectoryTree( | |
entry, | |
prefix: prefix + (isLast ? ' ' : 'β ') | |
); | |
} | |
} | |
} | |
bool shouldSkipEntry(FileSystemEntity entry) { | |
final fileName = entry.path.split(Platform.pathSeparator).last; | |
// List of directories and files to ignore | |
final ignoredPatterns = [ | |
'.git', | |
'.idea', | |
'.vscode', | |
'node_modules', | |
'build', | |
'out', | |
'dist', | |
'.dart_tool', | |
'.packages', | |
'.pub-cache', | |
'.flutter-plugins', | |
'.flutter-plugins-dependencies' | |
]; | |
return ignoredPatterns.any((pattern) => fileName.contains(pattern)) || | |
fileName.startsWith('.'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Upgraded to a full repository with even more functionalities. You can check it out here: https://github.com/HeySreelal/file_mapper