(path: P, limit: Option<usize>)
| 136 | } |
| 137 | |
| 138 | pub fn tree<P: AsRef<Path>>(path: P, limit: Option<usize>) -> Result<String, io::Error> { |
| 139 | let path = path.as_ref(); |
| 140 | let files = Self::files(path, FileSearchOptions::default())?; |
| 141 | |
| 142 | let mut root: BTreeMap<String, TreeNode> = BTreeMap::new(); |
| 143 | |
| 144 | for file in &files { |
| 145 | let rel_path = file.strip_prefix(path).unwrap_or(file); |
| 146 | let rel_str = rel_path.to_string_lossy(); |
| 147 | |
| 148 | if rel_str.contains(".opencode") { |
| 149 | continue; |
| 150 | } |
| 151 | |
| 152 | let parts: Vec<&str> = rel_str.split(std::path::MAIN_SEPARATOR).collect(); |
| 153 | if parts.len() < 2 { |
| 154 | continue; |
| 155 | } |
| 156 | |
| 157 | let mut current = &mut root; |
| 158 | for part in parts.iter().take(parts.len() - 1) { |
| 159 | let node = current.entry(part.to_string()).or_insert(TreeNode { |
| 160 | name: part.to_string(), |
| 161 | children: BTreeMap::new(), |
| 162 | }); |
| 163 | current = &mut node.children; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | let total = count_nodes(&root); |
| 168 | let limit = limit.unwrap_or(total); |
| 169 | let mut lines: Vec<String> = Vec::new(); |
| 170 | let mut queue: Vec<(String, String)> = Vec::new(); |
| 171 | |
| 172 | for (_, node) in root.iter() { |
| 173 | let node_json = serde_json::to_string(node).unwrap_or_default(); |
| 174 | queue.push((node.name.clone(), node_json)); |
| 175 | } |
| 176 | |
| 177 | let mut used = 0; |
| 178 | let mut i = 0; |
| 179 | while i < queue.len() && used < limit { |
| 180 | let (path_str, node_json) = &queue[i]; |
| 181 | let node: TreeNode = match serde_json::from_str(node_json) { |
| 182 | Ok(n) => n, |
| 183 | Err(_) => { |
| 184 | i += 1; |
| 185 | continue; |
| 186 | } |
| 187 | }; |
| 188 | lines.push(path_str.clone()); |
| 189 | used += 1; |
| 190 | |
| 191 | let children_to_add: Vec<(String, String)> = node |
| 192 | .children |
| 193 | .iter() |
| 194 | .map(|(_, child)| { |
| 195 | let child_json = serde_json::to_string(child).unwrap_or_default(); |
nothing calls this directly
no test coverage detected