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.

End Homebrew's progress and spinners when Homebrew finishes

Țugui Dragoș82b4e23f8633
diff --git a/Peel/Model/HomebrewLibrary.swift b/Peel/Model/HomebrewLibrary.swift
index 955d48aa..7e2a1112 100644
--- a/Peel/Model/HomebrewLibrary.swift
+++ b/Peel/Model/HomebrewLibrary.swift
@@ -127,10 +127,18 @@ final class HomebrewLibrary {
/// doesn't end it.
let scanRun = ScanRun()
var isScanning: Bool { scanRun.isRunning }
+ /// The command the page is busy with: Homebrew runs it, then Peel reads what it changed, and nothing else starts
+ /// until both are done.
private(set) var runningCommand: Command?
- /// The casks whose records Peel is moving to the Trash. No command runs beside it.
+ /// The casks the page is forgetting, until Peel has read Homebrew again. No command runs beside it.
private(set) var forgetting: Set<HomebrewPackage.ID> = []
var isForgetting: Bool { !forgetting.isEmpty }
+ /// True once a command or a Forget has finished and Peel reads Homebrew again to see what it changed.
+ private(set) var isReadingWhatChanged = false
+ /// The command while Homebrew itself runs it, which is what its spinner, "Running Homebrew…" and Stop stand for.
+ var commandHomebrewRuns: Command? { isReadingWhatChanged ? nil : runningCommand }
+ /// The casks while Peel moves their records, which is what Forget's spinner stands for.
+ var casksBeingForgotten: Set<HomebrewPackage.ID> { isReadingWhatChanged ? [] : forgetting }
/// The last lines Homebrew has written in the running upgrade. Nil when no upgrade runs.
private(set) var progress: String?
/// True once the person asked the running upgrade to stop.
@@ -306,7 +314,7 @@ final class HomebrewLibrary {
guard runningCommand == nil, !isForgetting, command.onlyLooks || mayChange else { return nil }
runningCommand = command
defer {
- (runningCommand, runningTask, progress, isStopping) = (nil, nil, nil, false)
+ (runningCommand, isReadingWhatChanged) = (nil, false)
}
let task = Task { await outcome(of: command) }
runningTask = task
@@ -321,8 +329,12 @@ final class HomebrewLibrary {
} else {
outcome = await QuitGuard.shared.run(stoppingWith: stop) { await task.value }
}
+ (runningTask, progress, isStopping) = (nil, nil, false)
if showsResult, let outcome { result = outcome }
- if !command.onlyLooks { await refresh(includingReclaimable: true) }
+ if !command.onlyLooks {
+ isReadingWhatChanged = true
+ await refresh(includingReclaimable: true)
+ }
return outcome
}
@@ -331,13 +343,14 @@ final class HomebrewLibrary {
func forget(_ casks: [HomebrewPackage], recording record: (TrashResult) async -> Void) async -> TrashResult? {
guard runningCommand == nil, !isForgetting, mayChange else { return nil }
forgetting = Set(casks.map(\.id))
- defer { forgetting = [] }
+ defer { (forgetting, isReadingWhatChanged) = ([], false) }
let result = await QuitGuard.shared.run {
let result = await HomebrewReceipt.forget(casks, exclusions: ExclusionsStore.shared.exclusions)
// Written down before Homebrew is read again, which can take a while: History is the way back.
await record(result)
return result
}
+ isReadingWhatChanged = true
await refresh(includingReclaimable: true)
return result
}
@@ -352,7 +365,7 @@ final class HomebrewLibrary {
/// Stops the running upgrade: Homebrew is asked to stop, and killed if it does not.
func stopUpgrade() {
- guard runningCommand?.isUpgrade == true else { return }
+ guard commandHomebrewRuns?.isUpgrade == true else { return }
isStopping = true
runningTask?.cancel()
}
diff --git a/Peel/Views/Homebrew/HomebrewDetailView.swift b/Peel/Views/Homebrew/HomebrewDetailView.swift
index 13ddefea..4de23fd4 100644
--- a/Peel/Views/Homebrew/HomebrewDetailView.swift
+++ b/Peel/Views/Homebrew/HomebrewDetailView.swift
@@ -194,6 +194,7 @@ struct HomebrewDetailView: View {
}
}
.formStyle(.grouped)
+ .dimmedWhileBusy(homebrew.isReadingWhatChanged)
.navigationTitle(package.name)
.toolbar(removing: .title)
// Asked first, as Upgrade All is: an upgrade can run for a long time while Homebrew builds on this Mac.
@@ -293,8 +294,8 @@ struct HomebrewDetailView: View {
}
private var isRunning: Bool {
- if homebrew.forgetting.contains(package.id) { return true }
- return switch homebrew.runningCommand {
+ if homebrew.casksBeingForgotten.contains(package.id) { return true }
+ return switch homebrew.commandHomebrewRuns {
case .upgrade(let id), .uninstall(let id): id == package.id
case .upgradeAll: package.isOutdated && package.joinsUpgradeAll
case .update, .cleanup, .clearOlderDownloads, .clearEveryDownload, .repairTaps, .health, .vulnerabilities, nil:
diff --git a/Peel/Views/Homebrew/HomebrewList.swift b/Peel/Views/Homebrew/HomebrewList.swift
index 465ea6ce..e966a81d 100644
--- a/Peel/Views/Homebrew/HomebrewList.swift
+++ b/Peel/Views/Homebrew/HomebrewList.swift
@@ -16,6 +16,14 @@ struct HomebrewList: View {
@State private var casksToForget: [HomebrewPackage]?
var body: some View {
+ // Reading what a command or a Forget changed dims the list as a Rescan does, on the same timing.
+ BusyShown(isBusy: homebrew.isReadingWhatChanged) { isReading in
+ page(isReading: isReading)
+ }
+ }
+
+ @ViewBuilder
+ private func page(isReading: Bool) -> some View {
@Bindable var homebrew = homebrew
let shown = listed
let byKind = Dictionary(grouping: shown, by: \.kind)
@@ -55,7 +63,7 @@ struct HomebrewList: View {
}
.accessibilityLabel(Text(Tool.homebrew.title))
.scrollBarBelowSectionHeaders()
- .scanState(phase(shown), isRescanning: isRescanning, scan: homebrew.scanRun) { placeholder(shown) }
+ .scanState(phase(shown), isRescanning: isRescanning || isReading, scan: homebrew.scanRun) { placeholder(shown) }
// A `List` inserts a new section in one frame, with no animation, so the rows below it jump. To soften
// that, the column fades in when a health or vulnerability report appears or is cleared.
.fadesInColumn(on: [homebrew.findings == nil, homebrew.advisories == nil])
@@ -490,12 +498,12 @@ struct HomebrewList: View {
SectionHeaderLine {
heading("Updates Available", "Homebrew has a newer version of these. A pinned package is held on purpose, so it is listed and left alone.")
} actions: {
- BusyShown(isBusy: homebrew.runningCommand == .upgradeAll) { isShown in
+ BusyShown(isBusy: homebrew.commandHomebrewRuns == .upgradeAll) { isShown in
if isShown {
ProgressView().controlSize(.small).transition(.opacity)
}
}
- .motion(value: homebrew.runningCommand == .upgradeAll)
+ .motion(value: homebrew.commandHomebrewRuns == .upgradeAll)
Button { isConfirmingUpgradeAll = true } label: {
Text("Upgrade All")
.minimumTarget()
@@ -537,12 +545,12 @@ struct HomebrewList: View {
.font(.caption)
.foregroundStyle(.secondary)
} actions: {
- BusyShown(isBusy: homebrew.isForgetting) { isShown in
+ BusyShown(isBusy: !homebrew.casksBeingForgotten.isEmpty) { isShown in
if isShown {
ProgressView().controlSize(.small).transition(.opacity)
}
}
- .motion(value: homebrew.isForgetting)
+ .motion(value: homebrew.casksBeingForgotten.isEmpty)
Button { casksToForget = casks } label: {
Text("Forget All")
.minimumTarget()
@@ -626,12 +634,12 @@ struct HomebrewList: View {
Label { Text(title) } icon: { Image(systemName: systemImage) }
InfoNote(name: String(localized: title), detail: detail)
Spacer(minLength: 8)
- BusyShown(isBusy: homebrew.runningCommand == command) { isShown in
+ BusyShown(isBusy: homebrew.commandHomebrewRuns == command) { isShown in
if isShown {
ProgressView().controlSize(.small).transition(.opacity)
}
}
- .motion(value: homebrew.runningCommand == command)
+ .motion(value: homebrew.commandHomebrewRuns == command)
// Only the controls are disabled while Homebrew is busy, so the info note can still be opened.
trailing()
.buttonStyle(.bordered)
diff --git a/PeelTests/HomebrewLibraryTests.swift b/PeelTests/HomebrewLibraryTests.swift
index d78556b5..6b3036d9 100644
--- a/PeelTests/HomebrewLibraryTests.swift
+++ b/PeelTests/HomebrewLibraryTests.swift
@@ -1,4 +1,5 @@
import Foundation
+import Observation
@testable import Peel
import PeelCore
import Testing
@@ -16,4 +17,65 @@ import Testing
#expect(await homebrew.readAgain(ifHomebrewCheckedAt: (checked ?? .now) + 1))
#expect(homebrew.revision == read + 1)
}
+
+ @Test func homebrewsOwnWorkEndsBeforePeelReadsWhatItChanged() async {
+ guard Homebrew.executableURL != nil, await Homebrew.hasLocalDefinitions() else { return }
+ // Nothing is read yet, so Upgrade All finds nothing to upgrade and runs no `brew upgrade`.
+ let homebrew = HomebrewLibrary()
+
+ let seen = await shownWhenTheReadingStarts(of: homebrew) { await homebrew.run(.upgradeAll, showsResult: false) }
+
+ #expect(seen?.progress == nil)
+ #expect(seen?.command == nil)
+ #expect(seen?.isReading == true)
+ #expect(seen?.isBusy == true)
+ #expect(homebrew.runningCommand == nil && !homebrew.isReadingWhatChanged && homebrew.progress == nil)
+ }
+
+ @Test func forgetsMovesEndBeforePeelReadsWhatChanged() async {
+ guard Homebrew.executableURL != nil, await Homebrew.hasLocalDefinitions() else { return }
+ let homebrew = HomebrewLibrary()
+ // A made-up cask has no folder in the Caskroom, so nothing moves.
+ let cask = HomebrewPackage(name: "org-example-gone", kind: .cask, installedVersion: "1.0")
+
+ let seen = await shownWhenTheReadingStarts(of: homebrew) { _ = await homebrew.forget([cask]) { _ in } }
+
+ #expect(seen?.forgetting.isEmpty == true)
+ #expect(seen?.isReading == true)
+ #expect(seen?.isBusy == true)
+ #expect(!homebrew.isForgetting && !homebrew.isReadingWhatChanged)
+ }
+
+ private struct Shown {
+ let progress: String?
+ let command: HomebrewLibrary.Command?
+ let forgetting: Set<HomebrewPackage.ID>
+ let isReading: Bool
+ let isBusy: Bool
+ }
+
+ @MainActor private final class Seen {
+ var shown: Shown?
+ }
+
+ /// What `homebrew` shows at the moment its reading starts while `work` runs.
+ private func shownWhenTheReadingStarts(
+ of homebrew: HomebrewLibrary,
+ during work: () async -> Void
+ ) async -> Shown? {
+ let seen = Seen()
+ withObservationTracking {
+ _ = homebrew.isScanning
+ } onChange: {
+ MainActor.assumeIsolated {
+ seen.shown = Shown(
+ progress: homebrew.progress, command: homebrew.commandHomebrewRuns,
+ forgetting: homebrew.casksBeingForgotten, isReading: homebrew.isReadingWhatChanged,
+ isBusy: homebrew.runningCommand != nil || homebrew.isForgetting
+ )
+ }
+ }
+ await work()
+ return seen.shown
+ }
}