28 lines
1.1 KiB
Swift
28 lines
1.1 KiB
Swift
import Foundation
|
|
|
|
enum DirectoryListing {
|
|
struct Entry {
|
|
enum Kind { case file, directory }
|
|
|
|
let name: String
|
|
let url: URL
|
|
let kind: Kind
|
|
}
|
|
|
|
/// Regular files and real folders of one folder in byte order of their names, dotfiles
|
|
/// included. Symlinks are left out, as ripgrep without `--follow` leaves them out.
|
|
static func entries(of directory: URL) -> [Entry] {
|
|
let keys: [URLResourceKey] = [.isSymbolicLinkKey, .isRegularFileKey, .isDirectoryKey]
|
|
guard let urls = try? FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: keys) else {
|
|
return []
|
|
}
|
|
return urls.compactMap { url -> Entry? in
|
|
guard let values = try? url.resourceValues(forKeys: Set(keys)), values.isSymbolicLink != true else { return nil }
|
|
if values.isDirectory == true { return Entry(name: url.lastPathComponent, url: url, kind: .directory) }
|
|
if values.isRegularFile == true { return Entry(name: url.lastPathComponent, url: url, kind: .file) }
|
|
return nil
|
|
}
|
|
.sorted { $0.name < $1.name }
|
|
}
|
|
}
|