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.

Name a removal from many pages by its first four

Țugui Dragoș50a5f3e07c29
diff --git a/Localization/Peel/Localizable.xcstrings b/Localization/Peel/Localizable.xcstrings
index c0fd729b..29bc23ce 100644
--- a/Localization/Peel/Localizable.xcstrings
+++ b/Localization/Peel/Localizable.xcstrings
@@ -210334,6 +210334,346 @@
}
}
},
+ "^[%lld more page](inflect: true)" : {
+ "localizations" : {
+ "cs" : {
+ "variations" : {
+ "plural" : {
+ "few" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%lld další stránky"
+ }
+ },
+ "many" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%lld další stránky"
+ }
+ },
+ "one" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%lld další stránka"
+ }
+ },
+ "other" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%lld dalších stránek"
+ }
+ }
+ }
+ }
+ },
+ "de" : {
+ "variations" : {
+ "plural" : {
+ "one" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "noch %lld Seite"
+ }
+ },
+ "other" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "noch %lld Seiten"
+ }
+ }
+ }
+ }
+ },
+ "es" : {
+ "variations" : {
+ "plural" : {
+ "many" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%lld de páginas más"
+ }
+ },
+ "one" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%lld página más"
+ }
+ },
+ "other" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%lld páginas más"
+ }
+ }
+ }
+ }
+ },
+ "fr" : {
+ "variations" : {
+ "plural" : {
+ "many" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%lld d’autres pages"
+ }
+ },
+ "one" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%lld autre page"
+ }
+ },
+ "other" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%lld autres pages"
+ }
+ }
+ }
+ }
+ },
+ "it" : {
+ "variations" : {
+ "plural" : {
+ "many" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "altre %lld di pagine"
+ }
+ },
+ "one" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "un’altra pagina"
+ }
+ },
+ "other" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "altre %lld pagine"
+ }
+ }
+ }
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "その他%lldページ"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "그 외 페이지 %lld개"
+ }
+ },
+ "nl" : {
+ "variations" : {
+ "plural" : {
+ "one" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%lld andere pagina"
+ }
+ },
+ "other" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%lld andere pagina's"
+ }
+ }
+ }
+ }
+ },
+ "pl" : {
+ "variations" : {
+ "plural" : {
+ "few" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "jeszcze %lld strony"
+ }
+ },
+ "many" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "jeszcze %lld stron"
+ }
+ },
+ "one" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "jeszcze %lld strona"
+ }
+ },
+ "other" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "jeszcze %lld strony"
+ }
+ }
+ }
+ }
+ },
+ "pt" : {
+ "variations" : {
+ "plural" : {
+ "many" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "mais %lld de páginas"
+ }
+ },
+ "one" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "mais %lld página"
+ }
+ },
+ "other" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "mais %lld páginas"
+ }
+ }
+ }
+ }
+ },
+ "ro" : {
+ "variations" : {
+ "plural" : {
+ "few" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "încă %lld pagini"
+ }
+ },
+ "one" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "încă %lld pagină"
+ }
+ },
+ "other" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "încă %lld de pagini"
+ }
+ }
+ }
+ }
+ },
+ "ru" : {
+ "variations" : {
+ "plural" : {
+ "few" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "еще %lld страницы"
+ }
+ },
+ "many" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "еще %lld страниц"
+ }
+ },
+ "one" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "еще %lld страница"
+ }
+ },
+ "other" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "еще %lld страницы"
+ }
+ }
+ }
+ }
+ },
+ "sv" : {
+ "variations" : {
+ "plural" : {
+ "one" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%lld sida till"
+ }
+ },
+ "other" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%lld sidor till"
+ }
+ }
+ }
+ }
+ },
+ "tr" : {
+ "variations" : {
+ "plural" : {
+ "one" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%lld sayfa daha"
+ }
+ },
+ "other" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%lld sayfa daha"
+ }
+ }
+ }
+ }
+ },
+ "uk" : {
+ "variations" : {
+ "plural" : {
+ "few" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "ще %lld сторінки"
+ }
+ },
+ "many" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "ще %lld сторінок"
+ }
+ },
+ "one" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "ще %lld сторінка"
+ }
+ },
+ "other" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "ще %lld сторінки"
+ }
+ }
+ }
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "其他%lld个页面"
+ }
+ },
+ "zh-Hant" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "其他%lld個頁面"
+ }
+ }
+ }
+ },
"^[%lld package](inflect: true)." : {
"localizations" : {
"cs" : {
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
index 3477fda4..a1ebe19e 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
@@ -17,7 +17,12 @@ struct Batch {
var date: Date { group.date }
/// Where the removal moved from: one part, or one for each tool whose selection it moved.
var parts: [RemovalPart] { group.parts }
+ /// Every part's source, as `--json` writes it.
var source: String { Output.list(parts.map(\.source)) }
+ /// The removal's name in the table: every part's source, or the first few and how many more pages there are.
+ var name: String {
+ RemovalPart.name(parts.map(\.source), more: { Output.count($0, "more page", "more pages") }, list: Output.list)
+ }
/// 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 { group.size }
@@ -213,7 +218,7 @@ struct HistoryCommand: AsyncParsableCommand {
[
batch.shortID,
Output.day(batch.date),
- batch.source,
+ batch.name,
Output.number(batch.records.count),
Output.size(batch.size),
batch.state,
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGrouping.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGrouping.swift
index 87799082..bd17cd51 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGrouping.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGrouping.swift
@@ -89,6 +89,16 @@ public struct RemovalPart: Sendable, Hashable {
}
.map(\.key)
}
+
+ /// How many parts a removal's name lists before it only counts the rest.
+ public static let mostNamed = 4
+
+ /// A removal's name from its parts' titles, short enough for a window's title or a menu: every title, or the
+ /// first few and then `more`, which says how many pages the rest are, joined by `list`.
+ public static func name(_ titles: [String], more: (Int) -> String, list: ([String]) -> String) -> String {
+ guard titles.count > mostNamed else { return list(titles) }
+ return list(Array(titles.prefix(mostNamed)) + [more(titles.count - mostNamed)])
+ }
}
extension RemovalPart {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
index 8e2b4749..a5c1d255 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
@@ -147,6 +147,34 @@ struct HistoryCommandTests {
#expect(listedParts.compactMap { $0["tool"] as? String } == ["developer", "projects", "duplicates"])
}
+ @Test func namesTheFirstPagesOfALongRemovalAndCountsTheRest() async throws {
+ let directory = try TemporaryDirectory()
+ let logs = logs(in: directory)
+ let batch = UUID()
+ let now = Date.now
+ let records = (1...30).map { index in
+ RemovalRecord(
+ batch: batch,
+ item: TrashedItem(
+ originalURL: directory.url.appending(path: "home/project\(index)/node_modules"),
+ trashedURL: directory.url.appending(path: "home/.Trash/node_modules \(index)"),
+ date: now.addingTimeInterval(Double(index))
+ ),
+ size: 10,
+ source: "project\(index)",
+ tool: "projects"
+ )
+ }
+ _ = await logs.removals.add(records)
+
+ let named = HistoryCommand.rows(for: Batch.all(in: records))[1][2]
+ #expect(named == "project1, project2, project3, project4, and 26 more pages")
+
+ let removal = try #require(try listed(try await printed(["history", "--json"], from: logs)).first as? [String: Any])
+ #expect((removal["parts"] as? [[String: Any]])?.count == 30)
+ #expect((removal["source"] as? String)?.hasSuffix(", and project30") == true)
+ }
+
/// A removal cut short comes back into History with no tool, since which tool moved it is not known, and
/// `--json` says so with null rather than a name.
@Test func anInterruptedRemovalNamesNoTool() async throws {
diff --git a/Peel/Model/RemovalHistoryStore.swift b/Peel/Model/RemovalHistoryStore.swift
index 7ccc8260..98343db8 100644
--- a/Peel/Model/RemovalHistoryStore.swift
+++ b/Peel/Model/RemovalHistoryStore.swift
@@ -10,16 +10,20 @@ nonisolated struct RemovalBatch: Identifiable, Hashable {
let tools: [Tool]
let date: Date
let records: [RemovalRecord]
- /// The title the row shows: each part's source, in the order they moved. A source Peel stored with a key is
- /// shown in the user's language, any other source as stored.
+ /// The title the row shows: the parts' titles in the order they moved, or the first few and how many more
+ /// pages there are.
let title: String
+ /// Every part's title, which a search looks through. A source Peel stored with a key is shown in the user's
+ /// language, any other source as stored.
+ let partTitles: [String]
init(id: UUID, parts: [RemovalPart], date: Date, records: [RemovalRecord]) {
self.id = id
tools = Self.tools(of: parts)
self.date = date
self.records = records
- title = Self.title(of: parts)
+ partTitles = Self.titles(of: parts)
+ title = Self.title(naming: partTitles)
}
/// The one tool the batch moved from, or nil for a batch from several.
@@ -33,9 +37,14 @@ nonisolated struct RemovalBatch: Identifiable, Hashable {
return tools
}
- static func title(of parts: [RemovalPart]) -> String {
+ static func titles(of parts: [RemovalPart]) -> [String] {
parts.map { title(source: $0.source, key: $0.sourceKey, tool: Tool(rawValue: $0.tool)) }
- .formatted(.list(type: .and))
+ }
+
+ static func title(naming titles: [String]) -> String {
+ RemovalPart.name(titles, more: { String(inflecting: "^[\($0) more page](inflect: true)") }) {
+ $0.formatted(.list(type: .and))
+ }
}
static func title(source: String, key: String?, tool: Tool?) -> String {
@@ -100,14 +109,16 @@ nonisolated struct RefusalBatch: Identifiable, Hashable {
let id: UUID
let date: Date
let records: [RefusalRecord]
- /// The sources in the user's language, as `RemovalBatch.title(of:)` gives them.
+ /// The sources in the user's language, named as `RemovalBatch` names them.
let title: String
+ let partTitles: [String]
init(_ group: RefusalGroup) {
id = group.id
date = group.date
records = group.records
- title = RemovalBatch.title(of: group.parts)
+ partTitles = RemovalBatch.titles(of: group.parts)
+ title = RemovalBatch.title(naming: partTitles)
}
}
@@ -378,7 +389,7 @@ final class RemovalHistoryStore {
RemovalBatch(id: group.id, parts: group.parts, date: group.date, records: group.records)
}
let searchKeys = Dictionary(uniqueKeysWithValues: batches.map {
- ($0.id, searchKey(title: $0.title, names: $0.records.map(\.originalURL.lastPathComponent)))
+ ($0.id, searchKey(titles: $0.partTitles, names: $0.records.map(\.originalURL.lastPathComponent)))
})
return (batches, searchKeys)
}
@@ -390,7 +401,7 @@ final class RemovalHistoryStore {
) async -> (batches: [RefusalBatch], searchKeys: [RefusalBatch.ID: String]) {
let batches = RefusalRecord.grouped(records).map(RefusalBatch.init)
let searchKeys = Dictionary(uniqueKeysWithValues: batches.map {
- ($0.id, searchKey(title: $0.title, names: $0.records.map(\.url.lastPathComponent)))
+ ($0.id, searchKey(titles: $0.partTitles, names: $0.records.map(\.url.lastPathComponent)))
})
return (batches, searchKeys)
}
@@ -458,10 +469,10 @@ final class RemovalHistoryStore {
}
- /// A batch's title and the name of each item in it, joined into one string, so a search makes one call
+ /// A batch's part titles and the name of each item in it, joined into one string, so a search makes one call
/// per batch rather than one per record.
- nonisolated private static func searchKey(title: String, names: [String]) -> String {
- ([title] + names).joined(separator: "\n")
+ nonisolated private static func searchKey(titles: [String], names: [String]) -> String {
+ (titles + names).joined(separator: "\n")
}
}
diff --git a/PeelTests/HistoryTitleTests.swift b/PeelTests/HistoryTitleTests.swift
new file mode 100644
index 00000000..f44e9808
--- /dev/null
+++ b/PeelTests/HistoryTitleTests.swift
@@ -0,0 +1,44 @@
+import Foundation
+@testable import Peel
+import PeelCore
+import Testing
+
+@MainActor
+struct HistoryTitleTests {
+ @Test func namesTheFirstPagesOfALongRemovalAndFindsItByAnyOfThem() async throws {
+ let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
+ defer { try? FileManager.default.removeItem(at: folder) }
+ let history = RemovalHistoryStore(
+ log: RemovalLog(url: folder.appending(path: "removals.json")),
+ refusals: RefusalLog(url: folder.appending(path: "refusals.json")),
+ knowsTheExclusions: { true }
+ )
+ var removal = RemovalInProgress()
+ for index in 1...30 {
+ let name = String(format: "project%02d", index)
+ let json = #"{"originalURL": "file:///Users/me/\#(name)/node_modules", "trashedURL": "file:///Users/me/.Trash/node_modules \#(index)", "date": \#(index)}"#
+ let moved = try JSONDecoder().decode(TrashedItem.self, from: Data(json.utf8))
+ let refused = TrashFailure(url: URL(filePath: "/Users/me/\(name)/build"), reason: .lastCopy)
+ await history.record(
+ TrashResult(trashed: [moved], failures: [refused]),
+ part: RemovalPart(source: name, sourceKey: nil, tool: Tool.projects.rawValue),
+ sizes: [:],
+ in: &removal
+ )
+ }
+ history.finish(removal)
+
+ let named = "project01, project02, project03, project04, and 26 more pages"
+ let batch = try #require(history.batches.first)
+ #expect(batch.title == named)
+ #expect(history.undoTitle == "Undo Remove \(named)")
+ let found = await SearchText.matching("project30", among: history.batches, keys: history.searchKeys)
+ #expect(found?.map(\.id) == [batch.id])
+ let refused = try #require(history.refusalBatches.first)
+ #expect(refused.title == named)
+ let foundRefused = await SearchText.matching(
+ "project30", among: history.refusalBatches, keys: history.refusalSearchKeys
+ )
+ #expect(foundRefused?.map(\.id) == [refused.id])
+ }
+}