Clock report reads open buffers !237

merged merged by cmc on 2026-10-11 06:20 UTC · krz/orgstar:clock-report-buffers into main

2 files changed, +12 −3

Layout: unified · split

Sources/Orgstar/ClockViews.swift +11 −2
@@ -130,6 +130,8 @@ final class ClockStatusItem: NSObject {
130/// Clocked time per day and heading (`ClockReport.timeReport`) for chosen files and dates. 130/// Clocked time per day and heading (`ClockReport.timeReport`) for chosen files and dates.
131struct ClockReportView: View { 131struct ClockReportView: View {
132 let workspace: WorkspaceModel 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 @State private var start = Calendar.current.date(byAdding: .day, value: -6, to: Date()) ?? Date() 135 @State private var start = Calendar.current.date(byAdding: .day, value: -6, to: Date()) ?? Date()
134 @State private var end = Date() 136 @State private var end = Date()
135 @State private var limit = false 137 @State private var limit = false
@@ -172,11 +174,13 @@ struct ClockReportView: View {
172 .navigationTitle("Clock Report") 174 .navigationTitle("Clock Report")
173 .onAppear { 175 .onAppear {
174 if selected.isEmpty { 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 compute() 179 compute()
178 } 180 }
179 .onChange(of: selected) { compute() } 181 .onChange(of: selected) { compute() }
182 .onChange(of: session.editVersion) { compute() }
183 .onChange(of: session.dirtyPaths) { compute() }
180 .onChange(of: start) { compute() } 184 .onChange(of: start) { compute() }
181 .onChange(of: end) { compute() } 185 .onChange(of: end) { compute() }
182 .onChange(of: limit) { compute() } 186 .onChange(of: limit) { compute() }
@@ -187,10 +191,15 @@ struct ClockReportView: View {
187 return String(format: "%04d-%02d-%02d", c.year!, c.month!, c.day!) 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 private func compute() { 199 private func compute() {
191 var entries: [ClockReport.Entry] = [] 200 var entries: [ClockReport.Entry] = []
192 for path in selected.sorted() { 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 entries += ClockReport.entries(text, settings: workspace.settings.org) 203 entries += ClockReport.entries(text, settings: workspace.settings.org)
195 } 204 }
196 report = selected.isEmpty ? "" : ClockReport.timeReport(entries, start: limit ? day(start) : nil, end: limit ? day(end) : nil) 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 .defaultSize(width: 1100, height: 700) 354 .defaultSize(width: 1100, height: 700)
355 Window("Clock Report", id: "clock-report") { 355 Window("Clock Report", id: "clock-report") {
356 if case .success(let model) = workspace { 356 if case .success(let model) = workspace {
357 ClockReportView(workspace: model) 357 ClockReportView(workspace: model, session: session)
358 } 358 }
359 } 359 }
360 .defaultSize(width: 860, height: 560) 360 .defaultSize(width: 860, height: 560)