This is Swift port of the well known and awesome Python package tqdm.
It allows you to add a fancy progress bar to your loops with minimal effort.
It has common features of the original package.
Installation
You can add this package as a dependency of your product with the code snippet below in Package.swift of your project:
You can wrap your Sequence and iterate over TqdmSequence.
This way, you will be printing progress bar while iterating. See example below.
import Tqdm
var sum = 0
let N = 1000
// Iterate over sequence and print progress bar
for i in TqdmSequence(sequence: 0..<N) {
sum += i
}
Updating manually
If you need to update progress bar with different increments or under some conditions,
you can use update(n: Int) method of the Tqdm object. You can also print messages using
write(message: String) method. See example below.
import Tqdm
let N = 500
let tqdm = Tqdm(description: "swift-tqdm", total: N, color: .red)
for i in 0..<N {
Thread.sleep(forTimeInterval: 0.005) // Simulate work
// Update bar
tqdm.update() // Increment iteration by 1
// tqdm.update(n: 3) // Increment iteration by 3
if i == (N / 2) {
// Print without overlap with the bar
tqdm.write("Half of the job is done")
}
}
tqdm.close()
You can observe TqdmExample executable to see other options.
Parameters
Both Tqdm and TqdmSequence has other parameters which let you control behavior of the progress bar.
Most of the parameters are same, except Tqdm accepts total: Int? while TqdmSequence accepts sequence : S.
sequence¹:Sequence to be wrapped and iterated over
description: Progress bar description before actual bar
total²: The number of expected iterations. If unspecified only basic progress
statistics are displayed (no ETA, no progressbar).
swift-tqdm
This is Swift port of the well known and awesome Python package
tqdm
. It allows you to add a fancy progress bar to your loops with minimal effort. It has common features of the original package.Installation
You can add this package as a dependency of your product with the code snippet below in
Package.swift
of your project:Usage
The package allows 2 usage scenarios:
Wrapping a
Sequence
You can wrap your
Sequence
and iterate overTqdmSequence
. This way, you will be printing progress bar while iterating. See example below.Updating manually
If you need to update progress bar with different increments or under some conditions, you can use
update(n: Int)
method of theTqdm
object. You can also print messages usingwrite(message: String)
method. See example below.You can observe
TqdmExample
executable to see other options.Parameters
Both
Tqdm
andTqdmSequence
has other parameters which let you control behavior of the progress bar. Most of the parameters are same, exceptTqdm
acceptstotal: Int?
whileTqdmSequence
acceptssequence : S
.Sequence
to be wrapped and iterated over0.1
) seconds." 123456789#"
"it"
)0.3
).0
)1000
), ignored unlessunitScale
istrue
1: Only in
TqdmSequence
2: Only in
Tqdm
Contributions
All source code is hosted on GitHub. Contributions are welcome. To contribute code, fork the repository and open a pull request.
To report bugs, propose features, or raise miscellaneous issues, create an issue in the issue tracker.
License
Code is licensed under MIT License.