tenseleyflow/sultree / 9301005

Browse files

'renderer'

Authored by espadonne
SHA
93010058264df03ac4813a33742d8a8acf4e067a
Parents
6b40f91
Tree
e9de9c7

2 changed files

StatusFile+-
M src/sultree/cli.py 10 7
M src/sultree/traversal.py 4 3
src/sultree/cli.pymodified
@@ -18,7 +18,7 @@ from typing import List, Optional
18
 
18
 
19
 from .args import parse_arguments
19
 from .args import parse_arguments
20
 from .traversal import SecureTraverser, FileEntry
20
 from .traversal import SecureTraverser, FileEntry
21
-from .formatting import TreeFormatter
21
+from .tree_renderer import TreeRenderer, build_tree_structure
22
 from .selinux import is_selinux_enabled, SELinuxQueryError
22
 from .selinux import is_selinux_enabled, SELinuxQueryError
23
 from .constants import CONFIG
23
 from .constants import CONFIG
24
 
24
 
@@ -46,7 +46,7 @@ def setup_logging(verbose: bool = False) -> None:
46
 
46
 
47
 
47
 
48
 def process_directory(directory: Path, traverser: SecureTraverser, 
48
 def process_directory(directory: Path, traverser: SecureTraverser, 
49
-                     formatter: TreeFormatter) -> bool:
49
+                     renderer: TreeRenderer) -> bool:
50
     """
50
     """
51
     Process a single directory and display its tree.
51
     Process a single directory and display its tree.
52
     
52
     
@@ -66,9 +66,12 @@ def process_directory(directory: Path, traverser: SecureTraverser,
66
         for entry in traverser.traverse(directory):
66
         for entry in traverser.traverse(directory):
67
             entries.append(entry)
67
             entries.append(entry)
68
             
68
             
69
-        # Format and display the tree
69
+        # Build proper tree structure
70
+        root_node = build_tree_structure(entries, directory)
71
+        
72
+        # Render the tree
70
         show_report = True  # Could be made configurable
73
         show_report = True  # Could be made configurable
71
-        formatter.format_tree(directory, entries, show_report)
74
+        renderer.render_tree(directory, root_node, show_report)
72
         
75
         
73
         return True
76
         return True
74
         
77
         
@@ -137,13 +140,13 @@ def main() -> int:
137
         # Setup logging if needed (could add --verbose flag)
140
         # Setup logging if needed (could add --verbose flag)
138
         setup_logging(verbose=False)
141
         setup_logging(verbose=False)
139
         
142
         
140
-        # Create traverser and formatter
143
+        # Create traverser and renderer
141
         traverser = SecureTraverser(options)
144
         traverser = SecureTraverser(options)
142
         
145
         
143
         # Determine if SELinux context should be shown
146
         # Determine if SELinux context should be shown
144
         show_selinux = options.selinux_filter is not None
147
         show_selinux = options.selinux_filter is not None
145
         
148
         
146
-        formatter = TreeFormatter(
149
+        renderer = TreeRenderer(
147
             show_full_path=options.show_full_path,
150
             show_full_path=options.show_full_path,
148
             show_selinux=show_selinux,
151
             show_selinux=show_selinux,
149
             use_ascii=False  # Could be made configurable
152
             use_ascii=False  # Could be made configurable
@@ -159,7 +162,7 @@ def main() -> int:
159
                 print()  # Empty line between directories
162
                 print()  # Empty line between directories
160
                 
163
                 
161
             # Process directory
164
             # Process directory
162
-            if process_directory(directory, traverser, formatter):
165
+            if process_directory(directory, traverser, renderer):
163
                 success_count += 1
166
                 success_count += 1
164
             else:
167
             else:
165
                 logger.warning(f"Failed to process directory: {directory}")
168
                 logger.warning(f"Failed to process directory: {directory}")
src/sultree/traversal.pymodified
@@ -136,7 +136,7 @@ class SecureTraverser:
136
             
136
             
137
         try:
137
         try:
138
             # Get directory contents
138
             # Get directory contents
139
-            entries = self._get_directory_entries(current_path)
139
+            entries = self._get_directory_entries(current_path, depth)
140
             
140
             
141
             # Apply file limit if specified
141
             # Apply file limit if specified
142
             if self.options.file_limit is not None:
142
             if self.options.file_limit is not None:
@@ -169,12 +169,13 @@ class SecureTraverser:
169
         except OSError as e:
169
         except OSError as e:
170
             logger.warning(f"Error accessing {current_path}: {e}")
170
             logger.warning(f"Error accessing {current_path}: {e}")
171
             
171
             
172
-    def _get_directory_entries(self, dir_path: Path) -> List[FileEntry]:
172
+    def _get_directory_entries(self, dir_path: Path, depth: int = 0) -> List[FileEntry]:
173
         """
173
         """
174
         Get entries in a directory with error handling.
174
         Get entries in a directory with error handling.
175
         
175
         
176
         Args:
176
         Args:
177
             dir_path: Directory to read
177
             dir_path: Directory to read
178
+            depth: Current depth in traversal
178
             
179
             
179
         Returns:
180
         Returns:
180
             List of FileEntry objects
181
             List of FileEntry objects
@@ -204,7 +205,7 @@ class SecureTraverser:
204
                         path=item,
205
                         path=item,
205
                         name=item.name,
206
                         name=item.name,
206
                         is_dir=is_dir,
207
                         is_dir=is_dir,
207
-                        depth=0  # Will be set by caller
208
+                        depth=depth + 1  # Set correct depth
208
                     ))
209
                     ))
209
                     
210
                     
210
                 except (OSError, RuntimeError) as e:
211
                 except (OSError, RuntimeError) as e: