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.

An app's page keeps what was chosen when it scans again

Țugui Dragoșc633e5d04472
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/KeptSelection.swift b/Packages/PeelCore/Sources/PeelCore/Removal/KeptSelection.swift
new file mode 100644
index 00000000..ce2500f8
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/KeptSelection.swift
@@ -0,0 +1,31 @@
+public import Foundation
+
+/// What stays selected on a page when what it lists changes: a scan finds other items, or the helper comes or goes.
+///
+/// An item the person could already choose keeps its checkbox as they left it, so nothing they deselected comes
+/// back. Peel's suggestion applies only to an item they could not choose until now: one new to the page, or one
+/// that could not be selected before. An item leaves the selection when it is gone, when it can no longer be
+/// selected, or when Peel had suggested it and no longer does.
+public struct KeptSelection: Sendable {
+ /// Each item the person could choose after the last update, and whether Peel suggested it.
+ private var offered: [URL: Bool] = [:]
+ /// The selection the last update made, so a page can tell whether the person has changed it since.
+ public private(set) var made: Set<URL> = []
+
+ public init() {}
+
+ /// The selection that follows `selected` now that the page offers `selectable` and suggests `suggested`.
+ public mutating func update(_ selected: Set<URL>, selectable: Set<URL>, suggested: Set<URL>) -> Set<URL> {
+ made = selectable.filter { url in
+ guard let wasSuggested = offered[url] else { return suggested.contains(url) }
+ return selected.contains(url) && (suggested.contains(url) || !wasSuggested)
+ }
+ offered = Dictionary(uniqueKeysWithValues: selectable.map { ($0, suggested.contains($0)) })
+ return made
+ }
+
+ /// Forgets every choice, so the next update is Peel's suggestion alone.
+ public mutating func startOver() {
+ offered = [:]
+ }
+}
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/UninstallSelection.swift b/Packages/PeelCore/Sources/PeelCore/Removal/UninstallSelection.swift
new file mode 100644
index 00000000..81986264
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/UninstallSelection.swift
@@ -0,0 +1,30 @@
+public import Foundation
+
+/// What is selected on an app's page, kept across its scans (`KeptSelection`) and as the helper comes and goes.
+///
+/// When the app comes to stay, everything selected was chosen for it going, so nothing stays selected: its leftovers
+/// would go and it would stay without them. When it can go again, Peel's suggestion comes back only if the person
+/// left the selection as Peel made it.
+public struct UninstallSelection: Sendable {
+ private var kept = KeptSelection()
+ private var appStays: Bool?
+ /// What a checkbox can select as of the last update.
+ public private(set) var selectable: Set<URL> = []
+
+ public init() {}
+
+ public mutating func update(_ selected: Set<URL>, in uninstallation: Uninstallation, canUseHelper: Bool) -> Set<URL> {
+ let stays = uninstallation.appStays(canUseHelper: canUseHelper)
+ var suggested = uninstallation.suggestedSelection(canUseHelper: canUseHelper)
+ if let stayed = appStays, stayed != stays {
+ if stays || selected == kept.made {
+ kept.startOver()
+ } else {
+ suggested = []
+ }
+ }
+ appStays = stays
+ selectable = uninstallation.selectable(canUseHelper: canUseHelper)
+ return kept.update(selected, selectable: selectable, suggested: suggested)
+ }
+}
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift b/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift
index 184054b5..b15e3e32 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift
@@ -165,6 +165,20 @@ public struct Uninstallation: Sendable {
return selection
}
+ /// What a checkbox on the app's page can select: nothing Peel leaves alone or with something excluded inside,
+ /// nothing that needs the helper while it cannot act, and nothing of an excluded app or of Peel.
+ public func selectable(canUseHelper: Bool) -> Set<URL> {
+ guard !isExcluded, !isPeel else { return [] }
+ var urls = Set(scan.leftovers.filter { leftover in
+ leftover.match.heldBack != .holdsAnExclusion && leftover.match.heldBack?.cannotBeMoved != true
+ && (canUseHelper || !leftover.requiresPrivileges)
+ }.map(\.url))
+ if !app.isSystemProtected, !isAppBeyondTheHelper, canUseHelper || !appRequiresPrivileges {
+ urls.insert(app.url)
+ }
+ return urls
+ }
+
/// What moves when nobody reviews the list, as when Peel removes itself: the app, plus the recommended
/// leftovers that need no helper and are certainly its own or named inside its bundle identifier (a
/// namespace only its maker uses). Empty when the app is excluded, kept by macOS, or needs the helper: then
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/KeptSelectionTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/KeptSelectionTests.swift
new file mode 100644
index 00000000..02161392
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/KeptSelectionTests.swift
@@ -0,0 +1,74 @@
+import Foundation
+@testable import PeelCore
+import Testing
+
+struct KeptSelectionTests {
+ private func url(_ name: String) -> URL {
+ URL(filePath: "/Users/me/Library/Caches/\(name)")
+ }
+
+ /// A page starts from Peel's suggestion. Scanned again with the same items, it keeps what the person
+ /// deselected deselected, and what they selected selected.
+ @Test func keepsWhatThePersonChoseWhenThePageIsScannedAgain() {
+ var kept = KeptSelection()
+ let items: Set = [url("a"), url("b"), url("c")]
+ var selected = kept.update([], selectable: items, suggested: [url("a"), url("b")])
+ #expect(selected == [url("a"), url("b")])
+
+ selected.remove(url("a"))
+ selected.insert(url("c"))
+ #expect(kept.update(selected, selectable: items, suggested: [url("a"), url("b")]) == [url("b"), url("c")])
+ }
+
+ /// What the person could already see keeps its checkbox, even when Peel would suggest it now. The suggestion
+ /// goes to an item new to the page.
+ @Test func suggestsOnlyWhatIsNewToThePage() {
+ var kept = KeptSelection()
+ let selected = kept.update([], selectable: [url("seen")], suggested: [])
+
+ #expect(kept.update(selected, selectable: [url("seen"), url("new")], suggested: [url("seen"), url("new")]) == [url("new")])
+ }
+
+ /// An item that could not be selected, such as one that needs the helper while it is not there, was never the
+ /// person's to choose. Once it can be selected, it takes Peel's suggestion.
+ @Test func anItemThatCouldNotBeSelectedTakesTheSuggestionOnceItCan() {
+ var kept = KeptSelection()
+ let selected = kept.update([], selectable: [url("free")], suggested: [url("free"), url("locked")])
+ #expect(selected == [url("free")])
+
+ #expect(kept.update(selected, selectable: [url("free"), url("locked")], suggested: [url("free"), url("locked")]) == [url("free"), url("locked")])
+ }
+
+ @Test func dropsWhatIsGoneOrCanNoLongerBeSelected() {
+ var kept = KeptSelection()
+ let selected = kept.update([], selectable: [url("a"), url("b")], suggested: [url("a"), url("b")])
+
+ #expect(kept.update(selected, selectable: [url("a")], suggested: [url("a")]) == [url("a")])
+ #expect(kept.update([url("a")], selectable: [], suggested: []).isEmpty)
+ }
+
+ /// An item Peel selected for the person leaves the selection once Peel no longer suggests it, since what it
+ /// was selected for has changed. An item the person picked on their own stays.
+ @Test func dropsWhatPeelSelectedOnceItNoLongerSuggestsIt() {
+ var kept = KeptSelection()
+ var selected = kept.update([], selectable: [url("suggested"), url("picked")], suggested: [url("suggested")])
+ selected.insert(url("picked"))
+
+ #expect(kept.update(selected, selectable: [url("suggested"), url("picked")], suggested: []) == [url("picked")])
+ }
+
+ @Test func startingOverGivesPeelsSuggestionAgain() {
+ var kept = KeptSelection()
+ _ = kept.update([], selectable: [url("a"), url("b")], suggested: [url("a")])
+
+ kept.startOver()
+ #expect(kept.update([], selectable: [url("a"), url("b")], suggested: [url("a")]) == [url("a")])
+ }
+
+ @Test func remembersTheSelectionItMade() {
+ var kept = KeptSelection()
+ let selected = kept.update([], selectable: [url("a"), url("b")], suggested: [url("a")])
+
+ #expect(kept.made == selected)
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/UninstallSelectionTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/UninstallSelectionTests.swift
new file mode 100644
index 00000000..813845c1
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/UninstallSelectionTests.swift
@@ -0,0 +1,90 @@
+import Foundation
+@testable import PeelCore
+import Testing
+
+struct UninstallSelectionTests {
+ private let app = InstalledApp(url: URL(filePath: "/Applications/Example.app"), bundleIdentifier: "com.example.app", name: "Example")
+
+ private func leftover(_ name: String, confidence: MatchConfidence = .certain, requiresPrivileges: Bool = false) -> Leftover {
+ Leftover(
+ url: URL(filePath: "/Users/me/Library/Caches/\(name)"),
+ kind: .caches,
+ match: LeftoverMatch(reason: .bundleIdentifier, confidence: confidence, sharedWith: []),
+ size: 1_000,
+ isMeasured: true,
+ requiresPrivileges: requiresPrivileges
+ )
+ }
+
+ private func uninstallation(appRequiresPrivileges: Bool = false, _ leftovers: [Leftover]) -> Uninstallation {
+ Uninstallation(
+ app: app,
+ appSize: 10_000,
+ appRequiresPrivileges: appRequiresPrivileges,
+ scan: LeftoverScan(leftovers: leftovers, unreadableLocations: [])
+ )
+ }
+
+ /// An app's page scans again when Homebrew's evidence arrives, when the exclusions change, and after a move.
+ /// What the person deselected stays deselected through every one of those scans.
+ @Test func anAppsPageKeepsWhatThePersonDeselectedWhenItScansAgain() {
+ let first = leftover("com.example.app")
+ let second = leftover("com.example.app.helper")
+ let plan = uninstallation([first, second])
+ var choices = UninstallSelection()
+
+ var selected = choices.update([], in: plan, canUseHelper: true)
+ #expect(selected == [app.url, first.url, second.url])
+ selected.remove(first.url)
+
+ #expect(choices.update(selected, in: plan, canUseHelper: true) == [app.url, second.url])
+ }
+
+ /// What needs the helper leaves the selection when the helper goes, and takes Peel's suggestion again once
+ /// it is back, since the person could not choose it meanwhile.
+ @Test func whatNeedsTheHelperFollowsTheHelper() {
+ let own = leftover("com.example.app")
+ let daemon = leftover("com.example.app.daemon.plist", requiresPrivileges: true)
+ let plan = uninstallation([own, daemon])
+ var choices = UninstallSelection()
+
+ let selected = choices.update([], in: plan, canUseHelper: true)
+ #expect(selected == [app.url, own.url, daemon.url])
+ let withoutHelper = choices.update(selected, in: plan, canUseHelper: false)
+ #expect(withoutHelper == [app.url, own.url])
+ #expect(!choices.selectable.contains(daemon.url))
+ #expect(choices.update(withoutHelper, in: plan, canUseHelper: true) == [app.url, own.url, daemon.url])
+ }
+
+ /// An app that needs the helper stays when the helper goes. Everything selected was chosen for it going, the
+ /// person's own picks included, so nothing stays selected: its leftovers would go and it would stay without them.
+ @Test func anAppThatComesToStayHasNothingSelected() {
+ let own = leftover("com.example.app")
+ let possible = leftover("Example", confidence: .possible)
+ let plan = uninstallation(appRequiresPrivileges: true, [own, possible])
+ var choices = UninstallSelection()
+
+ var selected = choices.update([], in: plan, canUseHelper: true)
+ #expect(selected == [app.url, own.url])
+ selected.insert(possible.url)
+
+ #expect(choices.update(selected, in: plan, canUseHelper: false).isEmpty)
+ }
+
+ /// When the helper comes back, the app can go again. Peel's suggestion comes back only when the person left
+ /// the selection as Peel made it; what they chose meanwhile is theirs, and the app is not selected for them.
+ @Test func anAppThatCanGoAgainGetsTheSuggestionOnlyIfNothingWasChanged() {
+ let own = leftover("com.example.app")
+ let possible = leftover("Example", confidence: .possible)
+ let plan = uninstallation(appRequiresPrivileges: true, [own, possible])
+
+ var untouched = UninstallSelection()
+ let nothing = untouched.update([], in: plan, canUseHelper: false)
+ #expect(nothing.isEmpty)
+ #expect(untouched.update(nothing, in: plan, canUseHelper: true) == [app.url, own.url])
+
+ var changed = UninstallSelection()
+ _ = changed.update([], in: plan, canUseHelper: false)
+ #expect(changed.update([possible.url], in: plan, canUseHelper: true) == [possible.url])
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift
index ad23ba30..16dd8f30 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift
@@ -64,6 +64,42 @@ struct UninstallationTests {
#expect(uninstallation(appRequiresPrivileges: true, leftovers: []).privilegedURLs == [app.url])
}
+ /// A checkbox can select what a row lets the person choose: nothing Peel leaves alone or with something
+ /// excluded inside, nothing that needs the helper while it cannot act, and nothing of an app the person excluded
+ /// or of Peel. Everything Peel suggests is among it.
+ @Test func selectsOnlyWhatARowLetsThePersonChoose() {
+ let plain = leftover("plain")
+ let possible = leftover("possible", confidence: .possible)
+ let keys = leftover("keys").heldBack(.holdsKeys)
+ let excluded = leftover("excluded").heldBack(.holdsAnExclusion)
+ let unmeasured = leftover("unmeasured").heldBack(.notMeasured)
+ let privileged = leftover("privileged", requiresPrivileges: true)
+ let plan = uninstallation(leftovers: [plain, possible, keys, excluded, unmeasured, privileged])
+
+ #expect(plan.selectable(canUseHelper: false) == [app.url, plain.url, possible.url, unmeasured.url])
+ #expect(plan.selectable(canUseHelper: true) == [app.url, plain.url, possible.url, unmeasured.url, privileged.url])
+ for canUseHelper in [false, true] {
+ #expect(plan.suggestedSelection(canUseHelper: canUseHelper).isSubset(of: plan.selectable(canUseHelper: canUseHelper)))
+ }
+
+ let needsHelper = uninstallation(appRequiresPrivileges: true, leftovers: [plain])
+ #expect(needsHelper.selectable(canUseHelper: false) == [plain.url])
+ #expect(needsHelper.selectable(canUseHelper: true) == [app.url, plain.url])
+ var beyond = needsHelper
+ beyond.isAppBeyondTheHelper = true
+ #expect(beyond.selectable(canUseHelper: true) == [plain.url])
+
+ let protectedApp = InstalledApp(url: URL(filePath: "/System/Applications/Chess.app"), bundleIdentifier: "com.apple.Chess", name: "Chess", isSystemProtected: true)
+ #expect(uninstallation(app: protectedApp, leftovers: [plain]).selectable(canUseHelper: true) == [plain.url])
+
+ var excludedApp = uninstallation(leftovers: [plain])
+ excludedApp.isExcluded = true
+ #expect(excludedApp.selectable(canUseHelper: true).isEmpty)
+
+ let peel = InstalledApp(url: URL(filePath: "/Applications/Peel.app"), bundleIdentifier: "com.tuguidragos.Peel", name: "Peel")
+ #expect(uninstallation(app: peel, leftovers: [plain]).selectable(canUseHelper: true).isEmpty)
+ }
+
/// An app that would stay gets nothing selected, as an app macOS keeps does: its leftovers would go first
/// and leave it without its settings. It stays when the helper may not move it, or when it needs the helper
/// and the helper is not there. In a batch, what such an app shares with another chosen app stays too.
diff --git a/Peel/Model/RemovalPlan.swift b/Peel/Model/RemovalPlan.swift
index 2416b3ba..f324e1fc 100644
--- a/Peel/Model/RemovalPlan.swift
+++ b/Peel/Model/RemovalPlan.swift
@@ -21,6 +21,9 @@ final class RemovalPlan {
private(set) var vendorUninstaller: URL?
private(set) var systemExtensions: [String] = []
var selectedURLs: Set<URL> = []
+ private var choices = UninstallSelection()
+ /// Whether the helper can act, as last heard, so a scan that lands after it changed selects by what is true now.
+ private var canUseHelper = false
/// Worked out once per scan, because the page reads each of these several times on every change.
private(set) var recommended: [Leftover] = []
private(set) var needsReview: [Leftover] = []
@@ -82,7 +85,11 @@ final class RemovalPlan {
uninstallation?.privilegedURLs.isEmpty == false
}
+ /// What a checkbox can select now.
+ var selectable: Set<URL> { choices.selectable }
+
func refresh(installedApps: [InstalledApp], canUseHelper: Bool, casks: [HomebrewPackage] = [], receipts: Set<String> = []) async {
+ self.canUseHelper = canUseHelper
let app = app
guard let (result, bundle) = await scanRun.run({
let result = await Uninstallation.prepare(
@@ -104,10 +111,17 @@ final class RemovalPlan {
leftoverSizes = Dictionary(leftovers.map { ($0.url, ($0.size, $0.isMeasured)) }, uniquingKeysWith: { first, _ in first })
self.installedApps = installedApps
(vendorUninstaller, systemExtensions) = bundle
- selectedURLs = result.suggestedSelection(canUseHelper: canUseHelper)
+ selectedURLs = choices.update(selectedURLs, in: result, canUseHelper: self.canUseHelper)
defaultRoles = await DefaultApps.roles(of: app)
}
+ /// Brings the selection in line with the helper as it is now, without scanning again.
+ func follow(canUseHelper: Bool) {
+ self.canUseHelper = canUseHelper
+ guard let uninstallation else { return }
+ selectedURLs = choices.update(selectedURLs, in: uninstallation, canUseHelper: canUseHelper)
+ }
+
@concurrent
private nonisolated static func look(inside app: InstalledApp) async -> (uninstaller: URL?, systemExtensions: [String]) {
(VendorRemoval.uninstaller(for: app), VendorRemoval.systemExtensions(in: app))
diff --git a/Peel/Views/AppDetailView.swift b/Peel/Views/AppDetailView.swift
index 93f586c6..b86d636b 100644
--- a/Peel/Views/AppDetailView.swift
+++ b/Peel/Views/AppDetailView.swift
@@ -74,6 +74,9 @@ struct AppDetailView: View {
await rescan()
}
.rescanOnExclusionChange("AppDetailView") { await rescan() }
+ .onChange(of: helper.canAct) { _, canAct in
+ plan.follow(canUseHelper: canAct)
+ }
}
private var phase: ScanPhase {
@@ -261,9 +264,7 @@ struct AppDetailView: View {
/// The Recommended rows a checkbox can select, for its Select All. Review Before Removing has no Select All,
/// because nothing there should be selected without being read.
private var recommendedSelectable: [URL] {
- let isAppSelectable = !(plan.appRequiresPrivileges && !helper.canAct) && !plan.isExcluded && !plan.isPeel
- && !plan.app.isSystemProtected && !plan.isAppBeyondTheHelper
- return (isAppSelectable ? [plan.app.url] : []) + plan.recommended.filter(canSelect).map(\.url)
+ ([plan.app.url] + plan.recommended.map(\.url)).filter(plan.selectable.contains)
}
@ViewBuilder
@@ -525,11 +526,6 @@ struct AppDetailView: View {
)
}
- private func canSelect(_ leftover: Leftover) -> Bool {
- !(leftover.requiresPrivileges && !helper.canAct) && leftover.match.heldBack != .holdsAnExclusion
- && leftover.match.heldBack?.cannotBeMoved != true && !plan.isPeel
- }
-
/// What the row has to say for itself beyond the match: who else uses it, and why Peel left the checkmark off.
private func warning(for leftover: Leftover) -> String? {
var lines: [String] = []