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 {
130130/// Clocked time per day and heading (`ClockReport.timeReport`) for chosen files and dates.
131131struct ClockReportView: View {
132132 let workspace: WorkspaceModel
133 /// Open buffers are read as they are, unsaved edits included, as the agenda reads them.
134 let session: DocumentSession
133135 @State private var start = Calendar.current.date(byAdding: .day, value: -6, to: Date()) ?? Date()
134136 @State private var end = Date()
135137 @State private var limit = false
@@ -172,11 +174,13 @@ struct ClockReportView: View {
172174 .navigationTitle("Clock Report")
173175 .onAppear {
174176 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))
176178 }
177179 compute()
178180 }
179181 .onChange(of: selected) { compute() }
182 .onChange(of: session.editVersion) { compute() }
183 .onChange(of: session.dirtyPaths) { compute() }
180184 .onChange(of: start) { compute() }
181185 .onChange(of: end) { compute() }
182186 .onChange(of: limit) { compute() }
@@ -187,10 +191,15 @@ struct ClockReportView: View {
187191 return String(format: "%04d-%02d-%02d", c.year!, c.month!, c.day!)
188192 }
189193
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
190199 private func compute() {
191200 var entries: [ClockReport.Entry] = []
192201 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 }
194203 entries += ClockReport.entries(text, settings: workspace.settings.org)
195204 }
196205 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 {
354354 .defaultSize(width: 1100, height: 700)
355355 Window("Clock Report", id: "clock-report") {
356356 if case .success(let model) = workspace {
357 ClockReportView(workspace: model)
357 ClockReportView(workspace: model, session: session)
358358 }
359359 }
360360 .defaultSize(width: 860, height: 560)