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.

Keep the Homebrew page smooth while an upgrade writes its output

Țugui Dragoș519eb1aa18a8
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/OutputTail.swift b/Packages/PeelCore/Sources/PeelCore/Support/OutputTail.swift
new file mode 100644
index 00000000..e7ed31cc
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Support/OutputTail.swift
@@ -0,0 +1,21 @@
+public import Foundation
+
+/// The end of a tool's output, as a page shows it while the tool runs.
+public enum OutputTail {
+ /// The last `count` lines of `output`, read back from its end, so the cost is the tail's however long the
+ /// output has grown.
+ public static func lastLines(of output: Data, count: Int) -> String {
+ let newline = UInt8(ascii: "\n")
+ var start = output.endIndex
+ var found = 0
+ while start > output.startIndex {
+ let before = output.index(before: start)
+ if output[before] == newline {
+ found += 1
+ if found == count { break }
+ }
+ start = before
+ }
+ return String(decoding: output[start...], as: UTF8.self)
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OutputTailTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OutputTailTests.swift
new file mode 100644
index 00000000..776f616f
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/OutputTailTests.swift
@@ -0,0 +1,24 @@
+import Foundation
+@testable import PeelCore
+import Testing
+
+struct OutputTailTests {
+ @Test func keepsOnlyTheLastLines() {
+ let output = Data((1...10).map { "line \($0)" }.joined(separator: "\n").utf8)
+
+ #expect(OutputTail.lastLines(of: output, count: 3) == "line 8\nline 9\nline 10")
+ #expect(OutputTail.lastLines(of: output, count: 20) == String(decoding: output, as: UTF8.self))
+ #expect(OutputTail.lastLines(of: Data(), count: 3) == "")
+ }
+
+ /// The cost of a tail is the tail's, whatever came before, so a long upgrade stays as cheap to show as a short one.
+ @Test func readsOnlyTheEndOfALongOutput() {
+ let line = Data("==> Pouring a package\n".utf8)
+ var output = Data()
+ for _ in 0..<200_000 { output.append(line) }
+
+ let clock = ContinuousClock()
+ let took = clock.measure { _ = OutputTail.lastLines(of: output, count: 400) }
+ #expect(took < .milliseconds(5), "\(took)")
+ }
+}
diff --git a/Peel/Model/HomebrewLibrary.swift b/Peel/Model/HomebrewLibrary.swift
index 9bf99cc2..0421c044 100644
--- a/Peel/Model/HomebrewLibrary.swift
+++ b/Peel/Model/HomebrewLibrary.swift
@@ -107,7 +107,7 @@ final class HomebrewLibrary {
let scanRun = ScanRun()
var isScanning: Bool { scanRun.isRunning }
private(set) var runningCommand: Command?
- /// What Homebrew has written so far in the running upgrade. Nil when no upgrade runs.
+ /// 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.
private(set) var isStopping = false
@@ -378,12 +378,19 @@ final class HomebrewLibrary {
) async -> CommandResult {
progress = ""
let (pieces, sink) = AsyncStream.makeStream(of: Data.self)
+ // The page shows the last lines, at most ten times a second: a long upgrade writes thousands of lines, and
+ // showing all of it on every piece would cost more with each piece.
let shown = Task {
var written = Data()
+ var shownAt = ContinuousClock.now - .seconds(1)
for await piece in pieces {
written.append(piece)
- progress = String(decoding: written, as: UTF8.self)
+ guard ContinuousClock.now - shownAt >= .milliseconds(100) else { continue }
+ progress = OutputTail.lastLines(of: written, count: 400)
+ shownAt = .now
}
+ progress = OutputTail.lastLines(of: written, count: 400)
+ return String(decoding: written, as: UTF8.self)
}
let outcome: Result<String, Homebrew.CommandFailure>
do {
@@ -392,8 +399,7 @@ final class HomebrewLibrary {
outcome = .failure(error)
}
sink.finish()
- await shown.value
- let written = progress ?? ""
+ let written = await shown.value
switch outcome {
case .success(let output):
return CommandResult(succeeded: true, output: written.isEmpty ? output : written)
diff --git a/Peel/Views/Homebrew/HomebrewProgressView.swift b/Peel/Views/Homebrew/HomebrewProgressView.swift
index e576b4b4..c5c671f7 100644
--- a/Peel/Views/Homebrew/HomebrewProgressView.swift
+++ b/Peel/Views/Homebrew/HomebrewProgressView.swift
@@ -18,7 +18,7 @@ struct HomebrewProgressView: View {
.disabled(homebrew.isStopping)
}
ScrollView {
- Text(verbatim: lastLines)
+ Text(verbatim: homebrew.progress ?? "")
.font(.system(.callout, design: .monospaced))
.textSelection(.enabled)
.frame(maxWidth: .infinity, alignment: .leading)
@@ -34,8 +34,4 @@ struct HomebrewProgressView: View {
}
}
- /// The last lines only, so a long build costs no more to draw than a screenful. The result shows them all.
- private var lastLines: String {
- (homebrew.progress ?? "").split(separator: "\n", omittingEmptySubsequences: false).suffix(400).joined(separator: "\n")
- }
}