-
Notifications
You must be signed in to change notification settings - Fork 583
Improve accuracy of progress updates #144
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
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b3acb1f
Make progress updates inside a single lock, so their values don't cha…
dkovba 4101809
Display a finished state
dkovba 7ece6da
Switch to the finished state correctly
dkovba 61bccb1
Use the finished property consistently
dkovba 4542b81
Add tests for the finished state
dkovba 4877205
Update comments
dkovba 462ee15
Update formatting
dkovba 8d2641b
Require a newer Containerization
dkovba 4848de2
Merge branch 'main' into progress-updates
dkovba 9439490
Merge branch 'main' into progress-updates
dkovba ef13f74
Finish a progress bar when all other criteria are met when running th…
dkovba 7720f37
Update `Package.resolved`
dkovba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,8 +20,9 @@ import SendableProperty | |
| /// A progress bar that updates itself as tasks are completed. | ||
| public final class ProgressBar: Sendable { | ||
| let config: ProgressConfig | ||
| // `@SendableProperty` adds `_state: Synchronized<State>`, which can be updated inside a lock using `_state.withLock()`. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this property should up in IDEs? Would the user be able to navigate into the def for the property?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| @SendableProperty | ||
| var state: State | ||
| var state = State() | ||
| @SendableProperty | ||
| var printedWidth = 0 | ||
| let term: FileHandle? | ||
|
|
@@ -97,7 +98,7 @@ public final class ProgressBar: Sendable { | |
| printFullDescription() | ||
| } | ||
|
|
||
| while !isFinished { | ||
| while !state.finished { | ||
| let intervalNanoseconds = UInt64(intervalSeconds * 1_000_000_000) | ||
| render() | ||
| state.iteration += 1 | ||
|
|
@@ -117,11 +118,15 @@ public final class ProgressBar: Sendable { | |
|
|
||
| /// Finishes the progress bar. | ||
| public func finish() { | ||
| guard !isFinished else { | ||
| guard !state.finished else { | ||
| return | ||
| } | ||
|
|
||
| state.finished = true | ||
|
|
||
| // The last render. | ||
| render(force: true) | ||
|
|
||
| if !config.disableProgressUpdates && !config.clearOnFinish { | ||
| displayText(state.output, terminating: "\n") | ||
| } | ||
|
|
@@ -143,8 +148,8 @@ extension ProgressBar { | |
| return timeDifferenceSeconds | ||
| } | ||
|
|
||
| func render() { | ||
| guard term != nil && !config.disableProgressUpdates && !isFinished else { | ||
| func render(force: Bool = false) { | ||
| guard term != nil && !config.disableProgressUpdates && (force || !state.finished) else { | ||
| return | ||
| } | ||
| let output = draw() | ||
|
|
@@ -154,8 +159,12 @@ extension ProgressBar { | |
| func draw() -> String { | ||
| var components = [String]() | ||
| if config.showSpinner && !config.showProgressBar { | ||
| let spinnerIcon = config.theme.getSpinnerIcon(state.iteration) | ||
| components.append("\(spinnerIcon)") | ||
| if !state.finished { | ||
| let spinnerIcon = config.theme.getSpinnerIcon(state.iteration) | ||
| components.append("\(spinnerIcon)") | ||
| } else { | ||
| components.append("\(config.theme.done)") | ||
| } | ||
| } | ||
|
|
||
| if config.showTasks, let totalTasks = state.totalTasks { | ||
|
|
@@ -176,13 +185,13 @@ extension ProgressBar { | |
| let total = state.totalSize ?? Int64(state.totalItems ?? 0) | ||
|
|
||
| if config.showPercent && total > 0 && allowProgress { | ||
| components.append("\(state.percent)") | ||
| components.append("\(state.finished ? "100%" : state.percent)") | ||
| } | ||
|
|
||
| if config.showProgressBar, total > 0, allowProgress { | ||
| let usedWidth = components.joined(separator: " ").count + 45 /* the maximum number of characters we may need */ | ||
| let remainingWidth = max(config.width - usedWidth, 1 /* the minumum width of a progress bar */) | ||
| let barLength = Int(Int64(remainingWidth) * value / total) | ||
| let barLength = state.finished ? remainingWidth : Int(Int64(remainingWidth) * value / total) | ||
| let barPaddingLength = remainingWidth - barLength | ||
| let bar = "\(String(repeating: config.theme.bar, count: barLength))\(String(repeating: " ", count: barPaddingLength))" | ||
| components.append("|\(bar)|") | ||
|
|
@@ -195,40 +204,56 @@ extension ProgressBar { | |
| if !state.itemsName.isEmpty { | ||
| itemsName = " \(state.itemsName)" | ||
| } | ||
| if let totalItems = state.totalItems { | ||
| additionalComponents.append("\(state.items.formattedNumber()) of \(totalItems.formattedNumber())\(itemsName)") | ||
| if state.finished { | ||
| if let totalItems = state.totalItems { | ||
| additionalComponents.append("\(totalItems.formattedNumber())\(itemsName)") | ||
| } | ||
| } else { | ||
| additionalComponents.append("\(state.items.formattedNumber())\(itemsName)") | ||
| if let totalItems = state.totalItems { | ||
| additionalComponents.append("\(state.items.formattedNumber()) of \(totalItems.formattedNumber())\(itemsName)") | ||
| } else { | ||
| additionalComponents.append("\(state.items.formattedNumber())\(itemsName)") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if state.size > 0 && allowProgress { | ||
| var formattedCombinedSize = "" | ||
| if config.showSize { | ||
| var formattedSize = state.size.formattedSize() | ||
| formattedSize = adjustFormattedSize(formattedSize) | ||
| if let totalSize = state.totalSize { | ||
| var formattedTotalSize = totalSize.formattedSize() | ||
| formattedTotalSize = adjustFormattedSize(formattedTotalSize) | ||
| formattedCombinedSize = combineSize(size: formattedSize, totalSize: formattedTotalSize) | ||
| } else { | ||
| formattedCombinedSize = formattedSize | ||
| if state.finished { | ||
| if config.showSize { | ||
| if let totalSize = state.totalSize { | ||
| var formattedTotalSize = totalSize.formattedSize() | ||
| formattedTotalSize = adjustFormattedSize(formattedTotalSize) | ||
| additionalComponents.append(formattedTotalSize) | ||
| } | ||
| } | ||
| } else { | ||
| var formattedCombinedSize = "" | ||
| if config.showSize { | ||
| var formattedSize = state.size.formattedSize() | ||
| formattedSize = adjustFormattedSize(formattedSize) | ||
| if let totalSize = state.totalSize { | ||
| var formattedTotalSize = totalSize.formattedSize() | ||
| formattedTotalSize = adjustFormattedSize(formattedTotalSize) | ||
| formattedCombinedSize = combineSize(size: formattedSize, totalSize: formattedTotalSize) | ||
| } else { | ||
| formattedCombinedSize = formattedSize | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var formattedSpeed = "" | ||
| if config.showSpeed { | ||
| formattedSpeed = "\(state.sizeSpeed ?? state.averageSizeSpeed)" | ||
| formattedSpeed = adjustFormattedSize(formattedSpeed) | ||
| } | ||
| var formattedSpeed = "" | ||
| if config.showSpeed { | ||
| formattedSpeed = "\(state.sizeSpeed ?? state.averageSizeSpeed)" | ||
| formattedSpeed = adjustFormattedSize(formattedSpeed) | ||
| } | ||
|
|
||
| if config.showSize && config.showSpeed { | ||
| additionalComponents.append(formattedCombinedSize) | ||
| additionalComponents.append(formattedSpeed) | ||
| } else if config.showSize { | ||
| additionalComponents.append(formattedCombinedSize) | ||
| } else if config.showSpeed { | ||
| additionalComponents.append(formattedSpeed) | ||
| if config.showSize && config.showSpeed { | ||
| additionalComponents.append(formattedCombinedSize) | ||
| additionalComponents.append(formattedSpeed) | ||
| } else if config.showSize { | ||
| additionalComponents.append(formattedCombinedSize) | ||
| } else if config.showSpeed { | ||
| additionalComponents.append(formattedSpeed) | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

Uh oh!
There was an error while loading. Please reload this page.