-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileSystem.swift
58 lines (53 loc) · 1.26 KB
/
FileSystem.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import ChromaShell
import Foundation
import Observation
import ScribeCore
import _NIOFileSystem
@Observable
final class FileSystemManager {
var cwd: String
var names: [String] = ["Zane"]
init() {
self.cwd = FileManager.default.currentDirectoryPath
}
func update() {
Task(priority: .userInitiated) {
self.names = await getFileNames(at: FilePath(cwd))
}
}
}
struct FileSystemBlock: Block {
init() {}
let fileSystem = FileSystemManager()
var component: some Block {
Button("\(fileSystem.cwd)") {
fileSystem.update()
}
for name in fileSystem.names {
"\(name)"
}
}
}
@main
struct FileSystemMain: ChromaShell {
var main: some Block {
// FileSystemBlock()
"Hello Zane"
}
}
func getFileNames(at path: FilePath) async -> [String] {
var names: [String] = []
let fileSystem: FileSystem = FileSystem.shared
do {
try await fileSystem.withDirectoryHandle(atPath: path, options: .init())
{ dir in
for try await file in dir.listContents() {
names.append("\(file.name)")
}
}
} catch let error {
// TODO log error
return []
}
return names
}