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.

A long Move to Trash shows how far it got

Țugui Dragoșaa04faa1f6dd
diff --git a/Localization/Peel/Localizable.xcstrings b/Localization/Peel/Localizable.xcstrings
index 3abdbb0b..a198739f 100644
--- a/Localization/Peel/Localizable.xcstrings
+++ b/Localization/Peel/Localizable.xcstrings
@@ -61829,6 +61829,118 @@
}
}
},
+ "Moved %@ of %@" : {
+ "localizations" : {
+ "cs" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Přesunuto %@ z %@"
+ }
+ },
+ "de" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%@ von %@ verschoben"
+ }
+ },
+ "en" : {
+ "stringUnit" : {
+ "state" : "new",
+ "value" : "Moved %1$@ of %2$@"
+ }
+ },
+ "es" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%@ de %@ a la papelera"
+ }
+ },
+ "fr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%@ sur %@ dans la corbeille"
+ }
+ },
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Spostati %@ di %@"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "移動済み: %@ / %@"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%@/%@ 이동됨"
+ }
+ },
+ "nl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%@ van %@ verplaatst"
+ }
+ },
+ "pl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Przeniesiono %@ z %@"
+ }
+ },
+ "pt" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%@ de %@ para o Lixo"
+ }
+ },
+ "ro" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Mutat %@ din %@"
+ }
+ },
+ "ru" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Перемещено %@ из %@"
+ }
+ },
+ "sv" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Flyttat %@ av %@"
+ }
+ },
+ "tr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Taşındı: %@ / %@"
+ }
+ },
+ "uk" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Переміщено %@ із %@"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "已移走%@/%@"
+ }
+ },
+ "zh-Hant" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "已移動%@/%@"
+ }
+ }
+ }
+ },
"Moved ^[%lld item](inflect: true) to the Trash, %@." : {
"localizations" : {
"cs" : {
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index f9058953..a9277f8c 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -307,6 +307,7 @@ public struct TrashService: Sendable {
}
result.trashed.append(item)
moved.append(path)
+ MoveCount.current?.add(1)
} catch {
result.failures.append(TrashFailure(url: url, reason: Self.reason(for: error)))
}
@@ -353,6 +354,7 @@ public struct TrashService: Sendable {
for batch in [allowed.filter { !links.contains($0) }, allowed.filter(links.contains)] where !batch.isEmpty {
let moved = await moveThroughHelper(batch)
journal?.note(moved.trashed, batch: removal)
+ MoveCount.current?.add(moved.trashed.count)
helperResult.trashed += moved.trashed
helperResult.failures += moved.failures
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/MoveCount.swift b/Packages/PeelCore/Sources/PeelCore/Support/MoveCount.swift
new file mode 100644
index 00000000..d6cb9207
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Support/MoveCount.swift
@@ -0,0 +1,17 @@
+import Synchronization
+
+/// How many items a removal has moved so far, so a long one can say how far it got.
+public final class MoveCount: Sendable {
+ private let moved = Atomic<Int>(0)
+
+ public init() {}
+
+ public var value: Int { moved.load(ordering: .relaxed) }
+
+ func add(_ count: Int) {
+ moved.wrappingAdd(count, ordering: .relaxed)
+ }
+
+ /// The count that moves in the current task add to, set by whoever runs the removal.
+ @TaskLocal public static var current: MoveCount?
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
index a6861cb1..2fe91e81 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
@@ -122,6 +122,23 @@ struct TrashServiceTests {
#expect(result.failures.isEmpty, "the item inside the folder was moved again: \(result.failures)")
}
+ /// A long removal says how far it got: every item that moved counts once, and one that stays counts nothing.
+ @Test func countsWhatMovedForWhoeverWatches() async throws {
+ let directory = try TemporaryDirectory()
+ let first = try directory.directory("home/Library/Caches/org.example.one")
+ let second = try directory.directory("home/Library/Caches/org.example.two")
+ let keychain = try directory.file("home/Library/Keychains/login.keychain-db")
+ let count = MoveCount()
+
+ let service = try service(in: directory)
+ let result = await MoveCount.$current.withValue(count) {
+ await service.trash([first, second, keychain])
+ }
+
+ #expect(result.trashed.count == 2)
+ #expect(count.value == 2)
+ }
+
/// A job stopped before a move that then fails is a job left stopped with its file still in place.
@Test func stopsAJobOnlyOnceItsFileHasMoved() async throws {
let directory = try TemporaryDirectory()
diff --git a/Peel/Model/SelectionCarrier.swift b/Peel/Model/SelectionCarrier.swift
index 09fbd569..e2be674a 100644
--- a/Peel/Model/SelectionCarrier.swift
+++ b/Peel/Model/SelectionCarrier.swift
@@ -44,6 +44,9 @@ final class SelectionCarrier {
private var carried = CarriedSelection()
/// True while a Move to Trash moves the parts, one after another.
private(set) var isMoving = false
+ /// How many of `toMove` items have moved, brought up to date ten times a second while a move runs.
+ private(set) var movedSoFar = 0
+ private(set) var toMove = 0
@ObservationIgnored private let tools: [Tool: any CarriesSelection]
@ObservationIgnored private let apps: AppLibrary
@ObservationIgnored private let history: RemovalHistoryStore
@@ -87,17 +90,31 @@ final class SelectionCarrier {
/// Peel stops halfway, and a part whose tool kept it stays selected, with the app to quit named after.
func move(_ parts: [CarriedSelection.Part]) async {
isMoving = true
- defer { isMoving = false }
+ movedSoFar = 0
+ toMove = parts.reduce(0) { $0 + $1.count }
+ let count = MoveCount()
+ let watching = Task { [weak self] in
+ while !Task.isCancelled {
+ try? await Task.sleep(for: .milliseconds(100))
+ self?.movedSoFar = count.value
+ }
+ }
+ defer {
+ watching.cancel()
+ isMoving = false
+ }
var removal = RemovalInProgress()
var appsToQuit: [String] = []
- let pass = await QuitGuard.shared.run {
- await CarriedSelection.pass(parts) { part in
- guard let tool = tool(of: part), let result = await tool.move(part, apps: apps) else {
- appsToQuit.append(tool(of: part)?.appToQuit(for: part) ?? part.title)
- return nil
+ let pass = await MoveCount.$current.withValue(count) {
+ await QuitGuard.shared.run {
+ await CarriedSelection.pass(parts) { part in
+ guard let tool = tool(of: part), let result = await tool.move(part, apps: apps) else {
+ appsToQuit.append(tool(of: part)?.appToQuit(for: part) ?? part.title)
+ return nil
+ }
+ await history.record(result, part: part.removalPart, sizes: part.measuredSizes, in: &removal)
+ return result
}
- await history.record(result, part: part.removalPart, sizes: part.measuredSizes, in: &removal)
- return result
}
}
history.finish(removal)
diff --git a/Peel/Views/RemovalBar.swift b/Peel/Views/RemovalBar.swift
index 05213b7e..64ab0e0b 100644
--- a/Peel/Views/RemovalBar.swift
+++ b/Peel/Views/RemovalBar.swift
@@ -91,55 +91,66 @@ struct RemovalBar: View {
var body: some View {
let reading = reading
let isEnabled = reading.isEnabled && exclusions.exclusions.isKnown && !history.isUnreadable
- FloatingBar {
- if let notice {
- Text(verbatim: notice.figure)
- .font(.barFigure)
- .monospacedDigit()
- Text(notice.words)
- .foregroundStyle(.secondary)
- } else if let moved {
- Text(moved.size.known > 0 ? moved.size.text : String(inflecting: "^[\(moved.records.count) item](inflect: true)"))
- .font(.barFigure)
- .monospacedDigit()
- Text("Moved", comment: "A label after a figure at the foot of a page, for a moment after a removal: a size, such as 303 kB, or a count, such as 3 items. Write it so it reads right after any figure, never as a form that agrees with the number or adds (s).")
- .foregroundStyle(.secondary)
- } else if isScanning {
- ProgressView()
- .controlSize(.small)
- ReadSoFar(scan: scan)
- .foregroundStyle(.secondary)
- } else if let pages = reading.pages, let page {
- // The pages go under the figure rather than beside it, so the capsule stays as wide as with the
- // selection of one page, which fits beside the button in every language at the narrowest column.
- Button {
- isShowingPages = true
- } label: {
- VStack(alignment: .leading, spacing: 0) {
- figure(reading.total)
- HStack(spacing: 4) {
- Text("Selected on ^[\(pages) page](inflect: true)", comment: "Under the figure in the bar at the foot of a page, when the selection is on several pages: the figure is a size, such as 426 kB. Word Selected as the bar's own \"Selected\" is worded, agreeing with no number but the pages'.")
- Image(systemName: "chevron.up")
- .font(.caption2.weight(.semibold))
- .accessibilityHidden(true)
- }
- .font(.subheadline)
+ BusyShown(isBusy: page != nil && carrier.isMoving) { isShowingMove in
+ FloatingBar {
+ if let notice {
+ Text(verbatim: notice.figure)
+ .font(.barFigure)
+ .monospacedDigit()
+ Text(notice.words)
+ .foregroundStyle(.secondary)
+ } else if let moved {
+ Text(moved.size.known > 0
+ ? moved.size.text
+ : String(inflecting: "^[\(moved.records.count) item](inflect: true)"))
+ .font(.barFigure)
+ .monospacedDigit()
+ Text("Moved", comment: "A label after a figure at the foot of a page, for a moment after a removal: a size, such as 303 kB, or a count, such as 3 items. Write it so it reads right after any figure, never as a form that agrees with the number or adds (s).")
+ .foregroundStyle(.secondary)
+ } else if isScanning {
+ ProgressView()
+ .controlSize(.small)
+ ReadSoFar(scan: scan)
+ .foregroundStyle(.secondary)
+ } else if isShowingMove {
+ ProgressView()
+ .controlSize(.small)
+ Text("Moved \(carrier.movedSoFar, format: .number) of \(carrier.toMove, format: .number)")
+ .monospacedDigit()
.foregroundStyle(.secondary)
+ } else if let pages = reading.pages, let page {
+ // The pages go under the figure rather than beside it, so the capsule stays as wide as with the
+ // selection of one page, which fits beside the button in every language at the narrowest column.
+ Button {
+ isShowingPages = true
+ } label: {
+ VStack(alignment: .leading, spacing: 0) {
+ figure(reading.total)
+ HStack(spacing: 4) {
+ Text("Selected on ^[\(pages) page](inflect: true)", comment: "Under the figure in the bar at the foot of a page, when the selection is on several pages: the figure is a size, such as 426 kB. Word Selected as the bar's own \"Selected\" is worded, agreeing with no number but the pages'.")
+ Image(systemName: "chevron.up")
+ .font(.caption2.weight(.semibold))
+ .accessibilityHidden(true)
+ }
+ .font(.subheadline)
+ .foregroundStyle(.secondary)
+ }
+ .contentShape(.rect)
}
- .contentShape(.rect)
- }
- .buttonStyle(.plain)
- .popover(isPresented: $isShowingPages, arrowEdge: .top) {
- CarriedPartsList(page: page)
+ .buttonStyle(.plain)
+ .popover(isPresented: $isShowingPages, arrowEdge: .top) {
+ CarriedPartsList(page: page)
+ }
+ } else {
+ figure(reading.total)
+ Text("Selected", comment: "A label after a figure at the foot of a page: a size, such as 303 kB, or a count, such as 3 items. Write it so it reads right after any figure, as \"en la selección\" does, never as a form that agrees with the number or adds (s).")
+ .foregroundStyle(.secondary)
}
- } else {
- figure(reading.total)
- Text("Selected", comment: "A label after a figure at the foot of a page: a size, such as 303 kB, or a count, such as 3 items. Write it so it reads right after any figure, as \"en la selección\" does, never as a form that agrees with the number or adds (s).")
- .foregroundStyle(.secondary)
+ } action: {
+ Button(String(localized: title), systemImage: systemImage, action: reading.remove)
+ .disabled(!isEnabled)
}
- } action: {
- Button(String(localized: title), systemImage: systemImage, action: reading.remove)
- .disabled(!isEnabled)
+ .motion(value: isShowingMove)
}
.focusedSceneValue(\.moveToTrash, MenuCommand(title: title, isEnabled: isEnabled, perform: reading.remove))
.motion(value: reading.total)