Commit 49cb8f339f
49cb8f339ff4526b33437e3b9ccc1aa62b1271ae
parent: b98a6509c5
Verified · cmc
cmc <hello@cleberg.net> · 2026-10-10 03:26 UTC
iOS: write a default config.toml into a chosen folder that has none
Layout: unified · split
Sources/OrgApp/UserConfig.swift
+8 −1
| @@ -371,6 +371,13 @@ public enum UserConfig { |
| 371 | |
371 | |
| 372 | /// A commented file with every setting at its current value. |
372 | /// A commented file with every setting at its current value. |
| 373 | public static func template(from defaults: UserDefaults) -> String { |
373 | public static func template(from defaults: UserDefaults) -> String { |
| |
374 | template { value($0, in: defaults) } |
| |
375 | } |
| |
376 | |
| |
377 | /// A commented file with every setting at its default. |
| |
378 | public static var defaultTemplate: String { template(\.fallback) } |
| |
379 | |
| |
380 | private static func template(_ value: (Setting) -> TOML.Value) -> String { |
| 374 | var out = """ |
381 | var out = """ |
| 375 | # Orgstar settings. The Settings window writes its changes here, and edits made here |
382 | # Orgstar settings. The Settings window writes its changes here, and edits made here |
| 376 | # apply while Orgstar runs. Keys go in keymap.toml, capture templates in capture.toml |
383 | # apply while Orgstar runs. Keys go in keymap.toml, capture templates in capture.toml |
| @@ -385,7 +392,7 @@ public enum UserConfig { |
| 385 | section = setting.section |
392 | section = setting.section |
| 386 | out += "\n[\(section)]\n" |
393 | out += "\n[\(section)]\n" |
| 387 | } |
394 | } |
| 388 | out += "\(setting.key) = \(format(value(setting, in: defaults))) # \(setting.comment)\n" |
395 | out += "\(setting.key) = \(format(value(setting))) # \(setting.comment)\n" |
| 389 | } |
396 | } |
| 390 | return out |
397 | return out |
| 391 | } |
398 | } |
Sources/OrgstarMobile/MobileModel.swift
+11
| @@ -88,6 +88,7 @@ final class MobileModel { |
| 88 | if let url { |
88 | if let url { |
| 89 | _ = url.startAccessingSecurityScopedResource() |
89 | _ = url.startAccessingSecurityScopedResource() |
| 90 | if let bookmark = try? url.bookmarkData() { UserDefaults.standard.set(bookmark, forKey: Self.bookmarkKey) } |
90 | if let bookmark = try? url.bookmarkData() { UserDefaults.standard.set(bookmark, forKey: Self.bookmarkKey) } |
| |
91 | Self.createConfig(in: url) |
| 91 | } else { |
92 | } else { |
| 92 | UserDefaults.standard.removeObject(forKey: Self.bookmarkKey) |
93 | UserDefaults.standard.removeObject(forKey: Self.bookmarkKey) |
| 93 | // What the folder set no longer applies. |
94 | // What the folder set no longer applies. |
| @@ -177,6 +178,16 @@ final class MobileModel { |
| 177 | |
178 | |
| 178 | /// Applies the folder's `config.toml` to the settings; returns its problems and the theme's |
179 | /// Applies the folder's `config.toml` to the settings; returns its problems and the theme's |
| 179 | /// colors. |
180 | /// colors. |
| |
181 | /// Writes `config.toml` with every setting at its default when the folder has none. |
| |
182 | private static func createConfig(in folder: URL) { |
| |
183 | let file = folder.appendingPathComponent("config.toml") |
| |
184 | var error: NSError? |
| |
185 | NSFileCoordinator(filePresenter: nil).coordinate(writingItemAt: file, options: [], error: &error) { url in |
| |
186 | guard !FileManager.default.fileExists(atPath: url.path) else { return } |
| |
187 | try? UserConfig.defaultTemplate.write(to: url, atomically: true, encoding: .utf8) |
| |
188 | } |
| |
189 | } |
| |
190 | |
| 180 | private static func readConfig(_ folder: URL?) -> ([String], ThemeSpec) { |
191 | private static func readConfig(_ folder: URL?) -> ([String], ThemeSpec) { |
| 181 | guard let folder else { return ([], .default) } |
192 | guard let folder else { return ([], .default) } |
| 182 | guard let text = read(folder.appendingPathComponent("config.toml")) else { |
193 | guard let text = read(folder.appendingPathComponent("config.toml")) else { |
Tests/OrgAppTests/UserConfigTests.swift
+9
| @@ -32,6 +32,15 @@ struct UserConfigTests { |
| 32 | #expect(target.bool(forKey: "showLineNumbers") == false) |
32 | #expect(target.bool(forKey: "showLineNumbers") == false) |
| 33 | } |
33 | } |
| 34 | |
34 | |
| |
35 | @Test func defaultTemplateHasEveryDefault() { |
| |
36 | #expect(UserConfig.defaultTemplate == UserConfig.template(from: defaults())) |
| |
37 | let target = defaults() |
| |
38 | #expect(UserConfig.apply(UserConfig.defaultTemplate, to: target).isEmpty) |
| |
39 | for setting in UserConfig.settings { |
| |
40 | #expect(UserConfig.value(setting, in: target) == setting.fallback, "\(setting.key)") |
| |
41 | } |
| |
42 | } |
| |
43 | |
| 35 | @Test func problemsAndRemovedLines() { |
44 | @Test func problemsAndRemovedLines() { |
| 36 | let target = defaults() |
45 | let target = defaults() |
| 37 | target.set(60, forKey: "fillColumn") |
46 | target.set(60, forKey: "fillColumn") |
docs/manual/guide/14-ios.org
+2 −2
| @@ -305,9 +305,9 @@ See [[file:15-alongside-emacs.org][Working alongside Emacs and other tools]] for |
| 305 | The iOS app reads its settings from a configuration folder: a folder holding =config.toml= and =capture.toml=, such as a copy of the Mac's =~/.config/orgstar= synced through iCloud Drive or another app. |
305 | The iOS app reads its settings from a configuration folder: a folder holding =config.toml= and =capture.toml=, such as a copy of the Mac's =~/.config/orgstar= synced through iCloud Drive or another app. |
| 306 | |
306 | |
| 307 | 1. Open the Settings tab. |
307 | 1. Open the Settings tab. |
| 308 | 2. Tap Choose Folder… and choose the folder. |
308 | 2. Tap Choose Folder… and choose the folder. If it has no =config.toml=, Orgstar writes one with every setting at its default, each with a comment, as the Mac does. |
| 309 | |
309 | |
| 310 | Changes to the files apply as they sync, and again each time you return to the app. Problems in the files show in red under the folder. "No config.toml in /folder/; the defaults apply" means the folder has no =config.toml=. |
310 | Changes to the files apply as they sync, and again each time you return to the app. Problems in the files show in red under the folder. "No config.toml in /folder/; the defaults apply" means the folder has no =config.toml=, for example because it was removed after you chose the folder. |
| 311 | |
311 | |
| 312 | Stop Using This Folder returns every setting to its default. |
312 | Stop Using This Folder returns every setting to its default. |
| 313 | |
313 | |