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('.'); | |
} |
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
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
Compile it and run it on any directory to map the directory structure.
π¨π»βπ» Compile
ππ»ββοΈ Run