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.

Keep each page's selection rule with the items it judges

Țugui Dragoș41eeb12ba50a
diff --git a/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift b/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift
index 4d34f425..c7dade63 100644
--- a/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift
@@ -92,6 +92,14 @@ public struct DeveloperEnvironment: Sendable, Hashable, Identifiable {
SizeTotal(locations.map(\.size))
}
+ public var selectableRows: SelectableRows<URL> {
+ SelectableRows(
+ rows: locations.map(\.url),
+ selectable: locations.map(\.url),
+ recommended: locations.filter(\.isRecommended).map(\.url)
+ )
+ }
+
/// The name of one of the environment's apps that is running, which is the one to ask the user to quit, or nil
/// when none runs. Nothing of the environment moves while one runs, from the app or `peel`: it writes in these
/// folders. Asked each time, since an app can be opened at any moment.
diff --git a/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift b/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift
index 5aac03a5..769dc580 100644
--- a/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift
@@ -32,6 +32,12 @@ public struct ProjectArtifact: Sendable, Hashable, Identifiable {
}
}
+extension Collection where Element == ProjectArtifact {
+ public var selectableRows: SelectableRows<URL> {
+ SelectableRows(rows: map(\.url), selectable: map(\.url), recommended: filter(\.isRecommended).map(\.url))
+ }
+}
+
public enum ProjectArtifacts {
/// A project changed within this time is in use, so none of its artifacts are selected for the user.
public static let recentlyActive: TimeInterval = 7 * 24 * 60 * 60
diff --git a/Packages/PeelCore/Sources/PeelCore/Orphans/Orphan.swift b/Packages/PeelCore/Sources/PeelCore/Orphans/Orphan.swift
index 63c775ba..6712926a 100644
--- a/Packages/PeelCore/Sources/PeelCore/Orphans/Orphan.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Orphans/Orphan.swift
@@ -22,6 +22,10 @@ public struct OrphanItem: Sendable, Hashable, Identifiable {
}
public var id: URL { url }
+
+ public func isLocked(canUseHelper: Bool) -> Bool {
+ requiresPrivileges && leftAlone == nil && !canUseHelper
+ }
}
public struct OrphanGroup: Sendable, Hashable, Identifiable {
@@ -52,6 +56,17 @@ public struct OrphanGroup: Sendable, Hashable, Identifiable {
public var lastModified: Date? {
items.compactMap(\.modificationDate).max()
}
+
+ /// Peel recommends no row it held back: such a row waits to be chosen by hand, as Review Before Removing does on
+ /// an app's page.
+ public func selectableRows(canUseHelper: Bool) -> SelectableRows<URL> {
+ let unlocked = items.filter { !$0.isLocked(canUseHelper: canUseHelper) }
+ return SelectableRows(
+ rows: items.map(\.url),
+ selectable: unlocked.filter { $0.leftAlone == nil }.map(\.url),
+ recommended: unlocked.filter { $0.heldBack == nil }.map(\.url)
+ )
+ }
}
public struct OrphanScan: Sendable {
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/Installers.swift b/Packages/PeelCore/Sources/PeelCore/Space/Installers.swift
index 4bcb54fe..87f5851f 100644
--- a/Packages/PeelCore/Sources/PeelCore/Space/Installers.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Space/Installers.swift
@@ -36,6 +36,10 @@ public struct InstallerItem: Sendable, Hashable, Identifiable {
public var id: URL { url }
+ public func isLocked(canUseHelper: Bool) -> Bool {
+ requiresPrivileges && heldBack?.cannotBeMoved != true && !canUseHelper
+ }
+
/// A fact shown beside the item's name. The app puts it into words.
public enum Note: Sendable, Hashable {
/// A macOS installer's version, as its bundle gives it.
@@ -46,6 +50,17 @@ public struct InstallerItem: Sendable, Hashable, Identifiable {
}
}
+extension Collection where Element == InstallerItem {
+ public func selectableRows(canUseHelper: Bool) -> SelectableRows<URL> {
+ let open = filter { !$0.isReadOnly && !$0.isLocked(canUseHelper: canUseHelper) }
+ return SelectableRows(
+ rows: map(\.url),
+ selectable: open.filter { $0.heldBack?.cannotBeMoved != true }.map(\.url),
+ recommended: open.filter { $0.heldBack == nil }.map(\.url)
+ )
+ }
+}
+
public struct InstallerScan: Sendable {
public let items: [InstallerItem]
/// The folders Peel looks in that macOS would not let it read, the device backups' among them, so what is in
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift b/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift
index d117b497..8b688f62 100644
--- a/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift
@@ -35,6 +35,20 @@ public enum SpaceRemoval {
public var suggested: Set<URL> {
Set(sizes.keys).subtracting(heldBack.keys)
}
+
+ /// One the helper would refuse anyway is left alone rather than locked.
+ public func isLocked(_ url: URL, canUseHelper: Bool) -> Bool {
+ needsTheHelper.contains(url) && heldBack[url]?.cannotBeMoved != true && !canUseHelper
+ }
+
+ public func selectableRows(canUseHelper: Bool) -> SelectableRows<URL> {
+ let unlocked = removable.filter { !isLocked($0, canUseHelper: canUseHelper) }
+ return SelectableRows(
+ rows: removable,
+ selectable: unlocked.filter { heldBack[$0]?.cannotBeMoved != true },
+ recommended: unlocked.filter(suggested.contains)
+ )
+ }
}
/// The plan while the apps in `running` are open, whose names and groups are read here, away from the main actor.
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SelectableRowsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SelectableRowsTests.swift
index 835ac7cf..ca2034f5 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SelectableRowsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SelectableRowsTests.swift
@@ -65,4 +65,119 @@ struct SelectableRowsTests {
let locked = SelectableRows(rows: [repository, cache], selectable: [cache], recommended: [cache])
#expect(!locked.isNoneSelected(in: [repository]))
}
+
+ @Test func anOrphanGroupLeavesALockedRowToTheHelper() {
+ func orphan(_ name: String, heldBack: HoldBack? = nil, requiresPrivileges: Bool = false) -> OrphanItem {
+ var item = OrphanItem(
+ url: URL(filePath: "/tmp/\(name)"), kind: .caches, size: 10, modificationDate: nil,
+ requiresPrivileges: requiresPrivileges
+ )
+ item.heldBack = heldBack
+ return item
+ }
+ let plain = orphan("org.example.Plain")
+ let unmeasured = orphan("org.example.Unmeasured", heldBack: .notMeasured)
+ let library = orphan("org.example.Library", heldBack: .holdsALibrary)
+ let system = orphan("org.example.System", requiresPrivileges: true)
+ let refused = orphan("org.example.Refused", heldBack: .beyondTheHelper, requiresPrivileges: true)
+ let group = OrphanGroup(identifier: "org.example", items: [plain, unmeasured, library, system, refused])
+
+ let helped = group.selectableRows(canUseHelper: true)
+ #expect(helped.rows == group.items.map(\.url))
+ #expect(helped.selectable == [plain.url, unmeasured.url, system.url])
+ #expect(helped.recommended == [plain.url, system.url])
+ let alone = group.selectableRows(canUseHelper: false)
+ #expect(alone.rows == group.items.map(\.url))
+ #expect(alone.selectable == [plain.url, unmeasured.url])
+ #expect(alone.recommended == [plain.url])
+ #expect(system.isLocked(canUseHelper: false) && !system.isLocked(canUseHelper: true))
+ #expect(!refused.isLocked(canUseHelper: false))
+ }
+
+ @Test func aDeveloperToolRecommendsWhatItSelectsForThePerson() {
+ func location(
+ _ name: String, _ kind: DeveloperEnvironment.ContentKind, size: Int64? = 10
+ ) -> DeveloperEnvironment.Location {
+ DeveloperEnvironment.Location(
+ url: URL(filePath: "/tmp/\(name)"), kind: kind, size: size, source: "https://example.com"
+ )
+ }
+ let cache = location("org.example.Cache", .cache)
+ let unmeasured = location("org.example.Unmeasured", .cache, size: nil)
+ let archive = location("org.example.Archive", .archives)
+ let tool = DeveloperEnvironment(
+ id: "org.example", name: "Example", systemImage: "hammer", appBundleIdentifiers: [],
+ locations: [cache, unmeasured, archive]
+ )
+
+ #expect(tool.selectableRows.selectable == [cache.url, unmeasured.url, archive.url])
+ #expect(tool.selectableRows.recommended == [cache.url])
+ }
+
+ @Test func aProjectRecommendsWhatItSelectsForThePerson() {
+ let project = URL(filePath: "/tmp/org.example.Project")
+ func artifact(_ name: String, generic: Bool) -> ProjectArtifact {
+ ProjectArtifact(
+ url: project.appending(path: name), project: project, name: name, tool: "Xcode", size: 10,
+ lastActivity: .now.addingTimeInterval(-ProjectArtifacts.recentlyActive * 2),
+ hasGenericName: generic, isEnvironment: false
+ )
+ }
+ let derived = artifact("DerivedData", generic: false)
+ let build = artifact("build", generic: true)
+
+ #expect([derived, build].selectableRows.selectable == [derived.url, build.url])
+ #expect([derived, build].selectableRows.recommended == [derived.url])
+ }
+
+ @Test func anInstallerKindNeverSelectsABackupOrALockedRow() {
+ func installer(
+ _ name: String, heldBack: HoldBack? = nil, privileged: Bool = false, readOnly: Bool = false
+ ) -> InstallerItem {
+ InstallerItem(
+ url: URL(filePath: "/tmp/\(name)"), kind: .appInstaller, name: name, size: 10, date: nil,
+ installedApp: nil, isReadOnly: readOnly, notes: [], requiresPrivileges: privileged, heldBack: heldBack
+ )
+ }
+ let image = installer("org.example.dmg")
+ let backup = installer("org.example.Backup", readOnly: true)
+ let unmeasured = installer("org.example.pkg", heldBack: .notMeasured)
+ let refused = installer("org.example.Refused", heldBack: .beyondTheHelper, privileged: true)
+ let system = installer("org.example.System", privileged: true)
+ let items = [image, backup, unmeasured, refused, system]
+
+ let helped = items.selectableRows(canUseHelper: true)
+ #expect(helped.selectable == [image.url, unmeasured.url, system.url])
+ #expect(helped.recommended == [image.url, system.url])
+ let alone = items.selectableRows(canUseHelper: false)
+ #expect(alone.rows == items.map(\.url))
+ #expect(alone.selectable == [image.url, unmeasured.url])
+ #expect(alone.recommended == [image.url])
+ #expect(system.isLocked(canUseHelper: false) && !refused.isLocked(canUseHelper: false))
+ }
+
+ @Test func aSpaceAreaRecommendsItsOwnSuggestion() {
+ let (cache, unmeasured, documents, system, refused, unknown) = (
+ URL(filePath: "/tmp/org.example.Cache"), URL(filePath: "/tmp/org.example.Unmeasured"),
+ URL(filePath: "/tmp/org.example.Documents"), URL(filePath: "/tmp/org.example.System"),
+ URL(filePath: "/tmp/org.example.Refused"), URL(filePath: "/tmp/org.example.Unknown")
+ )
+ let plan = SpaceRemoval.Plan(
+ removable: [cache, unmeasured, documents, system, refused, unknown],
+ inUse: [],
+ leftToDeveloper: [],
+ sizes: [cache: 1, documents: 2, system: 3, refused: 4],
+ heldBack: [unmeasured: .notMeasured, documents: .holdsDocuments, refused: .beyondTheHelper],
+ needsTheHelper: [system, refused]
+ )
+
+ let helped = plan.selectableRows(canUseHelper: true)
+ #expect(helped.selectable == [cache, unmeasured, system, unknown])
+ #expect(helped.recommended == [cache, system])
+ let alone = plan.selectableRows(canUseHelper: false)
+ #expect(alone.rows == plan.removable)
+ #expect(alone.selectable == [cache, unmeasured, unknown])
+ #expect(alone.recommended == [cache])
+ #expect(plan.isLocked(system, canUseHelper: false) && !plan.isLocked(refused, canUseHelper: false))
+ }
}
diff --git a/Peel/Views/Developer/DeveloperDetailView.swift b/Peel/Views/Developer/DeveloperDetailView.swift
index 11e90ba2..678c3b12 100644
--- a/Peel/Views/Developer/DeveloperDetailView.swift
+++ b/Peel/Views/Developer/DeveloperDetailView.swift
@@ -31,11 +31,7 @@ struct DeveloperDetailView: View {
Text("Caches")
} actions: {
SelectMenu(
- list: SelectableRows(
- rows: environment.locations.map(\.url),
- selectable: environment.locations.map(\.url),
- recommended: environment.locations.filter(\.isRecommended).map(\.url)
- ),
+ list: environment.selectableRows,
place: Text(verbatim: environment.name),
selection: developer
)
diff --git a/Peel/Views/Installers/InstallerDetailView.swift b/Peel/Views/Installers/InstallerDetailView.swift
index 26cc83a6..fa7ebd1e 100644
--- a/Peel/Views/Installers/InstallerDetailView.swift
+++ b/Peel/Views/Installers/InstallerDetailView.swift
@@ -16,7 +16,7 @@ struct InstallerDetailView: View {
.listRowSeparator(.hidden)
RemovalsHeldBanner()
- if rows.contains(where: { $0.requiresPrivileges && $0.heldBack?.cannotBeMoved != true }), !helper.canAct {
+ if rows.contains(where: { $0.isLocked(canUseHelper: helper.canAct) }) {
HelperRequiredBanner()
}
@@ -32,7 +32,7 @@ struct InstallerDetailView: View {
warning: item.heldBack.map { String(localized: $0.explanation) },
size: item.size ?? 0,
isMeasured: item.size != nil,
- isLocked: isLocked(item),
+ isLocked: item.isLocked(canUseHelper: helper.canAct),
isLeftAlone: item.heldBack?.cannotBeMoved == true,
isFirst: item.id == rows.first?.id,
hasNoteColumn: hasNoteColumn,
@@ -46,15 +46,7 @@ struct InstallerDetailView: View {
heading(kind.title, kind.explanation)
} actions: {
SelectMenu(
- list: SelectableRows(
- rows: rows.map(\.url),
- selectable: rows.filter {
- !$0.isReadOnly && !isLocked($0) && $0.heldBack?.cannotBeMoved != true
- }.map(\.url),
- recommended: rows.filter {
- !$0.isReadOnly && !isLocked($0) && $0.heldBack == nil
- }.map(\.url)
- ),
+ list: rows.selectableRows(canUseHelper: helper.canAct),
place: Text(kind.title),
selection: installers
)
@@ -129,10 +121,6 @@ struct InstallerDetailView: View {
}
return item.notes.first.map(\.words)
}
-
- private func isLocked(_ item: InstallerItem) -> Bool {
- item.requiresPrivileges && !helper.canAct
- }
}
extension InstallerItem.Note {
diff --git a/Peel/Views/Orphans/OrphanDetailView.swift b/Peel/Views/Orphans/OrphanDetailView.swift
index d5f3f21b..408a82f5 100644
--- a/Peel/Views/Orphans/OrphanDetailView.swift
+++ b/Peel/Views/Orphans/OrphanDetailView.swift
@@ -17,7 +17,7 @@ struct OrphanDetailView: View {
} else if let unreadable = orphans.scan?.unreadableLocations, !unreadable.isEmpty {
UnreadableFoldersNotice(folders: unreadable.map(\.url))
}
- if group.items.contains(where: { $0.requiresPrivileges && $0.leftAlone == nil }), !helper.canAct {
+ if group.items.contains(where: { $0.isLocked(canUseHelper: helper.canAct) }) {
HelperRequiredBanner()
}
@@ -32,7 +32,7 @@ struct OrphanDetailView: View {
warning: warning(for: item),
size: item.size ?? 0,
isMeasured: item.size != nil,
- isLocked: isLocked(item),
+ isLocked: item.isLocked(canUseHelper: helper.canAct),
isLeftAlone: item.leftAlone != nil,
isFirst: index == 0,
hasNoteColumn: hasNoteColumn,
@@ -44,7 +44,11 @@ struct OrphanDetailView: View {
SectionHeaderLine {
Text("Files")
} actions: {
- SelectMenu(list: list, place: Text(verbatim: group.title), selection: orphans)
+ SelectMenu(
+ list: group.selectableRows(canUseHelper: helper.canAct),
+ place: Text(verbatim: group.title),
+ selection: orphans
+ )
}
}
}
@@ -101,18 +105,4 @@ struct OrphanDetailView: View {
.accessibilityLabel(Text("\(group.movable.text) can be moved to the Trash"))
}
}
-
- /// Peel recommends no row it held back: such a row waits to be chosen by hand, as Review Before Removing does on
- /// an app's page.
- private var list: SelectableRows<URL> {
- SelectableRows(
- rows: group.items.map(\.url),
- selectable: group.items.filter { !isLocked($0) && $0.leftAlone == nil }.map(\.url),
- recommended: group.items.filter { !isLocked($0) && $0.heldBack == nil }.map(\.url)
- )
- }
-
- private func isLocked(_ item: OrphanItem) -> Bool {
- item.requiresPrivileges && !helper.canAct
- }
}
diff --git a/Peel/Views/Projects/ProjectDetailView.swift b/Peel/Views/Projects/ProjectDetailView.swift
index f2c94e49..10c1b53d 100644
--- a/Peel/Views/Projects/ProjectDetailView.swift
+++ b/Peel/Views/Projects/ProjectDetailView.swift
@@ -33,11 +33,7 @@ struct ProjectDetailView: View {
)
} actions: {
SelectMenu(
- list: SelectableRows(
- rows: group.artifacts.map(\.url),
- selectable: group.artifacts.map(\.url),
- recommended: group.artifacts.filter(\.isRecommended).map(\.url)
- ),
+ list: group.artifacts.selectableRows,
place: Text(verbatim: group.project.lastPathComponent),
selection: projects
)
diff --git a/Peel/Views/Space/SpaceDetailView.swift b/Peel/Views/Space/SpaceDetailView.swift
index 73ca751f..8f641de6 100644
--- a/Peel/Views/Space/SpaceDetailView.swift
+++ b/Peel/Views/Space/SpaceDetailView.swift
@@ -21,7 +21,7 @@ struct SpaceDetailView: View {
header
.listRowSeparator(.hidden)
RemovalsHeldBanner()
- if let plan, plan.removable.contains(where: { isLocked($0, in: plan) }) {
+ if let plan, plan.removable.contains(where: { plan.isLocked($0, canUseHelper: helper.canAct) }) {
HelperRequiredBanner()
}
@@ -123,7 +123,7 @@ struct SpaceDetailView: View {
warning: plan.heldBack[url].map { String(localized: $0.explanation) },
size: plan.sizes[url] ?? 0,
isMeasured: plan.sizes[url] != nil,
- isLocked: isLocked(url, in: plan),
+ isLocked: plan.isLocked(url, canUseHelper: helper.canAct),
isLeftAlone: plan.heldBack[url]?.cannotBeMoved == true,
isFirst: index == 0,
hasNoteColumn: !plan.heldBack.isEmpty,
@@ -136,11 +136,7 @@ struct SpaceDetailView: View {
heading("Inside", "What is selected goes to the Trash, and History can put it back. The folders it sits in stay, since macOS expects to find them.")
} actions: {
SelectMenu(
- list: SelectableRows(
- rows: rows,
- selectable: rows.filter { !isLocked($0, in: plan) && plan.heldBack[$0]?.cannotBeMoved != true },
- recommended: rows.filter { !isLocked($0, in: plan) && plan.heldBack[$0] == nil }
- ),
+ list: plan.selectableRows(canUseHelper: helper.canAct),
place: Text(item.words.title),
selection: space
)
@@ -199,12 +195,6 @@ struct SpaceDetailView: View {
return plan == nil ? Text("in here") : Text("to remove")
}
- /// True for an item only an administrator can move while the helper cannot act. One the helper would refuse
- /// anyway is left alone rather than locked.
- private func isLocked(_ url: URL, in plan: SpaceRemoval.Plan) -> Bool {
- plan.needsTheHelper.contains(url) && plan.heldBack[url]?.cannotBeMoved != true && !helper.canAct
- }
-
private static func ordered(_ plan: SpaceRemoval.Plan) -> [URL] {
plan.removable.sorted { first, second in
SizeTotal([plan.sizes[second]]) < SizeTotal([plan.sizes[first]])