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 {
371371
372372 /// A commented file with every setting at its current value.
373373 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 {
374381 var out = """
375382 # Orgstar settings. The Settings window writes its changes here, and edits made here
376383 # apply while Orgstar runs. Keys go in keymap.toml, capture templates in capture.toml
@@ -385,7 +392,7 @@ public enum UserConfig {
385392 section = setting.section
386393 out += "\n[\(section)]\n"
387394 }
388 out += "\(setting.key) = \(format(value(setting, in: defaults))) # \(setting.comment)\n"
395 out += "\(setting.key) = \(format(value(setting))) # \(setting.comment)\n"
389396 }
390397 return out
391398 }
Sources/OrgstarMobile/MobileModel.swift +11
@@ -88,6 +88,7 @@ final class MobileModel {
8888 if let url {
8989 _ = url.startAccessingSecurityScopedResource()
9090 if let bookmark = try? url.bookmarkData() { UserDefaults.standard.set(bookmark, forKey: Self.bookmarkKey) }
91 Self.createConfig(in: url)
9192 } else {
9293 UserDefaults.standard.removeObject(forKey: Self.bookmarkKey)
9394 // What the folder set no longer applies.
@@ -177,6 +178,16 @@ final class MobileModel {
177178
178179 /// Applies the folder's `config.toml` to the settings; returns its problems and the theme's
179180 /// 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
180191 private static func readConfig(_ folder: URL?) -> ([String], ThemeSpec) {
181192 guard let folder else { return ([], .default) }
182193 guard let text = read(folder.appendingPathComponent("config.toml")) else {
Tests/OrgAppTests/UserConfigTests.swift +9
@@ -32,6 +32,15 @@ struct UserConfigTests {
3232 #expect(target.bool(forKey: "showLineNumbers") == false)
3333 }
3434
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
3544 @Test func problemsAndRemovedLines() {
3645 let target = defaults()
3746 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
305305The 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.
306306
3073071. Open the Settings tab.
3082. Tap Choose Folder… and choose the folder.
3082. 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.
309309
310Changes 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=.
310Changes 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.
311311
312312Stop Using This Folder returns every setting to its default.
313313