Clock report reads open buffers !237
2 files changed, +12 −3
Layout: unified · split
Sources/Orgstar/ClockViews.swift +11 −2
| @@ -130,6 +130,8 @@ final class ClockStatusItem: NSObject { | ||
| 130 | 130 | /// Clocked time per day and heading (`ClockReport.timeReport`) for chosen files and dates. |
| 131 | 131 | struct ClockReportView: View { |
| 132 | 132 | let workspace: WorkspaceModel |
| 133 | /// Open buffers are read as they are, unsaved edits included, as the agenda reads them. | |
| 134 | let session: DocumentSession | |
| 133 | 135 | @State private var start = Calendar.current.date(byAdding: .day, value: -6, to: Date()) ?? Date() |
| 134 | 136 | @State private var end = Date() |
| 135 | 137 | @State private var limit = false |
| @@ -172,11 +174,13 @@ struct ClockReportView: View { | ||
| 172 | 174 | .navigationTitle("Clock Report") |
| 173 | 175 | .onAppear { |
| 174 | 176 | if selected.isEmpty { |
| 175 | selected = Set(candidates.filter { (try? String(contentsOfFile: $0.path, encoding: .utf8))?.contains("CLOCK: [") ?? false }.map(\.path)) | |
| 177 | selected = Set(candidates.filter { text(of: $0.path)?.contains("CLOCK: [") ?? false }.map(\.path)) | |
| 176 | 178 | } |
| 177 | 179 | compute() |
| 178 | 180 | } |
| 179 | 181 | .onChange(of: selected) { compute() } |
| 182 | .onChange(of: session.editVersion) { compute() } | |
| 183 | .onChange(of: session.dirtyPaths) { compute() } | |
| 180 | 184 | .onChange(of: start) { compute() } |
| 181 | 185 | .onChange(of: end) { compute() } |
| 182 | 186 | .onChange(of: limit) { compute() } |
| @@ -187,10 +191,15 @@ struct ClockReportView: View { | ||
| 187 | 191 | return String(format: "%04d-%02d-%02d", c.year!, c.month!, c.day!) |
| 188 | 192 | } |
| 189 | 193 | |
| 194 | /// The file's text: its open buffer, else the file on disk. | |
| 195 | private func text(of path: String) -> String? { | |
| 196 | session.openTexts[path] ?? (try? String(contentsOfFile: path, encoding: .utf8)) | |
| 197 | } | |
| 198 | ||
| 190 | 199 | private func compute() { |
| 191 | 200 | var entries: [ClockReport.Entry] = [] |
| 192 | 201 | for path in selected.sorted() { |
| 193 | guard let text = try? String(contentsOfFile: path, encoding: .utf8) else { continue } | |
| 202 | guard let text = text(of: path) else { continue } | |
| 194 | 203 | entries += ClockReport.entries(text, settings: workspace.settings.org) |
| 195 | 204 | } |
| 196 | 205 | report = selected.isEmpty ? "" : ClockReport.timeReport(entries, start: limit ? day(start) : nil, end: limit ? day(end) : nil) |
Sources/Orgstar/OrgstarApp.swift +1 −1
| @@ -354,7 +354,7 @@ struct OrgstarApp: App { | ||
| 354 | 354 | .defaultSize(width: 1100, height: 700) |
| 355 | 355 | Window("Clock Report", id: "clock-report") { |
| 356 | 356 | if case .success(let model) = workspace { |
| 357 | ClockReportView(workspace: model) | |
| 357 | ClockReportView(workspace: model, session: session) | |
| 358 | 358 | } |
| 359 | 359 | } |
| 360 | 360 | .defaultSize(width: 860, height: 560) |