Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

peel history groups and dates removals as the app does

Țugui Dragoșac8623a9af56
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
index f1b96fae..93309b68 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
@@ -7,31 +7,30 @@ extension RemovalPart {
fileprivate var namedTool: String? { tool.isEmpty ? nil : tool }
}
-/// One removal, as History shows it: everything that went in one go.
+/// One removal, as History shows it: everything that went in one go, grouped and dated as the app does.
struct Batch {
- let id: UUID
- let records: [RemovalRecord]
+ let group: RemovalGroup
- var date: Date { records.map(\.date).max() ?? .distantPast }
+ var id: UUID { group.id }
+ var records: [RemovalRecord] { group.records }
+ var date: Date { group.date }
/// Where the removal moved from: one part, or one for each tool whose selection it moved.
- var parts: [RemovalPart] { RemovalPart.of(records.map { ($0.date, $0.part) }) }
+ var parts: [RemovalPart] { group.parts }
var source: String { Output.list(parts.map(\.source)) }
/// The tool the removal moved from, or nil for one that moved from several or was interrupted.
var tool: String? { parts.count == 1 ? parts[0].namedTool : nil }
- var size: SizeTotal { records.totalSize }
+ var size: SizeTotal { group.size }
/// The records whose items are still in the Trash. An item emptied from the Trash can't be put back.
var restorable: [RemovalRecord] { records.filter(\.isStillInTrash) }
static func all(in records: [RemovalRecord]) -> [Batch] {
- Dictionary(grouping: records, by: \.batch)
- .map { Batch(id: $0.key, records: $0.value.sorted { $0.date < $1.date }) }
- .sorted { $0.date > $1.date }
+ RemovalRecord.grouped(records).map(Batch.init)
}
/// The first eight characters of the batch ID, which `peel history` shows and `peel restore` accepts.
var shortID: String { String(id.uuidString.prefix(8)).lowercased() }
- func state(among records: [RemovalRecord]) -> String {
+ var state: String {
if records.allSatisfy(\.isStillInTrash) { return "in the Trash" }
if records.contains(where: { !$0.isOnAConnectedDisk }) { return "on a disk that isn't connected" }
if records.contains(where: { $0.standing == .notKnown }) { return "can't look in the Trash" }
@@ -198,7 +197,7 @@ struct HistoryCommand: AsyncParsableCommand {
batch.source,
Output.number(batch.records.count),
Output.size(batch.size),
- batch.state(among: batch.records),
+ batch.state,
]
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift
index 05b79e7a..5a1aa942 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift
@@ -846,6 +846,19 @@ struct CommandLineTests {
#expect(batches[0].shortID == String(recent.uuidString.prefix(8)).lowercased())
}
+ @Test func datesARemovalAsTheAppDoesByItsFirstItem() {
+ let removal = UUID()
+ let records = [
+ record(removal, "/Users/me/Library/Caches/one", at: Date(timeIntervalSince1970: 1_000)),
+ record(removal, "/Users/me/Library/Caches/two", at: Date(timeIntervalSince1970: 1_005)),
+ ]
+
+ let batch = Batch.all(in: records)[0]
+
+ #expect(batch.date == RemovalRecord.grouped(records)[0].date)
+ #expect(batch.date == Date(timeIntervalSince1970: 1_000))
+ }
+
/// The first characters are enough, and an ID that fits two removals is refused rather than guessed at.
@Test func findsARemovalByTheStartOfItsIdentifier() throws {
let first = try #require(UUID(uuidString: "A955E2E4-0000-4000-8000-000000000001"))
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
index cb9560fc..31066ccb 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
@@ -179,7 +179,7 @@ struct HistoryCommandTests {
let batch = try #require(Batch.all(in: [record]).first)
- #expect(batch.state(among: batch.records) == "can't look in the Trash")
+ #expect(batch.state == "can't look in the Trash")
}
/// With nothing recorded, the command prints a sentence rather than an empty table, and `--json` prints an