Sources/OrgApp/KeymapLoader.swift
22 lines · 985 bytes
2 symbols in this file
1import Foundation
2import OrgCore
3
4@MainActor
5public enum KeymapLoader {
6 /// `<data dir>/keymap.toml`.
7 public static var userFile: URL { UserConfig.file("keymap.toml") }
8
9 /// The preset with the user's file on top, and the problems found in the file. A missing
10 /// file adds nothing; a file that isn't valid TOML adds nothing and is reported. With
11 /// `commands`, bindings of other command ids are reported and skipped.
12 public static func load(_ preset: KeymapPreset, userFile: URL = userFile, commands: Set<String>? = nil) -> (keymap: Keymap, problems: [String]) {
13 guard let text = try? String(contentsOf: userFile, encoding: .utf8) else { return (preset.keymap, []) }
14 var problems: [String] = []
15 do {
16 let user = try Keymap(toml: text, problems: &problems, commands: commands)
17 return (.layered([preset.keymap, user]), problems)
18 } catch {
19 return (preset.keymap, ["\(error)"])
20 }
21 }
22}