2021-03-25 21:08:36 +00:00
|
|
|
from rich.console import Console
|
2022-01-13 16:44:26 +00:00
|
|
|
from rich.progress_bar import ProgressBar, _get_pulse_segments
|
2020-06-28 10:10:59 +00:00
|
|
|
from rich.segment import Segment
|
|
|
|
from rich.style import Style
|
2020-04-12 11:48:00 +00:00
|
|
|
|
2020-07-16 16:24:16 +00:00
|
|
|
from .render import render
|
2020-04-12 11:48:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_init():
|
2020-10-16 09:56:08 +00:00
|
|
|
bar = ProgressBar(completed=50)
|
2020-04-12 11:48:00 +00:00
|
|
|
repr(bar)
|
|
|
|
assert bar.percentage_completed == 50.0
|
|
|
|
|
|
|
|
|
|
|
|
def test_update():
|
2020-10-16 09:56:08 +00:00
|
|
|
bar = ProgressBar()
|
2020-04-12 11:48:00 +00:00
|
|
|
assert bar.completed == 0
|
|
|
|
assert bar.total == 100
|
|
|
|
bar.update(10, 20)
|
|
|
|
assert bar.completed == 10
|
|
|
|
assert bar.total == 20
|
|
|
|
assert bar.percentage_completed == 50
|
|
|
|
bar.update(100)
|
|
|
|
assert bar.percentage_completed == 100
|
|
|
|
|
|
|
|
|
|
|
|
expected = [
|
2020-05-24 10:52:33 +00:00
|
|
|
"\x1b[38;2;249;38;114m━━━━━\x1b[0m\x1b[38;2;249;38;114m╸\x1b[0m\x1b[38;5;237m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m",
|
|
|
|
"\x1b[38;2;249;38;114m━━━━━━\x1b[0m\x1b[38;5;237m╺\x1b[0m\x1b[38;5;237m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m",
|
2020-04-12 11:48:00 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_render():
|
2020-10-16 09:56:08 +00:00
|
|
|
bar = ProgressBar(completed=11, width=50)
|
2020-04-12 11:48:00 +00:00
|
|
|
bar_render = render(bar)
|
|
|
|
assert bar_render == expected[0]
|
|
|
|
bar.update(completed=12)
|
|
|
|
bar_render = render(bar)
|
|
|
|
assert bar_render == expected[1]
|
|
|
|
|
|
|
|
|
|
|
|
def test_measure():
|
2021-03-25 21:08:36 +00:00
|
|
|
console = Console(width=120)
|
2020-10-16 09:56:08 +00:00
|
|
|
bar = ProgressBar()
|
2021-03-25 21:08:36 +00:00
|
|
|
measurement = bar.__rich_measure__(console, console.options)
|
2020-04-12 11:48:00 +00:00
|
|
|
assert measurement.minimum == 4
|
|
|
|
assert measurement.maximum == 120
|
|
|
|
|
|
|
|
|
2020-05-19 12:38:13 +00:00
|
|
|
def test_zero_total():
|
|
|
|
# Shouldn't throw zero division error
|
2020-10-16 09:56:08 +00:00
|
|
|
bar = ProgressBar(total=0)
|
2020-05-19 12:38:13 +00:00
|
|
|
render(bar)
|
|
|
|
|
|
|
|
|
2020-06-13 13:48:53 +00:00
|
|
|
def test_pulse():
|
2020-10-16 09:56:08 +00:00
|
|
|
bar = ProgressBar(pulse=True, animation_time=10)
|
2020-06-13 13:48:53 +00:00
|
|
|
bar_render = render(bar)
|
|
|
|
print(repr(bar_render))
|
|
|
|
expected = "\x1b[38;2;249;38;114m━\x1b[0m\x1b[38;2;244;38;112m━\x1b[0m\x1b[38;2;230;39;108m━\x1b[0m\x1b[38;2;209;42;102m━\x1b[0m\x1b[38;2;183;44;94m━\x1b[0m\x1b[38;2;153;48;86m━\x1b[0m\x1b[38;2;123;51;77m━\x1b[0m\x1b[38;2;97;53;69m━\x1b[0m\x1b[38;2;76;56;63m━\x1b[0m\x1b[38;2;62;57;59m━\x1b[0m\x1b[38;2;58;58;58m━\x1b[0m\x1b[38;2;62;57;59m━\x1b[0m\x1b[38;2;76;56;63m━\x1b[0m\x1b[38;2;97;53;69m━\x1b[0m\x1b[38;2;123;51;77m━\x1b[0m\x1b[38;2;153;48;86m━\x1b[0m\x1b[38;2;183;44;94m━\x1b[0m\x1b[38;2;209;42;102m━\x1b[0m\x1b[38;2;230;39;108m━\x1b[0m\x1b[38;2;244;38;112m━\x1b[0m\x1b[38;2;249;38;114m━\x1b[0m\x1b[38;2;244;38;112m━\x1b[0m\x1b[38;2;230;39;108m━\x1b[0m\x1b[38;2;209;42;102m━\x1b[0m\x1b[38;2;183;44;94m━\x1b[0m\x1b[38;2;153;48;86m━\x1b[0m\x1b[38;2;123;51;77m━\x1b[0m\x1b[38;2;97;53;69m━\x1b[0m\x1b[38;2;76;56;63m━\x1b[0m\x1b[38;2;62;57;59m━\x1b[0m\x1b[38;2;58;58;58m━\x1b[0m\x1b[38;2;62;57;59m━\x1b[0m\x1b[38;2;76;56;63m━\x1b[0m\x1b[38;2;97;53;69m━\x1b[0m\x1b[38;2;123;51;77m━\x1b[0m\x1b[38;2;153;48;86m━\x1b[0m\x1b[38;2;183;44;94m━\x1b[0m\x1b[38;2;209;42;102m━\x1b[0m\x1b[38;2;230;39;108m━\x1b[0m\x1b[38;2;244;38;112m━\x1b[0m\x1b[38;2;249;38;114m━\x1b[0m\x1b[38;2;244;38;112m━\x1b[0m\x1b[38;2;230;39;108m━\x1b[0m\x1b[38;2;209;42;102m━\x1b[0m\x1b[38;2;183;44;94m━\x1b[0m\x1b[38;2;153;48;86m━\x1b[0m\x1b[38;2;123;51;77m━\x1b[0m\x1b[38;2;97;53;69m━\x1b[0m\x1b[38;2;76;56;63m━\x1b[0m\x1b[38;2;62;57;59m━\x1b[0m\x1b[38;2;58;58;58m━\x1b[0m\x1b[38;2;62;57;59m━\x1b[0m\x1b[38;2;76;56;63m━\x1b[0m\x1b[38;2;97;53;69m━\x1b[0m\x1b[38;2;123;51;77m━\x1b[0m\x1b[38;2;153;48;86m━\x1b[0m\x1b[38;2;183;44;94m━\x1b[0m\x1b[38;2;209;42;102m━\x1b[0m\x1b[38;2;230;39;108m━\x1b[0m\x1b[38;2;244;38;112m━\x1b[0m\x1b[38;2;249;38;114m━\x1b[0m\x1b[38;2;244;38;112m━\x1b[0m\x1b[38;2;230;39;108m━\x1b[0m\x1b[38;2;209;42;102m━\x1b[0m\x1b[38;2;183;44;94m━\x1b[0m\x1b[38;2;153;48;86m━\x1b[0m\x1b[38;2;123;51;77m━\x1b[0m\x1b[38;2;97;53;69m━\x1b[0m\x1b[38;2;76;56;63m━\x1b[0m\x1b[38;2;62;57;59m━\x1b[0m\x1b[38;2;58;58;58m━\x1b[0m\x1b[38;2;62;57;59m━\x1b[0m\x1b[38;2;76;56;63m━\x1b[0m\x1b[38;2;97;53;69m━\x1b[0m\x1b[38;2;123;51;77m━\x1b[0m\x1b[38;2;153;48;86m━\x1b[0m\x1b[38;2;183;44;94m━\x1b[0m\x1b[38;2;209;42;102m━\x1b[0m\x1b[38;2;230;39;108m━\x1b[0m\x1b[38;2;244;38;112m━\x1b[0m\x1b[38;2;249;38;114m━\x1b[0m\x1b[38;2;244;38;112m━\x1b[0m\x1b[38;2;230;39;108m━\x1b[0m\x1b[38;2;209;42;102m━\x1b[0m\x1b[38;2;183;44;94m━\x1b[0m\x1b[38;2;153;48;86m━\x1b[0m\x1b[38;2;123;51;77m━\x1b[0m\x1b[38;2;97;53;69m━\x1b[0m\x1b[38;2;76;56;63m━\x1b[0m\x1b[38;2;62;57;59m━\x1b[0m\x1b[38;2;58;58;58m━\x1b[0m\x1b[38;2;62;57;59m━\x1b[0m\x1b[38;2;76;56;63m━\x1b[0m\x1b[38;2;97;53;69m━\x1b[0m\x1b[38;2;123;51;77m━\x1b[0m\x1b[38;2;153;48;86m━\x1b[0m\x1b[38;2;183;44;94m━\x1b[0m\x1b[38;2;209;42;102m━\x1b[0m\x1b[38;2;230;39;108m━\x1b[0m\x1b[38;2;244;38;112m━\x1b[0m"
|
|
|
|
assert bar_render == expected
|
|
|
|
|
|
|
|
|
2020-06-28 10:10:59 +00:00
|
|
|
def test_get_pulse_segments():
|
2020-10-16 09:56:08 +00:00
|
|
|
bar = ProgressBar()
|
2022-01-13 16:44:26 +00:00
|
|
|
segments = _get_pulse_segments(
|
2021-01-11 21:14:14 +00:00
|
|
|
Style.parse("red"), Style.parse("yellow"), None, False, False
|
2020-06-28 10:10:59 +00:00
|
|
|
)
|
|
|
|
print(repr(segments))
|
|
|
|
expected = [
|
2021-03-14 11:47:03 +00:00
|
|
|
Segment("━", Style.parse("red")),
|
|
|
|
Segment("━", Style.parse("red")),
|
|
|
|
Segment("━", Style.parse("red")),
|
|
|
|
Segment("━", Style.parse("red")),
|
|
|
|
Segment("━", Style.parse("red")),
|
|
|
|
Segment("━", Style.parse("red")),
|
|
|
|
Segment("━", Style.parse("red")),
|
|
|
|
Segment("━", Style.parse("red")),
|
|
|
|
Segment("━", Style.parse("red")),
|
|
|
|
Segment("━", Style.parse("red")),
|
|
|
|
Segment("━", Style.parse("yellow")),
|
|
|
|
Segment("━", Style.parse("yellow")),
|
|
|
|
Segment("━", Style.parse("yellow")),
|
|
|
|
Segment("━", Style.parse("yellow")),
|
|
|
|
Segment("━", Style.parse("yellow")),
|
|
|
|
Segment("━", Style.parse("yellow")),
|
|
|
|
Segment("━", Style.parse("yellow")),
|
|
|
|
Segment("━", Style.parse("yellow")),
|
|
|
|
Segment("━", Style.parse("yellow")),
|
|
|
|
Segment("━", Style.parse("yellow")),
|
2020-06-28 10:10:59 +00:00
|
|
|
]
|
|
|
|
assert segments == expected
|
|
|
|
|
|
|
|
|
2020-04-12 11:48:00 +00:00
|
|
|
if __name__ == "__main__":
|
2020-10-16 09:56:08 +00:00
|
|
|
bar = ProgressBar(completed=11, width=50)
|
2020-04-12 11:48:00 +00:00
|
|
|
bar_render = render(bar)
|
|
|
|
print(repr(bar_render))
|
|
|
|
bar.update(completed=12)
|
|
|
|
bar_render = render(bar)
|
|
|
|
print(repr(bar_render))
|