This repository was archived by the owner on Aug 6, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathCodeFilesView.swift
More file actions
151 lines (134 loc) · 4.5 KB
/
Copy pathCodeFilesView.swift
File metadata and controls
151 lines (134 loc) · 4.5 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import SwiftUI
/// Model representing a code file in a circuit directory
struct CodeFile: Identifiable, Hashable {
let id = UUID()
let url: URL
let relativePath: String
let content: String
/// System image name based on file extension
var icon: String {
let ext = url.pathExtension.lowercased()
switch ext {
case "swift": return "swift"
case "json": return "curlybraces"
case "md", "txt": return "doc.text"
case "plist", "yaml", "yml": return "list.bullet.rectangle"
case "js", "ts": return "chevron.left.forwardslash.chevron.right"
case "py": return "chevron.left.forwardslash.chevron.right"
default: return "doc"
}
}
}
/// View for browsing code files in a circuit's directory
struct CodeFilesView: View {
let circuit: Circuit
@Environment(\.dismiss) private var dismiss
@State private var files: [CodeFile] = []
@State private var isLoading = true
/// Extensions to include as code files
private let codeExtensions: Set<String> = [
"swift", "json", "md", "txt", "plist", "yaml", "yml",
"js", "ts", "py", "h", "m", "c", "cpp", "html", "css"
]
var body: some View {
NavigationStack {
Group {
if isLoading {
ProgressView()
} else if files.isEmpty {
ContentUnavailableView(
"No Code Files",
systemImage: "doc.text",
description: Text("This circuit has no generated code files yet.")
)
} else {
fileList
}
}
.navigationTitle("Code Files")
#if os(iOS)
.navigationBarTitleDisplayMode(.inline)
#endif
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button("Done") {
dismiss()
}
}
}
}
.task {
loadFiles()
}
}
private var fileList: some View {
List(files) { file in
NavigationLink(value: file) {
Label(file.relativePath, systemImage: file.icon)
}
}
.navigationDestination(for: CodeFile.self) { file in
CodeFileDetailView(file: file)
}
}
private func loadFiles() {
let circuitDir = CircuitStorage.shared.directory(for: circuit)
let fm = FileManager.default
guard let enumerator = fm.enumerator(
at: circuitDir,
includingPropertiesForKeys: [.isRegularFileKey],
options: [.skipsHiddenFiles]
) else {
isLoading = false
return
}
var loadedFiles: [CodeFile] = []
for case let fileURL as URL in enumerator {
// Skip .circuit metadata directory
let relativePath = fileURL.path.replacingOccurrences(
of: circuitDir.path + "/",
with: ""
)
if relativePath.hasPrefix(".circuit") {
continue
}
// Skip circuit.json manifest
if relativePath == "circuit.json" {
continue
}
// Check if it's a file with a code extension
let ext = fileURL.pathExtension.lowercased()
guard codeExtensions.contains(ext) else { continue }
// Load content
guard let content = try? String(contentsOf: fileURL, encoding: .utf8) else {
continue
}
let file = CodeFile(
url: fileURL,
relativePath: relativePath,
content: content
)
loadedFiles.append(file)
}
// Sort alphabetically by relative path
files = loadedFiles.sorted { $0.relativePath < $1.relativePath }
isLoading = false
}
}
/// Detail view showing the content of a code file
struct CodeFileDetailView: View {
let file: CodeFile
var body: some View {
ScrollView([.horizontal, .vertical]) {
Text(file.content)
.font(.system(.body, design: .monospaced))
.textSelection(.enabled)
.padding()
.frame(maxWidth: .infinity, alignment: .leading)
}
.navigationTitle(file.url.lastPathComponent)
#if os(iOS)
.navigationBarTitleDisplayMode(.inline)
#endif
}
}