Skip to content

Update terminal width when progress animation is updated #463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Sources/TSCBasic/TerminalController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@ public final class TerminalController {
private var stream: WritableByteStream

/// Width of the terminal.
public let width: Int
public var width: Int {
// Determine the terminal width otherwise assume a default.
if let terminalWidth = TerminalController.terminalWidth(), terminalWidth > 0 {
return terminalWidth
} else {
return 80
}
}

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

// Determine the terminal width otherwise assume a default.
if let terminalWidth = TerminalController.terminalWidth(), terminalWidth > 0 {
width = terminalWidth
} else {
width = 80
}

#if os(Windows)
// Enable VT100 interpretation
let hOut = GetStdHandle(STD_OUTPUT_HANDLE)
Expand Down
14 changes: 8 additions & 6 deletions Sources/TSCUtility/ProgressAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ public final class RedrawingNinjaProgressAnimation: ProgressAnimationProtocol {
terminal.clearLine()

let progressText = "[\(step)/\(total)] \(text)"
if progressText.utf8.count > terminal.width {
let width = terminal.width
if progressText.utf8.count > width {
let suffix = "…"
terminal.write(String(progressText.prefix(terminal.width - suffix.utf8.count)))
terminal.write(String(progressText.prefix(width - suffix.utf8.count)))
terminal.write(suffix)
} else {
terminal.write(progressText)
Expand Down Expand Up @@ -211,8 +212,9 @@ public final class RedrawingLitProgressAnimation: ProgressAnimationProtocol {
public func update(step: Int, total: Int, text: String) {
assert(step <= total)

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

let barWidth = terminal.width - prefix.utf8.count
let barWidth = width - prefix.utf8.count
let n = Int(Double(barWidth) * Double(percentage) / 100.0)

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

terminal.clearLine()
if text.utf8.count > terminal.width {
if text.utf8.count > width {
let prefix = "…"
terminal.write(prefix)
terminal.write(String(text.suffix(terminal.width - prefix.utf8.count)))
terminal.write(String(text.suffix(width - prefix.utf8.count)))
} else {
terminal.write(text)
}
Expand Down