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 PeelCore
import SwiftUI
import UniformTypeIdentifiers
/// Which `brew` Peel runs: the one where Homebrew installs itself, or one the person chose for a Homebrew in a
/// folder of its own.
struct HomebrewSection: View {
@Environment(HomebrewLibrary.self) private var homebrew
@State private var choice = HomebrewChoice().load()
@State private var isChoosing = false
@State private var confirming: URL?
@State private var refusal: HomebrewChoice.Refusal?
@State private var whyNotSaved: FileProblem?
var body: some View {
let unrunnable = HomebrewChoice().unrunnable()
let refused = Homebrew.refusedInAUsualPlace()
let brew = unrunnable?.brew ?? refused?.brew ?? Homebrew.executableURL
Section {
LabeledContent {
HStack(spacing: 8) {
Text(brew?.abbreviatedPath ?? String(localized: "Not found"))
.lineLimit(1)
.truncationMode(.middle)
Button("Choose…") { isChoosing = true }
if choice != nil {
Button("Use the Usual Places") { use(nil) }
}
}
.editingControls()
} label: {
Text(verbatim: "brew")
}
if let unrunnable {
VStack(alignment: .leading, spacing: 4) {
StatusLabel(title: Text("Peel runs only the `brew` you chose, and that one can’t be run now."))
unrunnable.refusal.explanation
.font(.callout)
.foregroundStyle(.secondary)
}
} else if let refused {
VStack(alignment: .leading, spacing: 4) {
StatusLabel(title: Text("Peel doesn’t run this `brew`."))
refused.refusal.explanation
.font(.callout)
.foregroundStyle(.secondary)
}
}
if let whyNotSaved {
StatusLabel(title: Text(notSaved(whyNotSaved)))
}
} header: {
titled("Homebrew", "Peel runs the `brew` in /opt/homebrew or /usr/local. If your Homebrew is in a folder of its own, such as /opt/brew, choose its `brew`, and Peel and `peel` run that one.")
}
.fileImporter(isPresented: $isChoosing, allowedContentTypes: [.item]) { result in
guard let url = try? result.get() else { return }
if let why = HomebrewChoice.refusal(of: url) {
refusal = why
} else {
confirming = url
}
}
.fileDialogDefaultDirectory(URL(filePath: "/opt", directoryHint: .isDirectory))
.alert("Peel can’t run this file.", isPresented: isShowingRefusal) {
Button("OK", role: .cancel) {}
} message: {
refusal?.explanation ?? Text(verbatim: "")
}
.alert("Run this brew for Homebrew?", isPresented: isConfirming, presenting: confirming) { brew in
Button("Use It") { use(brew) }
Button("Cancel", role: .cancel) {}
} message: { brew in
Text("Peel runs \(brew.abbreviatedPath) whenever it asks Homebrew something, and so does `peel`. Choose it only if it belongs to your Homebrew.")
}
}
private func notSaved(_ why: FileProblem) -> AttributedString {
why.said(after: AttributedString(localized: "Peel couldn’t save which `brew` to run."), about: HomebrewChoice().url)
}
private var isShowingRefusal: Binding<Bool> {
Binding(get: { refusal != nil }, set: { if !$0 { refusal = nil } })
}
private var isConfirming: Binding<Bool> {
Binding(get: { confirming != nil }, set: { if !$0 { confirming = nil } })
}
private func use(_ brew: URL?) {
whyNotSaved = HomebrewChoice().save(brew)
choice = HomebrewChoice().load()
Task { await homebrew.readAnotherBrew() }
}
}
extension HomebrewChoice.Refusal {
var explanation: Text {
switch self {
case .notBrew: Text("Choose the file called `brew`, in the bin folder of your Homebrew.")
case .notAProgram: Text("It isn’t a program Peel can run.")
case .ownedByAnotherAccount(let owner?):
Text("The account \(owner) owns it, so that account could change what Peel would run. Peel runs only a brew your account or the system owns.")
case .ownedByAnotherAccount(nil):
Text("Another account owns it, so that account could change what Peel would run. Peel runs only a brew your account or the system owns.")
case .writableByEveryone: Text("Anyone on this Mac can change it, so Peel won’t run it.")
}
}
}