restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
|
||||
public protocol RecentProjectsStoring: Sendable {
|
||||
/// Newest first; projects whose folder no longer exists are left out.
|
||||
func list() async -> [RecentProject]
|
||||
/// Moves a project folder to the front of the list.
|
||||
func add(_ path: String) async
|
||||
}
|
||||
|
||||
/// Machine-wide list in `~/Library/Application Support/Helder/recent-projects.json`.
|
||||
public struct RecentProjectsStore: RecentProjectsStoring {
|
||||
public let fileURL: URL
|
||||
|
||||
public init(fileURL: URL = RecentProjectsStore.defaultURL) {
|
||||
self.fileURL = fileURL
|
||||
}
|
||||
|
||||
public static var defaultURL: URL {
|
||||
let support = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first
|
||||
?? URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Library/Application Support")
|
||||
return support.appendingPathComponent("Helder/recent-projects.json")
|
||||
}
|
||||
|
||||
public func list() async -> [RecentProject] {
|
||||
guard let data = try? Data(contentsOf: fileURL),
|
||||
let list = try? JSONDecoder().decode([RecentProject].self, from: data) else { return [] }
|
||||
return Array(list.filter { FileManager.default.fileExists(atPath: $0.path) }.prefix(RecentLists.maxProjects))
|
||||
}
|
||||
|
||||
public func add(_ path: String) async {
|
||||
let abs = URL(fileURLWithPath: path).standardizedFileURL.path
|
||||
let entry = RecentProject(path: abs, name: PathUtils.basename(abs).isEmpty ? abs : PathUtils.basename(abs))
|
||||
let next = RecentLists.pushing(entry, onto: await list())
|
||||
do {
|
||||
try FileManager.default.createDirectory(at: fileURL.deletingLastPathComponent(), withIntermediateDirectories: true)
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes]
|
||||
try encoder.encode(next).write(to: fileURL, options: .atomic)
|
||||
} catch {
|
||||
Log.warn("recent", "cannot save recent projects: \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public protocol RecentFilesStoring: Sendable {
|
||||
/// Recently opened files of this project, newest first.
|
||||
func load() async -> [String]
|
||||
func save(_ paths: [String]) async
|
||||
}
|
||||
|
||||
/// Per-project list in `.helder/recent.json`; drives the ⌘↑/⌘↓ recent-files navigator.
|
||||
public struct RecentFilesStore: RecentFilesStoring {
|
||||
public let fileURL: URL
|
||||
|
||||
public init(root: URL) {
|
||||
fileURL = root.appendingPathComponent(HelderConfig.folderName).appendingPathComponent(RecentLists.recentFilesName)
|
||||
}
|
||||
|
||||
public func load() async -> [String] {
|
||||
guard let data = try? Data(contentsOf: fileURL),
|
||||
let list = try? JSONDecoder().decode([String].self, from: data) else { return [] }
|
||||
return Array(list.prefix(RecentLists.maxFiles))
|
||||
}
|
||||
|
||||
public func save(_ paths: [String]) async {
|
||||
do {
|
||||
try FileManager.default.createDirectory(at: fileURL.deletingLastPathComponent(), withIntermediateDirectories: true)
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes]
|
||||
try encoder.encode(Array(paths.prefix(RecentLists.maxFiles))).write(to: fileURL, options: .atomic)
|
||||
} catch {
|
||||
Log.warn("recent", "cannot save recent files: \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user