ishans24 commited on
Commit
884ba03
Β·
verified Β·
1 Parent(s): 39d818b

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +85 -7
src/streamlit_app.py CHANGED
@@ -103,6 +103,7 @@ class GitHubRepoAnalyzer:
103
  if not tree:
104
  return "πŸ“ No files found in the specified path."
105
 
 
106
  output = []
107
  output.append(f"πŸ“¦ Repository: {tree_data['owner']}/{tree_data['repo']}")
108
  output.append(f"🌿 Branch: {tree_data['branch']}")
@@ -111,15 +112,93 @@ class GitHubRepoAnalyzer:
111
  output.append(f"πŸ“Š Total items: {len(tree)}")
112
  output.append("\n" + "="*60 + "\n")
113
 
114
- sorted_tree = sorted(tree, key=lambda x: (x['type'] != 'tree', x['path']))
115
-
116
- for item in sorted_tree:
117
- icon = "πŸ“" if item['type'] == 'tree' else "πŸ“„"
118
- size_info = f" ({item['size']} bytes)" if item['type'] == 'blob' and item['size'] > 0 else ""
119
- output.append(f"{icon} {item['path']}{size_info}")
120
 
121
  return "\n".join(output)
122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  def get_file_content(self, owner: str, repo: str, branch: str, file_path: str) -> str:
124
  """Get content of a specific file."""
125
  try:
@@ -196,7 +275,6 @@ if 'files_content' not in st.session_state:
196
  # Initialize analyzer
197
  analyzer = GitHubRepoAnalyzer()
198
 
199
-
200
  # Main title and description
201
  st.title("🌳 GitHub Repository Tree Generator")
202
  st.markdown("""
 
103
  if not tree:
104
  return "πŸ“ No files found in the specified path."
105
 
106
+ # Header information
107
  output = []
108
  output.append(f"πŸ“¦ Repository: {tree_data['owner']}/{tree_data['repo']}")
109
  output.append(f"🌿 Branch: {tree_data['branch']}")
 
112
  output.append(f"πŸ“Š Total items: {len(tree)}")
113
  output.append("\n" + "="*60 + "\n")
114
 
115
+ # Build tree structure
116
+ tree_structure = self._build_tree_structure(tree, tree_data.get('subfolder', ''))
117
+ output.append(tree_structure)
 
 
 
118
 
119
  return "\n".join(output)
120
 
121
+ def _build_tree_structure(self, tree_items: List[Dict], subfolder: str = '') -> str:
122
+ """Build a hierarchical tree structure with proper indentation and lines."""
123
+ if not tree_items:
124
+ return ""
125
+
126
+ # Create a directory structure
127
+ dir_structure = {}
128
+
129
+ # Process all items and build directory structure
130
+ for item in tree_items:
131
+ path = item['path']
132
+
133
+ # Remove subfolder prefix if present
134
+ if subfolder and path.startswith(subfolder):
135
+ path = path[len(subfolder):].lstrip('/')
136
+
137
+ if not path: # Skip empty paths
138
+ continue
139
+
140
+ parts = path.split('/')
141
+ current = dir_structure
142
+
143
+ # Build nested structure
144
+ for i, part in enumerate(parts):
145
+ if part not in current:
146
+ current[part] = {
147
+ '_type': 'tree' if i < len(parts) - 1 or item['type'] == 'tree' else 'blob',
148
+ '_size': item.get('size', 0) if i == len(parts) - 1 else 0,
149
+ '_children': {}
150
+ }
151
+ current = current[part]['_children']
152
+
153
+ # Generate tree display
154
+ return self._format_tree_recursive(dir_structure, "", True)
155
+
156
+ def _format_tree_recursive(self, structure: Dict, prefix: str = "", is_root: bool = False) -> str:
157
+ """Recursively format the tree structure with proper tree lines."""
158
+ lines = []
159
+ items = list(structure.items())
160
+
161
+ # Sort: directories first, then files, both alphabetically
162
+ items.sort(key=lambda x: (x[1]['_type'] != 'tree', x[0].lower()))
163
+
164
+ for i, (name, data) in enumerate(items):
165
+ is_last = i == len(items) - 1
166
+
167
+ # Determine the tree characters
168
+ if is_root:
169
+ current_prefix = ""
170
+ next_prefix = ""
171
+ else:
172
+ current_prefix = prefix + ("└── " if is_last else "β”œβ”€β”€ ")
173
+ next_prefix = prefix + (" " if is_last else "β”‚ ")
174
+
175
+ # Format current item
176
+ if data['_type'] == 'tree':
177
+ # Directory
178
+ display_name = f"{name}/"
179
+ if not is_root:
180
+ lines.append(f"{current_prefix}{display_name}")
181
+ else:
182
+ lines.append(f"{display_name}")
183
+
184
+ # Recursively add children
185
+ children_output = self._format_tree_recursive(
186
+ data['_children'],
187
+ next_prefix if not is_root else "",
188
+ False
189
+ )
190
+ if children_output:
191
+ lines.append(children_output)
192
+ else:
193
+ # File
194
+ size_info = f" ({data['_size']} bytes)" if data['_size'] > 0 else ""
195
+ if not is_root:
196
+ lines.append(f"{current_prefix}{name}{size_info}")
197
+ else:
198
+ lines.append(f"{name}{size_info}")
199
+
200
+ return "\n".join(lines)
201
+
202
  def get_file_content(self, owner: str, repo: str, branch: str, file_path: str) -> str:
203
  """Get content of a specific file."""
204
  try:
 
275
  # Initialize analyzer
276
  analyzer = GitHubRepoAnalyzer()
277
 
 
278
  # Main title and description
279
  st.title("🌳 GitHub Repository Tree Generator")
280
  st.markdown("""