1+ #!/usr/bin/env python3
12import os
23import argparse
4+ import time
35from typing import Optional , List
46
57class TreeScannerConfig :
@@ -13,17 +15,21 @@ class TreeScannerConfig:
1315 max_depth (Optional[int]): Maximale Rekursionstiefe.
1416 align_comments (bool): Kommentare am Zeilenende ausrichten.
1517 language (str): Sprache der Programmausgabe (de oder en).
18+ output_file (str): Pfad und Name der Ausgabedatei.
19+ ignored_dirs (Optional[List[str]]): Liste von Verzeichnissen, die ignoriert werden sollen.
1620 """
1721
1822 def __init__ (
1923 self ,
2024 root_path : str = "." ,
2125 folder_icon : str = "\U0001F4C1 " ,
2226 file_icon : str = "\U0001F4C4 " ,
23- max_files_per_dir : int = 2 ,
27+ max_files_per_dir : int = 100 ,
2428 max_depth : Optional [int ] = None ,
2529 align_comments : bool = True ,
26- language : str = "de"
30+ language : str = "de" ,
31+ output_file : str = "tree.txt" ,
32+ ignored_dirs : Optional [List [str ]] = None
2733 ):
2834 self .root_path = root_path
2935 self .folder_icon = folder_icon
@@ -32,6 +38,8 @@ def __init__(
3238 self .max_depth = max_depth
3339 self .align_comments = align_comments
3440 self .language = language
41+ self .output_file = output_file
42+ self .ignored_dirs = ignored_dirs or []
3543
3644class TreeScanner :
3745 """Klasse zum Scannen von Verzeichnissen und Erzeugen einer ASCII-Baumstruktur."""
@@ -42,6 +50,7 @@ def __init__(self, config: TreeScannerConfig):
4250 Args:
4351 config (TreeScannerConfig): Konfiguration für den Scanner.
4452 """
53+ self .last_output = time .time ()
4554 self .config = config
4655 self .folder_count = 0
4756 self .file_count = 0
@@ -71,7 +80,7 @@ def scan_directory(self, path: str, depth: int = 0, prefix: str = "") -> List[st
7180 except PermissionError :
7281 return [f"{ prefix } └── [Zugriff verweigert] { path } " ]
7382
74- folders = [e for e in entries if os .path .isdir (os .path .join (path , e ))]
83+ folders = [e for e in entries if os .path .isdir (os .path .join (path , e )) and e not in self . config . ignored_dirs ]
7584 files = [e for e in entries if os .path .isfile (os .path .join (path , e ))]
7685
7786 for idx , folder in enumerate (folders ):
@@ -92,6 +101,10 @@ def scan_directory(self, path: str, depth: int = 0, prefix: str = "") -> List[st
92101 for idx , name in enumerate (combined ):
93102 if not name .startswith ("<und " ):
94103 self .file_count += 1
104+ if time .time () - self .last_output >= 5 :
105+ print (f"[Info] { self .folder_count + self .file_count } Einträge gescannt..." , flush = True )
106+ self .last_output = time .time ()
107+
95108 connector = "├── " if idx < len (combined ) - 1 else "└── "
96109 lines .append (f"{ prefix } { connector } { self .config .file_icon } { name } " )
97110
@@ -144,23 +157,44 @@ def main():
144157 parser .add_argument ("-d" , "--max-depth" , type = int , help = "Maximale Rekursionstiefe; unbegrenzt, wenn nicht gesetzt." )
145158 parser .add_argument ("--no-align-comments" , action = "store_false" , dest = "align_comments" , help = "Deaktiviert das Ausrichten der Kommentare am Zeilenende." )
146159 parser .add_argument ("-l" , "--language" , type = str , default = "de" , choices = ["de" , "en" ], help = "Sprache der Programmausgabe (de oder en)." )
160+ parser .add_argument (
161+ "--ignore" , "-x" ,
162+ action = "append" ,
163+ help = "Verzeichnisse, die ignoriert werden sollen (z. B. .git, __pycache__). Mehrfach verwendbar."
164+ )
165+
166+ parser .add_argument ("-o" , "--output" , type = str , help = "Pfad und Name der Ausgabedatei (Standard: tree.txt)" )
167+
147168 args = parser .parse_args ()
148169
170+ output_file = args .output if args .output else "tree.txt"
171+ ignored_dirs = args .ignore if args .ignore else []
172+
173+ # Pfad validieren
174+ if not os .path .isdir (args .root_path ):
175+ print (f"Fehler: Der angegebene Pfad '{ args .root_path } ' ist kein gültiges Verzeichnis oder anderer falscher Parameter." )
176+ return
177+
178+ output_dir = os .path .dirname (output_file )
179+ if output_dir :
180+ os .makedirs (output_dir , exist_ok = True )
181+
149182 config = TreeScannerConfig (
150183 root_path = args .root_path ,
151184 max_files_per_dir = args .max_files_per_dir ,
152185 max_depth = args .max_depth ,
153186 align_comments = args .align_comments ,
154- language = args .language
187+ language = args .language ,
188+ output_file = output_file ,
189+ ignored_dirs = ignored_dirs
155190 )
156191 scanner = TreeScanner (config )
157192 tree_output = scanner .generate_tree ()
158193
159- output_file = "tree.txt"
160- with open (output_file , "w" , encoding = "utf-8" ) as f :
194+ with open (config .output_file , "w" , encoding = "utf-8" ) as f :
161195 f .write (tree_output + "\n " )
162196
163- scanner .print_summary (output_file )
197+ scanner .print_summary (config . output_file )
164198
165199if __name__ == "__main__" :
166200 main ()
0 commit comments