Creates a Tree structure from a dictionary of files using the rich library. Args: files (dict): A dictionary where keys are file paths (strings) and values are file content (strings). Returns: Tree: The root of the create
(self, root_folder, files)
| 75 | super().print() |
| 76 | |
| 77 | def _create_tree_from_files(self, root_folder, files): |
| 78 | """ |
| 79 | Creates a Tree structure from a dictionary of files using the rich library. |
| 80 | |
| 81 | Args: |
| 82 | files (dict): A dictionary where keys are file paths (strings) |
| 83 | and values are file content (strings). |
| 84 | |
| 85 | Returns: |
| 86 | Tree: The root of the created tree structure. |
| 87 | """ |
| 88 | tree = Tree(root_folder) |
| 89 | for path, content in files.items(): |
| 90 | parts = path.split("/") |
| 91 | current_level = tree |
| 92 | for part in parts: |
| 93 | existing_level = None |
| 94 | for child in current_level.children: |
| 95 | if child.label == part: |
| 96 | existing_level = child |
| 97 | break |
| 98 | |
| 99 | if existing_level is None: |
| 100 | if part == parts[-1]: |
| 101 | if files[path] is None: |
| 102 | current_level = current_level.add(f"{part} [red]deleted[/red]") |
| 103 | else: |
| 104 | file_lines = len(content.splitlines()) |
| 105 | file_tokens = self._count_tokens(content) |
| 106 | current_level = current_level.add(f"{part} ({file_lines} lines, {file_tokens} tokens)") |
| 107 | else: |
| 108 | current_level = current_level.add(part) |
| 109 | else: |
| 110 | current_level = existing_level |
| 111 | |
| 112 | return tree |
| 113 | |
| 114 | def _count_tokens(self, text): |
| 115 | """Count tokens using tiktoken if available, otherwise estimate from character count.""" |
no test coverage detected