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.

Test what the Applications list keeps for a search and its filters

Țugui Dragoș47d75538ce52
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppListFilter.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppListFilter.swift
new file mode 100644
index 00000000..26f83851
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppListFilter.swift
@@ -0,0 +1,43 @@
+/// Where an app came from, as the Applications list's filter tells them apart.
+public enum AppSource: String, CaseIterable, Identifiable, Sendable {
+ case appStore
+ case homebrew
+ case setapp
+ case direct
+
+ public var id: String { rawValue }
+
+ public static func of(_ app: InstalledApp, installedByHomebrew: Bool) -> AppSource {
+ if app.isFromAppStore { return .appStore }
+ if app.isFromSetapp { return .setapp }
+ return installedByHomebrew ? .homebrew : .direct
+ }
+}
+
+/// What the Applications list keeps of the installed apps: what a search finds by name, identifier or developer,
+/// narrowed by each filter that is on.
+public struct AppListFilter: Sendable, Hashable {
+ public var text = ""
+ public var sources: Set<AppSource> = []
+ public var developer: String?
+ public var onlyUnused = false
+
+ public init(text: String = "", sources: Set<AppSource> = [], developer: String? = nil, onlyUnused: Bool = false) {
+ self.text = text
+ self.sources = sources
+ self.developer = developer
+ self.onlyUnused = onlyUnused
+ }
+
+ public func keeps(
+ _ app: InstalledApp, source: AppSource, developer appsDeveloper: String?, isUnused: Bool
+ ) -> Bool {
+ if !text.isEmpty, !SearchText.matches(app.name, text), !SearchText.matches(app.bundleIdentifier, text),
+ !(appsDeveloper.map { SearchText.matches($0, text) } ?? false) {
+ return false
+ }
+ if !sources.isEmpty, !sources.contains(source) { return false }
+ if let developer, appsDeveloper != developer { return false }
+ return !onlyUnused || isUnused
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppListFilterTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppListFilterTests.swift
new file mode 100644
index 00000000..2221874e
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppListFilterTests.swift
@@ -0,0 +1,37 @@
+import Foundation
+@testable import PeelCore
+import Testing
+
+struct AppListFilterTests {
+ private let app = InstalledApp(
+ url: URL(filePath: "/Applications/Studio.app"), bundleIdentifier: "org.example.studio", name: "Studio"
+ )
+
+ private func keeps(_ filter: AppListFilter, source: AppSource = .direct, developer: String? = "Example Inc.",
+ isUnused: Bool = false) -> Bool {
+ filter.keeps(app, source: source, developer: developer, isUnused: isUnused)
+ }
+
+ @Test func searchFindsAnAppByItsNameItsIdentifierOrItsDeveloper() {
+ #expect(keeps(AppListFilter(text: "stud")))
+ #expect(keeps(AppListFilter(text: "example.studio")))
+ #expect(keeps(AppListFilter(text: "example inc")))
+ #expect(!keeps(AppListFilter(text: "editor")))
+ #expect(!keeps(AppListFilter(text: "example inc"), developer: nil))
+ }
+
+ @Test func eachFilterOnNarrowsTheList() {
+ #expect(keeps(AppListFilter()))
+ #expect(keeps(AppListFilter(sources: [.appStore, .direct])))
+ #expect(!keeps(AppListFilter(sources: [.homebrew])))
+ #expect(keeps(AppListFilter(developer: "Example Inc.")))
+ #expect(!keeps(AppListFilter(developer: "Someone Else")))
+ #expect(!keeps(AppListFilter(onlyUnused: true)))
+ #expect(keeps(AppListFilter(onlyUnused: true), isUnused: true))
+ }
+
+ @Test func anAppComesFromTheAppStoreSetappHomebrewOrADownload() {
+ #expect(AppSource.of(app, installedByHomebrew: false) == .direct)
+ #expect(AppSource.of(app, installedByHomebrew: true) == .homebrew)
+ }
+}
diff --git a/Peel/Model/AppLibrary.swift b/Peel/Model/AppLibrary.swift
index 5faf02d3..95887c7f 100644
--- a/Peel/Model/AppLibrary.swift
+++ b/Peel/Model/AppLibrary.swift
@@ -20,14 +20,7 @@ enum AppSort: String, CaseIterable, Identifiable {
}
}
-enum AppSource: String, CaseIterable, Identifiable {
- case appStore
- case homebrew
- case setapp
- case direct
-
- var id: String { rawValue }
-
+extension AppSource {
var title: LocalizedStringResource {
switch self {
case .appStore: "App Store"
@@ -376,9 +369,7 @@ final class AppLibrary {
}
func source(of app: InstalledApp) -> AppSource {
- if app.isFromAppStore { return .appStore }
- if app.isFromSetapp { return .setapp }
- return homebrewApps[app.id] != nil ? .homebrew : .direct
+ AppSource.of(app, installedByHomebrew: homebrewApps[app.id] != nil)
}
func developer(of app: InstalledApp) -> String? {
@@ -459,21 +450,11 @@ final class AppLibrary {
}
private func visible(matching searchText: String) -> VisibleApps {
- var result = apps
- if !searchText.isEmpty {
- result = result.filter {
- SearchText.matches($0.name, searchText) || SearchText.matches($0.bundleIdentifier, searchText)
- || developer(of: $0).map { SearchText.matches($0, searchText) } == true
- }
- }
- if !sources.isEmpty {
- result = result.filter { sources.contains(source(of: $0)) }
- }
- if let selectedDeveloper {
- result = result.filter { developer(of: $0) == selectedDeveloper }
- }
- if showsOnlyUnused {
- result = result.filter(isUnused)
+ let filter = AppListFilter(
+ text: searchText, sources: sources, developer: selectedDeveloper, onlyUnused: showsOnlyUnused
+ )
+ let result = apps.filter {
+ filter.keeps($0, source: source(of: $0), developer: developer(of: $0), isUnused: isUnused($0))
}
var waiting: [InstalledApp] = []