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.

Never look for apps in the Trash

Țugui Dragoș74141ebe21d2
diff --git a/Localization/Peel/Localizable.xcstrings b/Localization/Peel/Localizable.xcstrings
index 4a84341e..755260c4 100644
--- a/Localization/Peel/Localizable.xcstrings
+++ b/Localization/Peel/Localizable.xcstrings
@@ -158123,6 +158123,112 @@
}
}
},
+ "The Trash holds what is on its way out, so Peel doesn’t look for apps there.\n\n%@" : {
+ "localizations" : {
+ "cs" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Koš obsahuje položky určené k vyhození, a proto v něm Peel aplikace nehledá.\n\n%@"
+ }
+ },
+ "de" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Im Papierkorb liegt, was weggeworfen wird, daher sucht Peel dort nicht nach Apps.\n\n%@"
+ }
+ },
+ "es" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "La papelera contiene lo que se va a eliminar, así que Peel no busca apps ahí.\n\n%@"
+ }
+ },
+ "fr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "La corbeille contient ce que vous jetez : Peel n’y recherche donc pas d’apps.\n\n%@"
+ }
+ },
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Il Cestino contiene ciò che è destinato a essere eliminato, quindi Peel non cerca app al suo interno.\n\n%@"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "ゴミ箱には、捨てようとしているものが入っているため、Peelはそこではアプリを探しません。\n\n%@"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "휴지통에는 버릴 항목이 들어 있으므로 Peel은 그곳에서 앱을 찾지 않습니다.\n\n%@"
+ }
+ },
+ "nl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "De prullenmand bevat wat wordt weggegooid, dus zoekt Peel daar niet naar apps.\n\n%@"
+ }
+ },
+ "pl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Kosz zawiera rzeczy przeznaczone do usunięcia, więc Peel nie szuka tam aplikacji.\n\n%@"
+ }
+ },
+ "pt" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "O Lixo contém o que está sendo descartado, então o Peel não procura apps lá.\n\n%@"
+ }
+ },
+ "ro" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Coșul conține ce urmează să fie aruncat, așa că Peel nu caută aplicații acolo.\n\n%@"
+ }
+ },
+ "ru" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "В Корзине лежит то, что предстоит удалить, поэтому Peel не ищет там приложения.\n\n%@"
+ }
+ },
+ "sv" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Papperskorgen innehåller det som är på väg att slängas, så Peel letar inte efter appar där.\n\n%@"
+ }
+ },
+ "tr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Çöp Sepeti, atılmak üzere olanları tutar, bu yüzden Peel orada uygulama aramaz.\n\n%@"
+ }
+ },
+ "uk" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "У Смітнику лежить те, що ви викидаєте, тож Peel не шукає там програм.\n\n%@"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "废纸篓中存放的是即将丢弃的内容,因此Peel不会在其中查找App。\n\n%@"
+ }
+ },
+ "zh-Hant" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "「垃圾桶」存放的是準備丟棄的項目,因此Peel不會在那裡尋找App。\n\n%@"
+ }
+ }
+ }
+ },
"The address written inside the app, and wherever it redirects. For an update that waits, also the page of release notes the feed names, once per version." : {
"localizations" : {
"cs" : {
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
index 4575dd37..397f8697 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
@@ -20,7 +20,8 @@ public enum AppCatalog {
/// Where Peel looks for apps in `environment`: its Applications folders and the folders chosen in its home.
static func directories(in environment: SearchEnvironment) -> [URL] {
- let chosen = AppFolders(url: AppFolders.url(inHome: environment.homeDirectory)).load() ?? []
+ let home = environment.homeDirectory
+ let chosen = AppFolders(url: AppFolders.url(inHome: home), home: home).load() ?? []
return standardDirectories(home: environment.homeDirectory, root: environment.rootDirectory) + chosen
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppFolders.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppFolders.swift
index 63c89b98..2c43cd1f 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppFolders.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppFolders.swift
@@ -9,12 +9,20 @@ public struct AppFolders: Sendable {
case tooBroad
/// An Applications folder, or one inside it, which Peel already looks in.
case alreadyLookedIn
+ /// The Trash, or a folder in it, which holds what is on its way out.
+ case inATrash
}
public let url: URL
+ let home: URL
public init(url: URL = AppFolders.defaultURL) {
+ self.init(url: url, home: .homeDirectory)
+ }
+
+ init(url: URL, home: URL) {
self.url = url
+ self.home = home
}
public static var defaultURL: URL {
@@ -63,6 +71,7 @@ public struct AppFolders: Sendable {
let path = folded(folder)
let broad = ["/", "/users", "/volumes", "/system", "/library", folded(home), folded(home) + "/library"]
if broad.contains(path) { return .tooBroad }
+ if TrashService.isAtOrInsideATrash(folder, home: home) { return .inATrash }
let lookedIn = AppCatalog.standardDirectories(home: home).contains {
PathComponents.isPath(path, atOrInside: folded($0))
}
@@ -85,9 +94,11 @@ public struct AppFolders: Sendable {
}
}
+ /// The folders saved, less any in a Trash, which a list saved before such a folder was refused can hold.
private func read() -> [URL]? {
guard let data = BoundedRead.data(at: url), let paths = try? JSONDecoder().decode([String].self, from: data)
else { return nil }
return paths.map { URL(filePath: $0, directoryHint: .isDirectory) }
+ .filter { !TrashService.isAtOrInsideATrash($0, home: home) }
}
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index 9ad7f6da..a031168b 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -777,15 +777,27 @@ public struct TrashService: Sendable {
/// The Trashes an item in `folder` could be in, with links resolved: the home's, and its volume's.
private func trashes(forItemsIn folder: String) -> [String] {
- var trashes = [URL.homeDirectory, environment.homeDirectory].map {
- $0.appending(path: ".Trash", directoryHint: .isDirectory)
- }
+ Self.trashes(forItemsIn: folder, homes: [.homeDirectory, environment.homeDirectory])
+ }
+
+ private static func trashes(forItemsIn folder: String, homes: [URL]) -> [String] {
+ var trashes = homes.map { $0.appending(path: ".Trash", directoryHint: .isDirectory) }
if let volume = try? URL(filePath: folder).resourceValues(forKeys: [.volumeURLKey]).volume {
trashes.append(volume.appending(path: ".Trashes/\(getuid())", directoryHint: .isDirectory))
}
return trashes.compactMap { PrivilegedPathPolicy.resolvedPath($0.path(percentEncoded: false)) }
}
+ /// Whether `url` is a Trash or inside one, links resolved: `home`'s, or its volume's. What isn't there is in none.
+ public static func isAtOrInsideATrash(_ url: URL, home: URL = .homeDirectory) -> Bool {
+ guard !url.pathComponents.contains(where: { $0 == "." || $0 == ".." }),
+ let resolved = PrivilegedPathPolicy.resolvedPath(url.path(percentEncoded: false))
+ else { return false }
+ return trashes(forItemsIn: resolved, homes: [.homeDirectory, home]).contains {
+ PathComponents.isPath(resolved, atOrInside: $0)
+ }
+ }
+
/// The failure reason for an error a move threw. macOS does not say which permission is missing, so any
/// refusal is `.notPermitted`, and the caller decides what it means.
static func reason(for error: any Error) -> TrashFailure.Reason {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppFoldersTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppFoldersTests.swift
index aa22c7c5..ec77818e 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppFoldersTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppFoldersTests.swift
@@ -18,6 +18,43 @@ struct AppFoldersTests {
}
}
+ @Test func refusesTheTrashAndAFolderInIt() throws {
+ let directory = try TemporaryDirectory()
+ let trash = try directory.directory(".Trash")
+ let inTheTrash = try directory.directory(".Trash/Apps")
+ let tools = try directory.directory("Tools")
+
+ #expect(AppFolders.refusal(of: trash, home: directory.url) == .inATrash)
+ #expect(AppFolders.refusal(of: inTheTrash, home: directory.url) == .inATrash)
+ #expect(AppFolders.refusal(of: tools, home: directory.url) == nil)
+ }
+
+ @Test func leavesOutAFolderInTheTrashChosenBefore() throws {
+ let directory = try TemporaryDirectory()
+ let inTheTrash = try directory.directory(".Trash/Apps")
+ let tools = try directory.directory("Tools")
+ let saved = [inTheTrash, tools].map { $0.path(percentEncoded: false) }
+ let list = try directory.file("app-folders.json", contents: JSONEncoder().encode(saved))
+
+ #expect(AppFolders(url: list, home: directory.url).load()?.map(\.lastPathComponent) == ["Tools"])
+ }
+
+ @Test func aReadingOfAnotherHomeLeavesOutAFolderInThatHomesTrash() throws {
+ let directory = try TemporaryDirectory()
+ let saved = [try directory.directory(".Trash/Apps"), try directory.directory("Tools")]
+ .map { $0.path(percentEncoded: false) }
+ try directory.file("Library/Application Support/Peel/app-folders.json", contents: JSONEncoder().encode(saved))
+ let environment = SearchEnvironment(
+ homeDirectory: directory.url,
+ rootDirectory: directory.url.appending(path: "root")
+ )
+
+ let read = AppCatalog.directories(in: environment).map(\.lastPathComponent)
+
+ #expect(read.contains("Tools"))
+ #expect(!read.contains("Apps"))
+ }
+
@Test func remembersTheFoldersChosenForApps() throws {
let directory = try TemporaryDirectory()
let folders = AppFolders(url: directory.url.appending(path: "app-folders.json"))
diff --git a/Peel/Views/Settings/AppFoldersSection.swift b/Peel/Views/Settings/AppFoldersSection.swift
index ed827c19..559f1d2f 100644
--- a/Peel/Views/Settings/AppFoldersSection.swift
+++ b/Peel/Views/Settings/AppFoldersSection.swift
@@ -7,9 +7,17 @@ struct AppFoldersSection: View {
@State private var isChoosing = false
@State private var selected: Set<URL> = []
@State private var refused: [String] = []
+ @State private var refusedInATrash: [String] = []
private var isShowingRefusal: Binding<Bool> {
- Binding(get: { !refused.isEmpty }, set: { if !$0 { refused = [] } })
+ Binding(
+ get: { !refused.isEmpty || !refusedInATrash.isEmpty },
+ set: {
+ guard !$0 else { return }
+ refused = []
+ refusedInATrash = []
+ }
+ )
}
var body: some View {
@@ -59,16 +67,30 @@ struct AppFoldersSection: View {
isPresented: $isChoosing, allowedContentTypes: [.folder], allowsMultipleSelection: true
) { result in
guard let urls = try? result.get() else { return }
- let refusedURLs = urls.filter { AppFolders.refusal(of: $0) != nil }
- refused = refusedURLs.map(\.abbreviatedPath)
- let adding = urls.filter { !refusedURLs.contains($0) }
+ let refusals = urls.compactMap { url in AppFolders.refusal(of: url).map { (url: url, refusal: $0) } }
+ refused = refusals.filter { $0.refusal != .inATrash }.map(\.url.abbreviatedPath)
+ refusedInATrash = refusals.filter { $0.refusal == .inATrash }.map(\.url.abbreviatedPath)
+ let adding = urls.filter { url in !refusals.contains { $0.url == url } }
guard !adding.isEmpty else { return }
library.addFolders(adding)
}
- .alert(refused.count == 1 ? "That folder can’t be added." : "These folders can’t be added.", isPresented: isShowingRefusal) {
+ .alert(
+ refused.count + refusedInATrash.count == 1 ? "That folder can’t be added." : "These folders can’t be added.",
+ isPresented: isShowingRefusal
+ ) {
Button("OK", role: .cancel) {}
} message: {
- Text("Peel already looks in the Applications folders, and a folder as wide as a whole disk, your home folder, or a Library would slow down every look for apps. Choose the folder that holds the apps.\n\n\(refused.joined(separator: "\n"))")
+ refusalMessage
+ }
+ }
+
+ private var refusalMessage: Text {
+ let others = Text("Peel already looks in the Applications folders, and a folder as wide as a whole disk, your home folder, or a Library would slow down every look for apps. Choose the folder that holds the apps.\n\n\(refused.joined(separator: "\n"))")
+ let inATrash = Text("The Trash holds what is on its way out, so Peel doesn’t look for apps there.\n\n\(refusedInATrash.joined(separator: "\n"))")
+ switch (refused.isEmpty, refusedInATrash.isEmpty) {
+ case (false, true): return others
+ case (true, false): return inATrash
+ default: return Text("\(others)\n\n\(inATrash)")
}
}