Skip to content

Instantly share code, notes, and snippets.

@HeySreelal
Created November 26, 2024 19:00
Show Gist options
  • Save HeySreelal/0e2757cd2ba5dd849c84e7a76a3ee41c to your computer and use it in GitHub Desktop.
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! 🎯
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('.');
}
@HeySreelal
Copy link
Author

Compile it and run it on any directory to map the directory structure.

πŸ‘¨πŸ»β€πŸ’» Compile

dart compile exe file_mapper.dart -o filemapper

πŸƒπŸ»β€β™‚οΈ Run

./filemapper

@HeySreelal
Copy link
Author

Here's a test output from a newly created Flutter project.

β”œβ”€β”€ README.md
β”œβ”€β”€ analysis_options.yaml
β”œβ”€β”€ android
β”‚   β”œβ”€β”€ app
β”‚   β”‚   └── src
β”‚   β”‚       β”œβ”€β”€ debug
β”‚   β”‚       β”‚   └── AndroidManifest.xml
β”‚   β”‚       β”œβ”€β”€ main
β”‚   β”‚       β”‚   β”œβ”€β”€ AndroidManifest.xml
β”‚   β”‚       β”‚   β”œβ”€β”€ java
β”‚   β”‚       β”‚   β”‚   └── io
β”‚   β”‚       β”‚   β”‚       └── flutter
β”‚   β”‚       β”‚   β”‚           └── plugins
β”‚   β”‚       β”‚   β”‚               └── GeneratedPluginRegistrant.java
β”‚   β”‚       β”‚   β”œβ”€β”€ kotlin
β”‚   β”‚       β”‚   β”‚   └── com
β”‚   β”‚       β”‚   β”‚       └── example
β”‚   β”‚       β”‚   β”‚           └── project
β”‚   β”‚       β”‚   β”‚               └── MainActivity.kt
β”‚   β”‚       β”‚   └── res
β”‚   β”‚       β”‚       β”œβ”€β”€ drawable
β”‚   β”‚       β”‚       β”‚   └── launch_background.xml
β”‚   β”‚       β”‚       β”œβ”€β”€ drawable-v21
β”‚   β”‚       β”‚       β”‚   └── launch_background.xml
β”‚   β”‚       β”‚       β”œβ”€β”€ mipmap-hdpi
β”‚   β”‚       β”‚       β”‚   └── ic_launcher.png
β”‚   β”‚       β”‚       β”œβ”€β”€ mipmap-mdpi
β”‚   β”‚       β”‚       β”‚   └── ic_launcher.png
β”‚   β”‚       β”‚       β”œβ”€β”€ mipmap-xhdpi
β”‚   β”‚       β”‚       β”‚   └── ic_launcher.png
β”‚   β”‚       β”‚       β”œβ”€β”€ mipmap-xxhdpi
β”‚   β”‚       β”‚       β”‚   └── ic_launcher.png
β”‚   β”‚       β”‚       β”œβ”€β”€ mipmap-xxxhdpi
β”‚   β”‚       β”‚       β”‚   └── ic_launcher.png
β”‚   β”‚       β”‚       β”œβ”€β”€ values
β”‚   β”‚       β”‚       β”‚   └── styles.xml
β”‚   β”‚       β”‚       └── values-night
β”‚   β”‚       β”‚           └── styles.xml
β”‚   β”‚       └── profile
β”‚   β”‚           └── AndroidManifest.xml
β”‚   β”œβ”€β”€ gradle
β”‚   β”‚   └── wrapper
β”‚   β”‚       β”œβ”€β”€ gradle-wrapper.jar
β”‚   β”‚       └── gradle-wrapper.properties
β”‚   β”œβ”€β”€ gradle.properties
β”‚   β”œβ”€β”€ gradlew
β”‚   β”œβ”€β”€ gradlew.bat
β”‚   β”œβ”€β”€ local.properties
β”‚   β”œβ”€β”€ project_android.iml
β”‚   └── settings.gradle
β”œβ”€β”€ ios
β”‚   β”œβ”€β”€ Flutter
β”‚   β”‚   β”œβ”€β”€ AppFrameworkInfo.plist
β”‚   β”‚   β”œβ”€β”€ Debug.xcconfig
β”‚   β”‚   β”œβ”€β”€ Generated.xcconfig
β”‚   β”‚   β”œβ”€β”€ Release.xcconfig
β”‚   β”‚   └── flutter_export_environment.sh
β”‚   β”œβ”€β”€ Runner
β”‚   β”‚   β”œβ”€β”€ AppDelegate.swift
β”‚   β”‚   β”œβ”€β”€ Assets.xcassets
β”‚   β”‚   β”‚   β”œβ”€β”€ AppIcon.appiconset
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ Contents.json
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ [email protected]
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ [email protected]
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ [email protected]
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ [email protected]
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ [email protected]
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ [email protected]
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ [email protected]
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ [email protected]
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ [email protected]
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ [email protected]
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ [email protected]
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ [email protected]
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ [email protected]
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ [email protected]
β”‚   β”‚   β”‚   β”‚   └── [email protected]
β”‚   β”‚   β”‚   └── LaunchImage.imageset
β”‚   β”‚   β”‚       β”œβ”€β”€ Contents.json
β”‚   β”‚   β”‚       β”œβ”€β”€ LaunchImage.png
β”‚   β”‚   β”‚       β”œβ”€β”€ [email protected]
β”‚   β”‚   β”‚       β”œβ”€β”€ [email protected]
β”‚   β”‚   β”‚       └── README.md
β”‚   β”‚   β”œβ”€β”€ Base.lproj
β”‚   β”‚   β”‚   β”œβ”€β”€ LaunchScreen.storyboard
β”‚   β”‚   β”‚   └── Main.storyboard
β”‚   β”‚   β”œβ”€β”€ GeneratedPluginRegistrant.h
β”‚   β”‚   β”œβ”€β”€ GeneratedPluginRegistrant.m
β”‚   β”‚   β”œβ”€β”€ Info.plist
β”‚   β”‚   └── Runner-Bridging-Header.h
β”‚   β”œβ”€β”€ Runner.xcodeproj
β”‚   β”‚   β”œβ”€β”€ project.pbxproj
β”‚   β”‚   β”œβ”€β”€ project.xcworkspace
β”‚   β”‚   β”‚   β”œβ”€β”€ contents.xcworkspacedata
β”‚   β”‚   β”‚   └── xcshareddata
β”‚   β”‚   β”‚       β”œβ”€β”€ IDEWorkspaceChecks.plist
β”‚   β”‚   β”‚       └── WorkspaceSettings.xcsettings
β”‚   β”‚   └── xcshareddata
β”‚   β”‚       └── xcschemes
β”‚   β”‚           └── Runner.xcscheme
β”‚   β”œβ”€β”€ Runner.xcworkspace
β”‚   β”‚   β”œβ”€β”€ contents.xcworkspacedata
β”‚   β”‚   └── xcshareddata
β”‚   β”‚       β”œβ”€β”€ IDEWorkspaceChecks.plist
β”‚   β”‚       └── WorkspaceSettings.xcsettings
β”‚   └── RunnerTests
β”‚       └── RunnerTests.swift
β”œβ”€β”€ lib
β”‚   └── main.dart
β”œβ”€β”€ project.iml
β”œβ”€β”€ pubspec.lock
β”œβ”€β”€ pubspec.yaml
β”œβ”€β”€ test
β”‚   └── widget_test.dart
└── web
    β”œβ”€β”€ favicon.png
    β”œβ”€β”€ icons
    β”‚   β”œβ”€β”€ Icon-192.png
    β”‚   β”œβ”€β”€ Icon-512.png
    β”‚   β”œβ”€β”€ Icon-maskable-192.png
    β”‚   └── Icon-maskable-512.png
    β”œβ”€β”€ index.html
    └── manifest.json

@HeySreelal
Copy link
Author

Upgraded to a full repository with even more functionalities. You can check it out here: https://github.com/HeySreelal/file_mapper

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment