Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
import Accessibility
import PeelCore
import SwiftUI
// What each tool found: the summary its pane shows, and what VoiceOver reads when a scan ends.
//
// Each sentence is one localized string with its numbers inside, so a translation can put the parts in any
// order, as Japanese or Turkish need. The sentences avoid verbs that would have to agree with a number, and the
// total comes first because `SizeTotal.text` ("Over X") is written to stand on its own.
extension Tool {
/// What the tool found, as its page says it and the menu bar panel repeats it, or nil for a tool with no such
/// sentence.
func sentence(for looked: Looked) -> AttributedString? {
let count = looked.count
let total = looked.size.flatMap { $0.known > 0 ? $0.text : nil }
switch self {
case .applications, .homebrew:
return AttributedString(localized: "^[\(count) update](inflect: true) waiting.")
case .orphans, .duplicates:
return total.map { AttributedString(localized: "\($0) in ^[\(count) group](inflect: true).") }
?? AttributedString(localized: "^[\(count) group](inflect: true).")
case .developer:
return total.map { AttributedString(localized: "\($0) in ^[\(count) tool](inflect: true).") }
?? AttributedString(localized: "^[\(count) tool](inflect: true).")
case .projects:
return total.map { AttributedString(localized: "\($0) in ^[\(count) project](inflect: true).") }
?? AttributedString(localized: "^[\(count) project](inflect: true).")
case .space:
return AttributedString(localized: "^[\(count) area](inflect: true).")
case .installers, .cloud, .fileSearch:
return total.map { AttributedString(localized: "\($0) in ^[\(count) item](inflect: true).") }
?? AttributedString(localized: "^[\(count) item](inflect: true).")
case .intel:
return AttributedString(localized: "^[\(count) Intel item](inflect: true).")
case .home, .packages, .backgroundItems, .extensions, .plugins, .tweaks, .terminal, .history:
return nil
}
}
/// What the tool found in a word or two, for the menu bar panel's Found list: the size when it is known, and
/// otherwise the count. Space only counts, since it never adds its areas up.
func figure(for looked: Looked) -> String? {
if self != .space, let size = looked.size, size.known > 0 { return size.text }
let count = looked.count
let words: AttributedString? = switch self {
case .applications, .homebrew: AttributedString(localized: "^[\(count) update](inflect: true)")
case .orphans, .duplicates: AttributedString(localized: "^[\(count) group](inflect: true)")
case .developer: AttributedString(localized: "^[\(count) tool](inflect: true)")
case .projects: AttributedString(localized: "^[\(count) project](inflect: true)")
case .space: AttributedString(localized: "^[\(count) area](inflect: true)")
case .installers, .cloud, .fileSearch, .intel: AttributedString(localized: "^[\(count) item](inflect: true)")
case .home, .packages, .backgroundItems, .extensions, .plugins, .tweaks, .terminal, .history: nil
}
return words.map { String($0.characters) }
}
/// The sentence for what `looked` holds, or nil while the tool hasn't looked or found nothing.
func summary(of looked: Looked?) -> AttributedString? {
guard let looked, looked.count > 0 else { return nil }
return sentence(for: looked)
}
}
extension AppLibrary {
var summary: AttributedString? {
guard hasLoaded else { return nil }
let count = apps.count
let known = sizes.values.cappedSum
let total = SizeTotal(known: known, isComplete: sizes.count >= count)
let size = known > 0 ? AttributedString(localized: "\(total.text) in ^[\(count) app](inflect: true).") : AttributedString(localized: "^[\(count) app](inflect: true).")
guard let updates = Tool.applications.summary(of: Looked(count: appsWithUpdates.count, size: nil)) else {
return size
}
return size + AttributedString("\n") + updates
}
}
extension OrphanLibrary {
func summary(canUseHelper: Bool) -> AttributedString? {
Self.summary(
groups: Tool.orphans.summary(of: looked(canUseHelper: canUseHelper)),
forAnAdministrator: scan?.forAnAdministrator.count ?? 0
)
}
/// The groups' sentence, or, with no group, how many items only an administrator can move.
static func summary(groups: AttributedString?, forAnAdministrator count: Int) -> AttributedString? {
groups ?? (count > 0 ? AttributedString(localized: "^[\(count) item](inflect: true) only an administrator can move.") : nil)
}
}
extension DeveloperLibrary {
var summary: AttributedString? { environments.flatMap(Self.summary(of:)) }
/// What was found, and how much of it Peel recommends when that is only part of it: models and archives count in
/// the total and are never selected for the person.
static func summary(of environments: [DeveloperEnvironment]) -> AttributedString? {
let count = environments.count
let total = SizeTotal(combining: environments.map(\.total))
guard let found = Tool.developer.summary(of: Looked(count: count, size: total)) else { return nil }
let recommended = SizeTotal(combining: environments.map(\.recommendedTotal))
guard total.known > 0, recommended != total else { return found }
guard recommended.known > 0 else {
return AttributedString(localized: "\(total.text) in ^[\(count) tool](inflect: true), none of it recommended.")
}
return AttributedString(
localized: "\(total.text) in ^[\(count) tool](inflect: true), \(recommended.text) of it recommended."
)
}
}
extension ProjectLibrary {
var summary: AttributedString? { groups.flatMap(Self.summary(of:)) }
/// What was found, and how much of it Peel recommends when that is only part of it: what has a generic name, what
/// is installed, and what changed lately count in the total and are never selected for the person.
static func summary(of groups: [ProjectGroup]) -> AttributedString? {
let count = groups.count
let total = SizeTotal(combining: groups.map(\.total))
guard let found = Tool.projects.summary(of: Looked(count: count, size: total)) else { return nil }
let recommended = SizeTotal(groups.flatMap(\.artifacts).filter(\.isRecommended).map(\.size))
guard total.known > 0, recommended != total else { return found }
guard recommended.known > 0 else {
return AttributedString(localized: "\(total.text) in ^[\(count) project](inflect: true), none of it recommended.")
}
return AttributedString(
localized: "\(total.text) in ^[\(count) project](inflect: true), \(recommended.text) of it recommended."
)
}
}
extension SpaceLibrary {
var summary: AttributedString? { Tool.space.summary(of: looked) }
}
extension InstallerLibrary {
var summary: AttributedString? { Tool.installers.summary(of: looked) }
}
extension HomebrewLibrary {
var summary: AttributedString? {
guard let packages, !packages.isEmpty else { return nil }
let count = AttributedString(localized: "^[\(packages.count) package](inflect: true).")
guard let updates = Tool.homebrew.summary(of: looked) else { return count }
return count + AttributedString("\n") + updates
}
}
extension FileSearchLibrary {
var summary: AttributedString? {
results.flatMap { results in
Tool.fileSearch.summary(
of: Looked(
count: results.files.count,
size: SizeTotal(known: results.files.map(\.size).cappedSum, isComplete: true)
)
)
}
}
}
extension DuplicateLibrary {
var summary: AttributedString? { Tool.duplicates.summary(of: looked) }
}
extension CloudLibrary {
var summary: AttributedString? { Tool.cloud.summary(of: looked) }
/// The title of the page's message when an empty list doesn't mean there is nothing to free.
var couldNotLook: LocalizedStringResource? {
if couldNotRead { return "iCloud Drive Couldn’t Be Read" }
guard files?.isEmpty == true else { return nil }
if hasMoreThanItRead { return "Nothing Found So Far" }
return isNotSetUp ? "iCloud Drive Isn’t Set Up" : nil
}
}
extension PackageLibrary {
var summary: AttributedString? {
guard let receipts, !receipts.isEmpty else { return nil }
return AttributedString(localized: "^[\(receipts.count) package](inflect: true).")
}
}
extension PluginLibrary {
var summary: AttributedString? {
guard let plugins, !plugins.isEmpty else { return nil }
return AttributedString(localized: "^[\(plugins.count) plug-in](inflect: true).")
}
}
extension ExtensionLibrary {
var summary: AttributedString? {
guard let extensions, !extensions.isEmpty else { return nil }
return AttributedString(localized: "^[\(extensions.count) extension](inflect: true).")
}
}
extension BackgroundItemLibrary {
var summary: AttributedString? {
guard let items, !items.isEmpty else { return nil }
return AttributedString(localized: "^[\(items.count) background item](inflect: true).")
}
}
extension IntelLibrary {
var summary: AttributedString? { Tool.intel.summary(of: looked) }
}
extension RemovalHistoryStore {
var summary: AttributedString? {
guard !batches.isEmpty else { return nil }
return AttributedString(localized: "^[\(batches.count) removal](inflect: true).")
}
}
extension AttributedString {
/// These words as an announcement VoiceOver queues after what it is saying. At the default priority it would cut
/// that off.
var waitingForOtherSpeech: AttributedString {
var words = self
words.accessibilitySpeechAnnouncementPriority = .low
return words
}
}
extension View {
/// Tells VoiceOver how a scan ended, since the list fills in silently: what it found, that it was stopped, or,
/// when it found nothing to tell, `couldNotLook`, the page's own words for why, in place of "Nothing found.". The
/// summary is worked out only then, so the page does not depend on everything it reads. It waits for what is being
/// said, since a scan often ends right after the action that caused it says how it went.
func announcesScan(
_ isScanning: Bool,
found summary: @autoclosure @escaping () -> AttributedString?,
couldNotLook: LocalizedStringResource? = nil,
wasStopped: Bool = false
) -> some View {
onChange(of: isScanning) { wasScanning, scanning in
guard wasScanning, !scanning else { return }
let nothing = couldNotLook.map { AttributedString(localized: $0) }
?? AttributedString(localized: "Nothing found.")
let words = wasStopped ? AttributedString(localized: "Scan stopped.") : summary() ?? nothing
AccessibilityNotification.Announcement(words.waitingForOtherSpeech).post()
}
}
}