Commit abcaabca33
Verified · cmc
Layout: unified · split
.qlty/qlty.toml +9
| @@ -58,3 +58,12 @@ match.plugins = ["swiftlint"] | |||
| 58 | match.rules = ["swiftlint:force_try"] | 58 | match.rules = ["swiftlint:force_try"] |
| 59 | match.file_patterns = ["Tests/**"] | 59 | match.file_patterns = ["Tests/**"] |
| 60 | set.ignored = true | 60 | set.ignored = true |
| 61 | |||
| 62 | # Exhaustive switch-over-enum lookup tables and the light and dark color tables share shape but not content. | ||
| 63 | [language.swift.smells.duplication] | ||
| 64 | filter_patterns = ["(import_declaration _)", "(switch_statement _)", "(dictionary_literal _)"] | ||
| 65 | |||
| 66 | # tree-sitter-swift nests each `else if` inside the previous branch, so a one-line `if` in the | ||
| 67 | # fourth branch of a flat chain reports level 5. | ||
| 68 | [language.swift.smells.nested_control_flow] | ||
| 69 | enabled = false | ||
Sources/OrgApp/ColumnViewGrid.swift added +31
| @@ -0,0 +1,31 @@ | |||
| 1 | import OrgCore | ||
| 2 | import SwiftUI | ||
| 3 | |||
| 4 | /// `org-columns` laid out: a row of column titles, a rule, then a row per entry, each cell from `cell(row, column)`. | ||
| 5 | public struct ColumnViewGrid<Cell: View>: View { | ||
| 6 | let view: ColumnView.View | ||
| 7 | let verticalSpacing: CGFloat | ||
| 8 | let cell: (Int, Int) -> Cell | ||
| 9 | |||
| 10 | public init(view: ColumnView.View, verticalSpacing: CGFloat, @ViewBuilder cell: @escaping (Int, Int) -> Cell) { | ||
| 11 | self.view = view | ||
| 12 | self.verticalSpacing = verticalSpacing | ||
| 13 | self.cell = cell | ||
| 14 | } | ||
| 15 | |||
| 16 | public var body: some View { | ||
| 17 | Grid(alignment: .leading, horizontalSpacing: 14, verticalSpacing: verticalSpacing) { | ||
| 18 | GridRow { | ||
| 19 | ForEach(view.specs.indices, id: \.self) { i in | ||
| 20 | Text(view.specs[i].title).bold().foregroundStyle(.secondary) | ||
| 21 | } | ||
| 22 | } | ||
| 23 | Divider() | ||
| 24 | ForEach(view.rows.indices, id: \.self) { r in | ||
| 25 | GridRow { | ||
| 26 | ForEach(view.specs.indices, id: \.self) { c in cell(r, c) } | ||
| 27 | } | ||
| 28 | } | ||
| 29 | } | ||
| 30 | } | ||
| 31 | } | ||
Sources/OrgApp/DiffView.swift added +79
| @@ -0,0 +1,79 @@ | |||
| 1 | import OrgDocument | ||
| 2 | import SwiftUI | ||
| 3 | |||
| 4 | /// Lines removed and added between two versions, with a few lines of context around each change. | ||
| 5 | public struct DiffView: View { | ||
| 6 | let lines: [DiffLine] | ||
| 7 | var context = 3 | ||
| 8 | |||
| 9 | public init(old: String, new: String) { | ||
| 10 | lines = lineDiff(old: old, new: new) | ||
| 11 | } | ||
| 12 | |||
| 13 | /// Lines to show, nil standing for a run of unchanged lines left out. | ||
| 14 | private var shown: [DiffLine?] { | ||
| 15 | let changed = lines.indices.filter { lines[$0].kind != .same } | ||
| 16 | guard !changed.isEmpty else { return [] } | ||
| 17 | var keep = Set<Int>() | ||
| 18 | for i in changed { keep.formUnion(max(0, i - context)...min(lines.count - 1, i + context)) } | ||
| 19 | var out: [DiffLine?] = [] | ||
| 20 | var skipped = false | ||
| 21 | for i in lines.indices { | ||
| 22 | if keep.contains(i) { | ||
| 23 | out.append(lines[i]) | ||
| 24 | skipped = false | ||
| 25 | } else if !skipped { | ||
| 26 | out.append(nil) | ||
| 27 | skipped = true | ||
| 28 | } | ||
| 29 | } | ||
| 30 | return out | ||
| 31 | } | ||
| 32 | |||
| 33 | public var body: some View { | ||
| 34 | let shown = shown | ||
| 35 | ScrollView([.vertical, .horizontal]) { | ||
| 36 | VStack(alignment: .leading, spacing: 0) { | ||
| 37 | if shown.isEmpty { Text("No differences").foregroundStyle(.secondary).padding(8) } | ||
| 38 | ForEach(shown.indices, id: \.self) { i in | ||
| 39 | if let line = shown[i] { | ||
| 40 | Text(prefix(line.kind) + line.text.trimmingCharacters(in: .newlines)) | ||
| 41 | .frame(maxWidth: .infinity, alignment: .leading) | ||
| 42 | .background(background(line.kind)) | ||
| 43 | } else { | ||
| 44 | Text("⋯").foregroundStyle(.secondary) | ||
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
| 48 | #if os(macOS) | ||
| 49 | .font(.system(.callout, design: .monospaced)) | ||
| 50 | #else | ||
| 51 | .font(.system(.footnote, design: .monospaced)) | ||
| 52 | #endif | ||
| 53 | .textSelection(.enabled) | ||
| 54 | .padding(8) | ||
| 55 | } | ||
| 56 | .defaultScrollAnchor(.topLeading) | ||
| 57 | #if os(macOS) | ||
| 58 | .background(Color(nsColor: .textBackgroundColor)) | ||
| 59 | #else | ||
| 60 | .background(Color(uiColor: .secondarySystemBackground)) | ||
| 61 | #endif | ||
| 62 | } | ||
| 63 | |||
| 64 | private func prefix(_ kind: DiffLine.Kind) -> String { | ||
| 65 | switch kind { | ||
| 66 | case .same: " " | ||
| 67 | case .removed: "- " | ||
| 68 | case .added: "+ " | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | private func background(_ kind: DiffLine.Kind) -> Color { | ||
| 73 | switch kind { | ||
| 74 | case .same: .clear | ||
| 75 | case .removed: .red.opacity(0.18) | ||
| 76 | case .added: .green.opacity(0.18) | ||
| 77 | } | ||
| 78 | } | ||
| 79 | } | ||
Sources/OrgApp/EmacsImportSections.swift added +60
| @@ -0,0 +1,60 @@ | |||
| 1 | import SwiftUI | ||
| 2 | |||
| 3 | extension EmacsImport { | ||
| 4 | /// The items by kind of change, empty kinds left out; agenda folders only when `folders`. | ||
| 5 | public func groups(folders: Bool) -> [(String, [Item])] { | ||
| 6 | [ | ||
| 7 | ("Settings", items.filter { if case .setting = $0.change { true } else { false } }), | ||
| 8 | ("Folders", folders ? items.filter { if case .folder = $0.change { true } else { false } } : []), | ||
| 9 | ("Capture templates", items.filter { if case .capture = $0.change { true } else { false } }), | ||
| 10 | ("Key bindings", items.filter { if case .binding = $0.change { true } else { false } }), | ||
| 11 | ("Structure templates", items.filter { if case .structureTemplate = $0.change { true } else { false } }), | ||
| 12 | ("Agenda views", items.filter { if case .view = $0.change { true } else { false } }), | ||
| 13 | ].filter { !$0.1.isEmpty } | ||
| 14 | } | ||
| 15 | } | ||
| 16 | |||
| 17 | /// A section of toggles per group of an import, then what wasn't imported and why. | ||
| 18 | public struct EmacsImportSections: View { | ||
| 19 | let result: EmacsImport | ||
| 20 | let groups: [(String, [EmacsImport.Item])] | ||
| 21 | @Binding var selected: Set<Int> | ||
| 22 | let font: Font | ||
| 23 | |||
| 24 | public init(result: EmacsImport, groups: [(String, [EmacsImport.Item])], selected: Binding<Set<Int>>, font: Font) { | ||
| 25 | self.result = result | ||
| 26 | self.groups = groups | ||
| 27 | _selected = selected | ||
| 28 | self.font = font | ||
| 29 | } | ||
| 30 | |||
| 31 | public var body: some View { | ||
| 32 | ForEach(groups, id: \.0) { title, items in | ||
| 33 | Section(title) { | ||
| 34 | ForEach(items) { item in | ||
| 35 | Toggle(isOn: Binding { selected.contains(item.id) } set: { on in | ||
| 36 | if on { selected.insert(item.id) } else { selected.remove(item.id) } | ||
| 37 | }) { | ||
| 38 | VStack(alignment: .leading, spacing: 1) { | ||
| 39 | Text(item.summary).font(font) | ||
| 40 | Text([item.name == item.summary ? nil : item.name, item.source.isEmpty ? nil : item.source].compactMap { $0 }.joined(separator: " · ")) | ||
| 41 | .font(.caption).foregroundStyle(.secondary) | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | } | ||
| 47 | let notes = result.unsupported.map { ($0.id, $0.name, $0.reason, $0.source) } + result.unreadable.map { ($0.id, $0.name, $0.reason, $0.source) } | ||
| 48 | if !notes.isEmpty || !result.problems.isEmpty { | ||
| 49 | Section("Not imported") { | ||
| 50 | ForEach(result.problems, id: \.self) { Text($0).foregroundStyle(.red) } | ||
| 51 | ForEach(notes, id: \.0) { note in | ||
| 52 | VStack(alignment: .leading, spacing: 1) { | ||
| 53 | Text(note.1).font(font) | ||
| 54 | Text("\(note.2) · \(note.3)").font(.caption).foregroundStyle(.secondary) | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | } | ||
Sources/OrgApp/Publish.swift +28 −16
| @@ -270,27 +270,13 @@ public enum Publish { | |||
| 270 | let relative = String(file.dropFirst(base.count)) | 270 | let relative = String(file.dropFirst(base.count)) |
| 271 | let outputDirectory = directory(p.publishingDirectory) + ((relative as NSString).deletingLastPathComponent.isEmpty ? "" : (relative as NSString).deletingLastPathComponent + "/") | 271 | let outputDirectory = directory(p.publishingDirectory) + ((relative as NSString).deletingLastPathComponent.isEmpty ? "" : (relative as NSString).deletingLastPathComponent + "/") |
| 272 | for function in p.publishingFunctions { | 272 | for function in p.publishingFunctions { |
| 273 | let output: String | 273 | guard let output = try outputPath(file, function: function, in: outputDirectory, read: read) else { continue } |
| 274 | if function == "org-publish-attachment" { | ||
| 275 | output = outputDirectory + (file as NSString).lastPathComponent | ||
| 276 | if directory((file as NSString).deletingLastPathComponent) == outputDirectory { continue } | ||
| 277 | } else { | ||
| 278 | let ext = function == "org-md-publish-to-md" ? "md" : "html" | ||
| 279 | guard let text = read(file) else { throw Failure(description: "Can't read \(file)") } | ||
| 280 | output = ExportScope.output(for: URL(fileURLWithPath: outputDirectory + (file as NSString).lastPathComponent), | ||
| 281 | name: ExportScope.fileName(text), extension: ext).path | ||
| 282 | } | ||
| 283 | if !force, upToDate(output, source: file) { | 274 | if !force, upToDate(output, source: file) { |
| 284 | report.skipped.append(file) | 275 | report.skipped.append(file) |
| 285 | continue | 276 | continue |
| 286 | } | 277 | } |
| 287 | try fm.createDirectory(atPath: (output as NSString).deletingLastPathComponent, withIntermediateDirectories: true) | 278 | try fm.createDirectory(atPath: (output as NSString).deletingLastPathComponent, withIntermediateDirectories: true) |
| 288 | if function == "org-publish-attachment" { | 279 | try write(file, to: output, function: function, bodyOnly: p.bodyOnly, read: read, export: export) |
| 289 | if fm.fileExists(atPath: output) { try fm.removeItem(atPath: output) } | ||
| 290 | try fm.copyItem(atPath: file, toPath: output) | ||
| 291 | } else { | ||
| 292 | try export(function, read(file) ?? "", file, p.bodyOnly).write(toFile: output, atomically: true, encoding: .utf8) | ||
| 293 | } | ||
| 294 | report.published.append(file) | 280 | report.published.append(file) |
| 295 | } | 281 | } |
| 296 | } | 282 | } |
| @@ -298,6 +284,32 @@ public enum Publish { | |||
| 298 | return report | 284 | return report |
| 299 | } | 285 | } |
| 300 | 286 | ||
| 287 | /// Where `function` publishes `file` in `outputDirectory`; nil for an attachment already there. | ||
| 288 | private static func outputPath(_ file: String, function: String, in outputDirectory: String, read: (String) -> String?) throws -> String? { | ||
| 289 | guard function != "org-publish-attachment" else { | ||
| 290 | if directory((file as NSString).deletingLastPathComponent) == outputDirectory { return nil } | ||
| 291 | return outputDirectory + (file as NSString).lastPathComponent | ||
| 292 | } | ||
| 293 | let ext = function == "org-md-publish-to-md" ? "md" : "html" | ||
| 294 | guard let text = read(file) else { throw Failure(description: "Can't read \(file)") } | ||
| 295 | return ExportScope.output(for: URL(fileURLWithPath: outputDirectory + (file as NSString).lastPathComponent), | ||
| 296 | name: ExportScope.fileName(text), extension: ext).path | ||
| 297 | } | ||
| 298 | |||
| 299 | /// Copies an attachment to `output`, or writes what `function` exports from `file`. | ||
| 300 | private static func write( | ||
| 301 | _ file: String, to output: String, function: String, bodyOnly: Bool, | ||
| 302 | read: (String) -> String?, export: (_ function: String, _ text: String, _ path: String, _ bodyOnly: Bool) -> String | ||
| 303 | ) throws { | ||
| 304 | let fm = FileManager.default | ||
| 305 | if function == "org-publish-attachment" { | ||
| 306 | if fm.fileExists(atPath: output) { try fm.removeItem(atPath: output) } | ||
| 307 | try fm.copyItem(atPath: file, toPath: output) | ||
| 308 | } else { | ||
| 309 | try export(function, read(file) ?? "", file, bodyOnly).write(toFile: output, atomically: true, encoding: .utf8) | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 301 | static func upToDate(_ output: String, source: String) -> Bool { | 313 | static func upToDate(_ output: String, source: String) -> Bool { |
| 302 | let fm = FileManager.default | 314 | let fm = FileManager.default |
| 303 | guard let out = try? fm.attributesOfItem(atPath: output)[.modificationDate] as? Date, | 315 | guard let out = try? fm.attributesOfItem(atPath: output)[.modificationDate] as? Date, |
Sources/OrgCore/Commands/ElectricPair.swift +29 −23
| @@ -277,38 +277,44 @@ private final class PairBuffer { | |||
| 277 | return (innermost!, outermost!) | 277 | return (innermost!, outermost!) |
| 278 | } | 278 | } |
| 279 | 279 | ||
| 280 | /// `electric-pair-inhibit-if-helps-balance`, with the typed character before point. | 280 | /// Runs `check` with the typed character taken out from before point, as electric-pair's |
| 281 | func inhibit(_ c: unichar) -> Bool { | 281 | /// balance checks do, and puts it back. |
| 282 | private func withoutTyped(_ c: unichar, _ check: (Info) -> Bool) -> Bool { | ||
| 282 | guard let info = info(c) else { return false } | 283 | guard let info = info(c) else { return false } |
| 283 | let at = point - 1 | 284 | let at = point - 1 |
| 284 | delete(at..<point) | 285 | delete(at..<point) |
| 285 | defer { insert(String(utf16CodeUnits: [c], count: 1), at: at) } | 286 | defer { insert(String(utf16CodeUnits: [c], count: 1), at: at) } |
| 286 | switch info.syntax { | 287 | return check(info) |
| 287 | case .open: | 288 | } |
| 288 | let outermost = balance(forward: true, stringStart: info.stringStart).outermost | 289 | |
| 289 | return outermost.matched ? false : outermost.pair == info.pair | 290 | /// `electric-pair-inhibit-if-helps-balance`, with the typed character before point. |
| 290 | case .string: | 291 | func inhibit(_ c: unichar) -> Bool { |
| 291 | return ppss(length).stringChar == c | 292 | withoutTyped(c) { info in |
| 292 | case .close: | 293 | switch info.syntax { |
| 293 | return false | 294 | case .open: |
| 295 | let outermost = balance(forward: true, stringStart: info.stringStart).outermost | ||
| 296 | return outermost.matched ? false : outermost.pair == info.pair | ||
| 297 | case .string: | ||
| 298 | return ppss(length).stringChar == c | ||
| 299 | case .close: | ||
| 300 | return false | ||
| 301 | } | ||
| 294 | } | 302 | } |
| 295 | } | 303 | } |
| 296 | 304 | ||
| 297 | /// `electric-pair-skip-if-helps-balance`, with the typed character before point. | 305 | /// `electric-pair-skip-if-helps-balance`, with the typed character before point. |
| 298 | func skip(_ c: unichar) -> Bool { | 306 | func skip(_ c: unichar) -> Bool { |
| 299 | guard let info = info(c) else { return false } | 307 | withoutTyped(c) { info in |
| 300 | let at = point - 1 | 308 | switch info.syntax { |
| 301 | delete(at..<point) | 309 | case .close: |
| 302 | defer { insert(String(utf16CodeUnits: [c], count: 1), at: at) } | 310 | let (innermost, outermost) = balance(forward: false, stringStart: info.stringStart) |
| 303 | switch info.syntax { | 311 | if outermost.matched { return innermost.matched } |
| 304 | case .close: | 312 | return innermost.matched && outermost.pair != info.pair |
| 305 | let (innermost, outermost) = balance(forward: false, stringStart: info.stringStart) | 313 | case .string: |
| 306 | if outermost.matched { return innermost.matched } | 314 | return ppss(point).stringChar == c |
| 307 | return innermost.matched && outermost.pair != info.pair | 315 | case .open: |
| 308 | case .string: | 316 | return false |
| 309 | return ppss(point).stringChar == c | 317 | } |
| 310 | case .open: | ||
| 311 | return false | ||
| 312 | } | 318 | } |
| 313 | } | 319 | } |
| 314 | 320 | ||
Sources/Orgstar/ColumnViewSheet.swift +2 −13
| @@ -1,3 +1,4 @@ | |||
| 1 | import OrgApp | ||
| 1 | import OrgCore | 2 | import OrgCore |
| 2 | import SwiftUI | 3 | import SwiftUI |
| 3 | 4 | ||
| @@ -37,19 +38,7 @@ struct ColumnViewSheet: View { | |||
| 37 | .padding(10) | 38 | .padding(10) |
| 38 | Divider() | 39 | Divider() |
| 39 | ScrollView([.horizontal, .vertical]) { | 40 | ScrollView([.horizontal, .vertical]) { |
| 40 | Grid(alignment: .leading, horizontalSpacing: 14, verticalSpacing: 4) { | 41 | ColumnViewGrid(view: view, verticalSpacing: 4) { r, c in cellView(r, c) } |
| 41 | GridRow { | ||
| 42 | ForEach(view.specs.indices, id: \.self) { i in | ||
| 43 | Text(view.specs[i].title).bold().foregroundStyle(.secondary) | ||
| 44 | } | ||
| 45 | } | ||
| 46 | Divider() | ||
| 47 | ForEach(view.rows.indices, id: \.self) { r in | ||
| 48 | GridRow { | ||
| 49 | ForEach(view.specs.indices, id: \.self) { c in cellView(r, c) } | ||
| 50 | } | ||
| 51 | } | ||
| 52 | } | ||
| 53 | .font(.system(.body, design: .monospaced)) | 42 | .font(.system(.body, design: .monospaced)) |
| 54 | .padding(10) | 43 | .padding(10) |
| 55 | } | 44 | } |
Sources/Orgstar/EmacsImportView.swift +3 −36
| @@ -65,42 +65,9 @@ struct EmacsImportView: View { | |||
| 65 | } | 65 | } |
| 66 | 66 | ||
| 67 | private func list(_ result: EmacsImport) -> some View { | 67 | private func list(_ result: EmacsImport) -> some View { |
| 68 | let groups: [(String, [EmacsImport.Item])] = [ | 68 | List { |
| 69 | ("Settings", result.items.filter { if case .setting = $0.change { true } else { false } }), | 69 | EmacsImportSections(result: result, groups: result.groups(folders: true), selected: $selected, |
| 70 | ("Folders", result.items.filter { if case .folder = $0.change { true } else { false } }), | 70 | font: .system(.body, design: .monospaced)) |
| 71 | ("Capture templates", result.items.filter { if case .capture = $0.change { true } else { false } }), | ||
| 72 | ("Key bindings", result.items.filter { if case .binding = $0.change { true } else { false } }), | ||
| 73 | ("Structure templates", result.items.filter { if case .structureTemplate = $0.change { true } else { false } }), | ||
| 74 | ("Agenda views", result.items.filter { if case .view = $0.change { true } else { false } }), | ||
| 75 | ] | ||
| 76 | return List { | ||
| 77 | ForEach(groups.filter { !$0.1.isEmpty }, id: \.0) { title, items in | ||
| 78 | Section(title) { | ||
| 79 | ForEach(items) { item in | ||
| 80 | Toggle(isOn: Binding { selected.contains(item.id) } set: { on in | ||
| 81 | if on { selected.insert(item.id) } else { selected.remove(item.id) } | ||
| 82 | }) { | ||
| 83 | VStack(alignment: .leading, spacing: 1) { | ||
| 84 | Text(item.summary).font(.system(.body, design: .monospaced)) | ||
| 85 | Text([item.name == item.summary ? nil : item.name, item.source.isEmpty ? nil : item.source].compactMap { $0 }.joined(separator: " · ")) | ||
| 86 | .font(.caption).foregroundStyle(.secondary) | ||
| 87 | } | ||
| 88 | } | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | let notes = result.unsupported.map { ($0.id, $0.name, $0.reason, $0.source) } + result.unreadable.map { ($0.id, $0.name, $0.reason, $0.source) } | ||
| 93 | if !notes.isEmpty || !result.problems.isEmpty { | ||
| 94 | Section("Not imported") { | ||
| 95 | ForEach(result.problems, id: \.self) { Text($0).foregroundStyle(.red) } | ||
| 96 | ForEach(notes, id: \.0) { note in | ||
| 97 | VStack(alignment: .leading, spacing: 1) { | ||
| 98 | Text(note.1).font(.system(.body, design: .monospaced)) | ||
| 99 | Text("\(note.2) · \(note.3)").font(.caption).foregroundStyle(.secondary) | ||
| 100 | } | ||
| 101 | } | ||
| 102 | } | ||
| 103 | } | ||
| 104 | } | 71 | } |
| 105 | } | 72 | } |
| 106 | 73 | ||
Sources/Orgstar/OrgstarApp.swift +21 −28
| @@ -844,20 +844,7 @@ struct SettingsView: View { | |||
| 844 | Section { | 844 | Section { |
| 845 | Toggle("Show events from Calendar", isOn: $calendarEvents) | 845 | Toggle("Show events from Calendar", isOn: $calendarEvents) |
| 846 | if calendarEvents { | 846 | if calendarEvents { |
| 847 | if calendars.isEmpty { | 847 | choices(calendars, empty: CalendarEvents.isAuthorized ? "No calendars." : "Allow orgstar to read your calendars in System Settings ▸ Privacy & Security ▸ Calendars.", setting: $calendarEventCalendars) |
| 848 | Text(CalendarEvents.isAuthorized ? "No calendars." : "Allow orgstar to read your calendars in System Settings ▸ Privacy & Security ▸ Calendars.") | ||
| 849 | .foregroundStyle(.secondary) | ||
| 850 | } | ||
| 851 | ForEach(calendars, id: \.id) { calendar in | ||
| 852 | Toggle(isOn: chosen(calendar, among: calendars, setting: $calendarEventCalendars)) { | ||
| 853 | HStack(spacing: 6) { | ||
| 854 | Circle() | ||
| 855 | .fill(calendar.color.map { Color(.sRGB, red: $0.red, green: $0.green, blue: $0.blue, opacity: $0.alpha) } ?? .secondary) | ||
| 856 | .frame(width: 8, height: 8) | ||
| 857 | Text(calendar.title) | ||
| 858 | } | ||
| 859 | } | ||
| 860 | } | ||
| 861 | } | 848 | } |
| 862 | } footer: { | 849 | } footer: { |
| 863 | Text("Events show in the agenda; double-click one to open it in Calendar, or choose Edit Event… in its context menu. New calendars show while every calendar is checked.") | 850 | Text("Events show in the agenda; double-click one to open it in Calendar, or choose Edit Event… in its context menu. New calendars show while every calendar is checked.") |
| @@ -870,20 +857,7 @@ struct SettingsView: View { | |||
| 870 | Section { | 857 | Section { |
| 871 | Toggle("Show reminders from Reminders", isOn: $agendaReminders) | 858 | Toggle("Show reminders from Reminders", isOn: $agendaReminders) |
| 872 | if agendaReminders { | 859 | if agendaReminders { |
| 873 | if reminderLists.isEmpty { | 860 | choices(reminderLists, empty: ReminderLists.isAuthorized ? "No lists." : "Allow orgstar to read your reminders in System Settings ▸ Privacy & Security ▸ Reminders.", setting: $agendaReminderLists) |
| 874 | Text(ReminderLists.isAuthorized ? "No lists." : "Allow orgstar to read your reminders in System Settings ▸ Privacy & Security ▸ Reminders.") | ||
| 875 | .foregroundStyle(.secondary) | ||
| 876 | } | ||
| 877 | ForEach(reminderLists, id: \.id) { list in | ||
| 878 | Toggle(isOn: chosen(list, among: reminderLists, setting: $agendaReminderLists)) { | ||
| 879 | HStack(spacing: 6) { | ||
| 880 | Circle() | ||
| 881 | .fill(list.color.map { Color(.sRGB, red: $0.red, green: $0.green, blue: $0.blue, opacity: $0.alpha) } ?? .secondary) | ||
| 882 | .frame(width: 8, height: 8) | ||
| 883 | Text(list.title) | ||
| 884 | } | ||
| 885 | } | ||
| 886 | } | ||
| 887 | } | 861 | } |
| 888 | } footer: { | 862 | } footer: { |
| 889 | Text("Incomplete reminders with a due date show in the agenda read-only, overdue ones under today; double-click one to open it in Reminders.") | 863 | Text("Incomplete reminders with a due date show in the agenda read-only, overdue ones under today; double-click one to open it in Reminders.") |
| @@ -919,6 +893,25 @@ struct SettingsView: View { | |||
| 919 | .frame(height: 560) | 893 | .frame(height: 560) |
| 920 | } | 894 | } |
| 921 | 895 | ||
| 896 | /// A toggle per calendar or reminder list, with its color; `empty` when there are none. | ||
| 897 | @ViewBuilder | ||
| 898 | private func choices(_ calendars: [(id: String, title: String, color: RGBA?)], empty: LocalizedStringKey, setting: Binding<String>) -> some View { | ||
| 899 | if calendars.isEmpty { | ||
| 900 | Text(empty) | ||
| 901 | .foregroundStyle(.secondary) | ||
| 902 | } | ||
| 903 | ForEach(calendars, id: \.id) { calendar in | ||
| 904 | Toggle(isOn: chosen(calendar, among: calendars, setting: setting)) { | ||
| 905 | HStack(spacing: 6) { | ||
| 906 | Circle() | ||
| 907 | .fill(calendar.color.map { Color(.sRGB, red: $0.red, green: $0.green, blue: $0.blue, opacity: $0.alpha) } ?? .secondary) | ||
| 908 | .frame(width: 8, height: 8) | ||
| 909 | Text(calendar.title) | ||
| 910 | } | ||
| 911 | } | ||
| 912 | } | ||
| 913 | } | ||
| 914 | |||
| 922 | /// `calendar-event-calendars` and `agenda-reminder-lists`: empty for every calendar or | 915 | /// `calendar-event-calendars` and `agenda-reminder-lists`: empty for every calendar or |
| 923 | /// list, else the titles checked. | 916 | /// list, else the titles checked. |
| 924 | private func chosen( | 917 | private func chosen( |
Sources/Orgstar/VersionViews.swift −69
| @@ -3,75 +3,6 @@ import OrgApp | |||
| 3 | import OrgDocument | 3 | import OrgDocument |
| 4 | import SwiftUI | 4 | import SwiftUI |
| 5 | 5 | ||
| 6 | /// Lines removed and added between two versions, with a few lines of context around each change. | ||
| 7 | struct DiffView: View { | ||
| 8 | let lines: [DiffLine] | ||
| 9 | var context = 3 | ||
| 10 | |||
| 11 | init(old: String, new: String) { | ||
| 12 | lines = lineDiff(old: old, new: new) | ||
| 13 | } | ||
| 14 | |||
| 15 | /// Lines to show, nil standing for a run of unchanged lines left out. | ||
| 16 | private var shown: [DiffLine?] { | ||
| 17 | let changed = lines.indices.filter { lines[$0].kind != .same } | ||
| 18 | guard !changed.isEmpty else { return [] } | ||
| 19 | var keep = Set<Int>() | ||
| 20 | for i in changed { keep.formUnion(max(0, i - context)...min(lines.count - 1, i + context)) } | ||
| 21 | var out: [DiffLine?] = [] | ||
| 22 | var skipped = false | ||
| 23 | for i in lines.indices { | ||
| 24 | if keep.contains(i) { | ||
| 25 | out.append(lines[i]) | ||
| 26 | skipped = false | ||
| 27 | } else if !skipped { | ||
| 28 | out.append(nil) | ||
| 29 | skipped = true | ||
| 30 | } | ||
| 31 | } | ||
| 32 | return out | ||
| 33 | } | ||
| 34 | |||
| 35 | var body: some View { | ||
| 36 | let shown = shown | ||
| 37 | ScrollView([.vertical, .horizontal]) { | ||
| 38 | VStack(alignment: .leading, spacing: 0) { | ||
| 39 | if shown.isEmpty { Text("No differences").foregroundStyle(.secondary).padding(8) } | ||
| 40 | ForEach(shown.indices, id: \.self) { i in | ||
| 41 | if let line = shown[i] { | ||
| 42 | Text(prefix(line.kind) + line.text.trimmingCharacters(in: .newlines)) | ||
| 43 | .frame(maxWidth: .infinity, alignment: .leading) | ||
| 44 | .background(background(line.kind)) | ||
| 45 | } else { | ||
| 46 | Text("⋯").foregroundStyle(.secondary) | ||
| 47 | } | ||
| 48 | } | ||
| 49 | } | ||
| 50 | .font(.system(.callout, design: .monospaced)) | ||
| 51 | .textSelection(.enabled) | ||
| 52 | .padding(8) | ||
| 53 | } | ||
| 54 | .defaultScrollAnchor(.topLeading) | ||
| 55 | .background(Color(nsColor: .textBackgroundColor)) | ||
| 56 | } | ||
| 57 | |||
| 58 | private func prefix(_ kind: DiffLine.Kind) -> String { | ||
| 59 | switch kind { | ||
| 60 | case .same: " " | ||
| 61 | case .removed: "- " | ||
| 62 | case .added: "+ " | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | private func background(_ kind: DiffLine.Kind) -> Color { | ||
| 67 | switch kind { | ||
| 68 | case .same: .clear | ||
| 69 | case .removed: .red.opacity(0.18) | ||
| 70 | case .added: .green.opacity(0.18) | ||
| 71 | } | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | /// The buffer and the file on disk changed differently: what differs, and how to settle it. | 6 | /// The buffer and the file on disk changed differently: what differs, and how to settle it. |
| 76 | struct ConflictSheet: View { | 7 | struct ConflictSheet: View { |
| 77 | let session: DocumentSession | 8 | let session: DocumentSession |
Sources/OrgstarMobile/ColumnViewScreen.swift +1 −13
| @@ -19,19 +19,7 @@ struct ColumnViewScreen: View { | |||
| 19 | var body: some View { | 19 | var body: some View { |
| 20 | NavigationStack { | 20 | NavigationStack { |
| 21 | ScrollView([.horizontal, .vertical]) { | 21 | ScrollView([.horizontal, .vertical]) { |
| 22 | Grid(alignment: .leading, horizontalSpacing: 14, verticalSpacing: 6) { | 22 | ColumnViewGrid(view: view, verticalSpacing: 6) { r, c in cellView(r, c) } |
| 23 | GridRow { | ||
| 24 | ForEach(view.specs.indices, id: \.self) { i in | ||
| 25 | Text(view.specs[i].title).bold().foregroundStyle(.secondary) | ||
| 26 | } | ||
| 27 | } | ||
| 28 | Divider() | ||
| 29 | ForEach(view.rows.indices, id: \.self) { r in | ||
| 30 | GridRow { | ||
| 31 | ForEach(view.specs.indices, id: \.self) { c in cellView(r, c) } | ||
| 32 | } | ||
| 33 | } | ||
| 34 | } | ||
| 35 | .font(.system(.callout, design: .monospaced)) | 23 | .font(.system(.callout, design: .monospaced)) |
| 36 | .padding(12) | 24 | .padding(12) |
| 37 | } | 25 | } |
Sources/OrgstarMobile/EmacsImportScreen.swift +3 −38
| @@ -27,33 +27,8 @@ struct EmacsImportScreen: View { | |||
| 27 | if let summary { | 27 | if let summary { |
| 28 | Section { Text(summary) } | 28 | Section { Text(summary) } |
| 29 | } else if let result { | 29 | } else if let result { |
| 30 | ForEach(groups(result), id: \.0) { title, items in | 30 | EmacsImportSections(result: result, groups: result.groups(folders: false), selected: $selected, |
| 31 | Section(title) { | 31 | font: .system(.callout, design: .monospaced)) |
| 32 | ForEach(items) { item in | ||
| 33 | Toggle(isOn: Binding { selected.contains(item.id) } set: { on in | ||
| 34 | if on { selected.insert(item.id) } else { selected.remove(item.id) } | ||
| 35 | }) { | ||
| 36 | VStack(alignment: .leading, spacing: 1) { | ||
| 37 | Text(item.summary).font(.system(.callout, design: .monospaced)) | ||
| 38 | Text([item.name == item.summary ? nil : item.name, item.source.isEmpty ? nil : item.source].compactMap { $0 }.joined(separator: " · ")) | ||
| 39 | .font(.caption).foregroundStyle(.secondary) | ||
| 40 | } | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | let notes = result.unsupported.map { ($0.id, $0.name, $0.reason, $0.source) } + result.unreadable.map { ($0.id, $0.name, $0.reason, $0.source) } | ||
| 46 | if !notes.isEmpty || !result.problems.isEmpty { | ||
| 47 | Section("Not imported") { | ||
| 48 | ForEach(result.problems, id: \.self) { Text($0).foregroundStyle(.red) } | ||
| 49 | ForEach(notes, id: \.0) { note in | ||
| 50 | VStack(alignment: .leading, spacing: 1) { | ||
| 51 | Text(note.1).font(.system(.callout, design: .monospaced)) | ||
| 52 | Text("\(note.2) · \(note.3)").font(.caption).foregroundStyle(.secondary) | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } | 32 | } |
| 58 | } | 33 | } |
| 59 | .navigationTitle("Import from Emacs") | 34 | .navigationTitle("Import from Emacs") |
| @@ -67,23 +42,13 @@ struct EmacsImportScreen: View { | |||
| 67 | } | 42 | } |
| 68 | } | 43 | } |
| 69 | 44 | ||
| 70 | private func groups(_ result: EmacsImport) -> [(String, [EmacsImport.Item])] { | ||
| 71 | [ | ||
| 72 | ("Settings", result.items.filter { if case .setting = $0.change { true } else { false } }), | ||
| 73 | ("Capture templates", result.items.filter { if case .capture = $0.change { true } else { false } }), | ||
| 74 | ("Key bindings", result.items.filter { if case .binding = $0.change { true } else { false } }), | ||
| 75 | ("Structure templates", result.items.filter { if case .structureTemplate = $0.change { true } else { false } }), | ||
| 76 | ("Agenda views", result.items.filter { if case .view = $0.change { true } else { false } }), | ||
| 77 | ].filter { !$0.1.isEmpty } | ||
| 78 | } | ||
| 79 | |||
| 80 | private func read(_ url: URL) { | 45 | private func read(_ url: URL) { |
| 81 | let accessing = url.startAccessingSecurityScopedResource() | 46 | let accessing = url.startAccessingSecurityScopedResource() |
| 82 | defer { if accessing { url.stopAccessingSecurityScopedResource() } } | 47 | defer { if accessing { url.stopAccessingSecurityScopedResource() } } |
| 83 | location = url | 48 | location = url |
| 84 | let scanned = EmacsImport.scan(EmacsImport.files(in: url)) | 49 | let scanned = EmacsImport.scan(EmacsImport.files(in: url)) |
| 85 | result = scanned | 50 | result = scanned |
| 86 | selected = Set(groups(scanned).flatMap(\.1).map(\.id)) | 51 | selected = Set(scanned.groups(folders: false).flatMap(\.1).map(\.id)) |
| 87 | summary = nil | 52 | summary = nil |
| 88 | } | 53 | } |
| 89 | 54 | ||
Sources/OrgstarMobile/VersionScreens.swift −70
| @@ -2,76 +2,6 @@ | |||
| 2 | import OrgApp | 2 | import OrgApp |
| 3 | import OrgDocument | 3 | import OrgDocument |
| 4 | import SwiftUI | 4 | import SwiftUI |
| 5 | import UIKit | ||
| 6 | |||
| 7 | /// Lines removed and added between two versions, with a few lines of context around each change. | ||
| 8 | struct DiffView: View { | ||
| 9 | let lines: [DiffLine] | ||
| 10 | var context = 3 | ||
| 11 | |||
| 12 | init(old: String, new: String) { | ||
| 13 | lines = lineDiff(old: old, new: new) | ||
| 14 | } | ||
| 15 | |||
| 16 | /// Lines to show, nil standing for a run of unchanged lines left out. | ||
| 17 | private var shown: [DiffLine?] { | ||
| 18 | let changed = lines.indices.filter { lines[$0].kind != .same } | ||
| 19 | guard !changed.isEmpty else { return [] } | ||
| 20 | var keep = Set<Int>() | ||
| 21 | for i in changed { keep.formUnion(max(0, i - context)...min(lines.count - 1, i + context)) } | ||
| 22 | var out: [DiffLine?] = [] | ||
| 23 | var skipped = false | ||
| 24 | for i in lines.indices { | ||
| 25 | if keep.contains(i) { | ||
| 26 | out.append(lines[i]) | ||
| 27 | skipped = false | ||
| 28 | } else if !skipped { | ||
| 29 | out.append(nil) | ||
| 30 | skipped = true | ||
| 31 | } | ||
| 32 | } | ||
| 33 | return out | ||
| 34 | } | ||
| 35 | |||
| 36 | var body: some View { | ||
| 37 | let shown = shown | ||
| 38 | ScrollView([.vertical, .horizontal]) { | ||
| 39 | VStack(alignment: .leading, spacing: 0) { | ||
| 40 | if shown.isEmpty { Text("No differences").foregroundStyle(.secondary).padding(8) } | ||
| 41 | ForEach(shown.indices, id: \.self) { i in | ||
| 42 | if let line = shown[i] { | ||
| 43 | Text(prefix(line.kind) + line.text.trimmingCharacters(in: .newlines)) | ||
| 44 | .frame(maxWidth: .infinity, alignment: .leading) | ||
| 45 | .background(background(line.kind)) | ||
| 46 | } else { | ||
| 47 | Text("⋯").foregroundStyle(.secondary) | ||
| 48 | } | ||
| 49 | } | ||
| 50 | } | ||
| 51 | .font(.system(.footnote, design: .monospaced)) | ||
| 52 | .textSelection(.enabled) | ||
| 53 | .padding(8) | ||
| 54 | } | ||
| 55 | .defaultScrollAnchor(.topLeading) | ||
| 56 | .background(Color(uiColor: .secondarySystemBackground)) | ||
| 57 | } | ||
| 58 | |||
| 59 | private func prefix(_ kind: DiffLine.Kind) -> String { | ||
| 60 | switch kind { | ||
| 61 | case .same: " " | ||
| 62 | case .removed: "- " | ||
| 63 | case .added: "+ " | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | private func background(_ kind: DiffLine.Kind) -> Color { | ||
| 68 | switch kind { | ||
| 69 | case .same: .clear | ||
| 70 | case .removed: .red.opacity(0.18) | ||
| 71 | case .added: .green.opacity(0.18) | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | 5 | ||
| 76 | /// The buffer and the file on disk changed differently: what differs, and how to settle it. | 6 | /// The buffer and the file on disk changed differently: what differs, and how to settle it. |
| 77 | struct ConflictSheet: View { | 7 | struct ConflictSheet: View { |