| 83 | } |
| 84 | |
| 85 | pub fn files<P: AsRef<Path>>( |
| 86 | path: P, |
| 87 | options: FileSearchOptions, |
| 88 | ) -> Result<Vec<PathBuf>, io::Error> { |
| 89 | let path = path.as_ref(); |
| 90 | if !path.is_dir() { |
| 91 | return Err(io::Error::new( |
| 92 | io::ErrorKind::NotFound, |
| 93 | format!("No such directory: '{}'", path.display()), |
| 94 | )); |
| 95 | } |
| 96 | |
| 97 | let mut result = Vec::new(); |
| 98 | let mut walk = WalkDir::new(path); |
| 99 | |
| 100 | if let Some(depth) = options.max_depth { |
| 101 | walk = walk.max_depth(depth); |
| 102 | } |
| 103 | |
| 104 | for entry in walk.into_iter().filter_map(|e| e.ok()) { |
| 105 | let entry_path = entry.path(); |
| 106 | |
| 107 | if !entry.file_type().is_file() { |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | let file_name = entry.file_name().to_string_lossy(); |
| 112 | if !options.hidden && file_name.starts_with('.') { |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | if file_name == ".git" || entry_path.to_string_lossy().contains(".git/") { |
| 117 | continue; |
| 118 | } |
| 119 | |
| 120 | if !options.glob.is_empty() { |
| 121 | let matches_glob = options.glob.iter().any(|g| { |
| 122 | if g.starts_with("!") { |
| 123 | return !glob_match::glob_match(&g[1..], entry_path); |
| 124 | } |
| 125 | glob_match::glob_match(g, entry_path) |
| 126 | }); |
| 127 | if !matches_glob { |
| 128 | continue; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | result.push(entry_path.to_path_buf()); |
| 133 | } |
| 134 | |
| 135 | Ok(result) |
| 136 | } |
| 137 | |
| 138 | pub fn tree<P: AsRef<Path>>(path: P, limit: Option<usize>) -> Result<String, io::Error> { |
| 139 | let path = path.as_ref(); |