Skip to content

Commit 9595fce

Browse files
Update terminal width when progress animation is updated (#463)
Without updating the terminal width for each progress update, a progress line may be printed as more than one line, so line clearing will not work as expected and many lines will be printed when overflowing max columns of line. Ninja also checks the terminal width every line print: https://github.com/ninja-build/ninja/blob/fd7067652cae480190bf13b2ee5475efdf09ac7d/src/line_printer.cc#L110 This particularly improves the build and test progress report of SwiftPM. **Before** https://github.com/apple/swift-tools-support-core/assets/11702759/7c32c2d5-0469-479b-a542-6ffd0656610a **After** https://github.com/apple/swift-tools-support-core/assets/11702759/71e6ce59-f888-4a19-819a-3012c9ea378f
1 parent 397343f commit 9595fce

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

Sources/TSCBasic/TerminalController.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,14 @@ public final class TerminalController {
6464
private var stream: WritableByteStream
6565

6666
/// Width of the terminal.
67-
public let width: Int
67+
public var width: Int {
68+
// Determine the terminal width otherwise assume a default.
69+
if let terminalWidth = TerminalController.terminalWidth(), terminalWidth > 0 {
70+
return terminalWidth
71+
} else {
72+
return 80
73+
}
74+
}
6875

6976
/// Code to clear the line on a tty.
7077
private let clearLineString = "\u{001B}[2K"
@@ -84,13 +91,6 @@ public final class TerminalController {
8491
return nil
8592
}
8693

87-
// Determine the terminal width otherwise assume a default.
88-
if let terminalWidth = TerminalController.terminalWidth(), terminalWidth > 0 {
89-
width = terminalWidth
90-
} else {
91-
width = 80
92-
}
93-
9494
#if os(Windows)
9595
// Enable VT100 interpretation
9696
let hOut = GetStdHandle(STD_OUTPUT_HANDLE)

Sources/TSCUtility/ProgressAnimation.swift

+8-6
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,10 @@ public final class RedrawingNinjaProgressAnimation: ProgressAnimationProtocol {
117117
terminal.clearLine()
118118

119119
let progressText = "[\(step)/\(total)] \(text)"
120-
if progressText.utf8.count > terminal.width {
120+
let width = terminal.width
121+
if progressText.utf8.count > width {
121122
let suffix = ""
122-
terminal.write(String(progressText.prefix(terminal.width - suffix.utf8.count)))
123+
terminal.write(String(progressText.prefix(width - suffix.utf8.count)))
123124
terminal.write(suffix)
124125
} else {
125126
terminal.write(progressText)
@@ -211,8 +212,9 @@ public final class RedrawingLitProgressAnimation: ProgressAnimationProtocol {
211212
public func update(step: Int, total: Int, text: String) {
212213
assert(step <= total)
213214

215+
let width = terminal.width
214216
if !hasDisplayedHeader {
215-
let spaceCount = terminal.width / 2 - header.utf8.count / 2
217+
let spaceCount = width / 2 - header.utf8.count / 2
216218
terminal.write(repeating(string: " ", count: spaceCount))
217219
terminal.write(header, inColor: .cyan, bold: true)
218220
terminal.endLine()
@@ -225,18 +227,18 @@ public final class RedrawingLitProgressAnimation: ProgressAnimationProtocol {
225227
let prefix = "\(paddedPercentage)% " + terminal.wrap("[", inColor: .green, bold: true)
226228
terminal.write(prefix)
227229

228-
let barWidth = terminal.width - prefix.utf8.count
230+
let barWidth = width - prefix.utf8.count
229231
let n = Int(Double(barWidth) * Double(percentage) / 100.0)
230232

231233
terminal.write(repeating(string: "=", count: n) + repeating(string: "-", count: barWidth - n), inColor: .green)
232234
terminal.write("]", inColor: .green, bold: true)
233235
terminal.endLine()
234236

235237
terminal.clearLine()
236-
if text.utf8.count > terminal.width {
238+
if text.utf8.count > width {
237239
let prefix = ""
238240
terminal.write(prefix)
239-
terminal.write(String(text.suffix(terminal.width - prefix.utf8.count)))
241+
terminal.write(String(text.suffix(width - prefix.utf8.count)))
240242
} else {
241243
terminal.write(text)
242244
}

0 commit comments

Comments
 (0)