Skip to content
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
34 changes: 19 additions & 15 deletions Sources/TerminalProgress/ProgressBar+Terminal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,32 @@ import Foundation
enum EscapeSequence {
static let hideCursor = "\u{001B}[?25l"
static let showCursor = "\u{001B}[?25h"
static let clearLine = "\u{001B}[2K"
static let moveUp = "\u{001B}[1A"
}

extension ProgressBar {
static var terminalWidth: Int {
guard
let termimalHandle = ProgressBar.term,
let terminal = try? Terminal(descriptor: termimalHandle.fileDescriptor)
else {
return 0
}

let terminalWidth = (try? Int(terminal.size.width)) ?? 0
return terminalWidth
}

/// Clears the progress bar and resets the cursor.
static public func clearAndResetCursor() {
ProgressBar.clear()
public func clearAndResetCursor() {
clear()
ProgressBar.resetCursor()
}

/// Clears the progress bar.
static public func clear() {
ProgressBar.display(EscapeSequence.clearLine)
public func clear() {
// We can't use "\u{001B}[2K" for clearing the line because this may lead to a race with `stdout` when using `stderr` for progress updates.
displayText("")
}

/// Resets the cursor.
Expand All @@ -59,24 +71,16 @@ extension ProgressBar {
}

func displayText(_ text: String, terminating: String = "\r") {
guard
let termimalHandle = ProgressBar.term,
let terminal = try? Terminal(descriptor: termimalHandle.fileDescriptor)
else {
return
}

var text = text

// Clears previously printed characters if the new string is shorter.
text += String(repeating: " ", count: max(state.output.count - text.count, 0))
state.output = text

// Clears previously printed lines.
let terminalWidth = (try? Int(terminal.size.width)) ?? 0
var lines = ""
if terminalWidth > 0 {
let lineCount = (text.count - 1) / terminalWidth
if ProgressBar.terminalWidth > 0 {
let lineCount = (text.count - 1) / ProgressBar.terminalWidth
for _ in 0..<lineCount {
lines += EscapeSequence.moveUp
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/TerminalProgress/ProgressBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class ProgressBar: Sendable {
}

deinit {
ProgressBar.clear()
clear()
}

/// Allows resetting the progress state.
Expand All @@ -48,7 +48,7 @@ public final class ProgressBar: Sendable {

/// Allows resetting the progress state of the current task.
public func resetCurrentTask() {
ProgressBar.clear()
clear()
state = State(description: state.description, itemsName: state.itemsName, tasks: state.tasks, totalTasks: state.totalTasks, startTime: state.startTime)
}

Expand Down Expand Up @@ -125,7 +125,7 @@ public final class ProgressBar: Sendable {
}

if config.clearOnFinish {
ProgressBar.clearAndResetCursor()
clearAndResetCursor()
} else {
ProgressBar.resetCursor()
}
Expand Down