mirror of
https://github.com/Textualize/rich.git
synced 2025-08-04 10:08:40 +00:00
Add an example showing how to copy a file with a progress bar
This commit is contained in:
parent
5cfa2039c1
commit
1432457bf1
1 changed files with 39 additions and 0 deletions
39
examples/cp_progress.py
Normal file
39
examples/cp_progress.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
"""
|
||||
A very minimal `cp` clone that displays a progress bar.
|
||||
"""
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
|
||||
from rich.progress import (
|
||||
BarColumn,
|
||||
DownloadColumn,
|
||||
Progress,
|
||||
TaskID,
|
||||
TextColumn,
|
||||
TimeRemainingColumn,
|
||||
TransferSpeedColumn,
|
||||
)
|
||||
|
||||
progress = Progress(
|
||||
TextColumn("[bold blue]{task.description}", justify="right"),
|
||||
BarColumn(bar_width=None),
|
||||
"[progress.percentage]{task.percentage:>3.1f}%",
|
||||
"•",
|
||||
DownloadColumn(),
|
||||
"•",
|
||||
TransferSpeedColumn(),
|
||||
"•",
|
||||
TimeRemainingColumn(),
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) == 3:
|
||||
|
||||
with progress:
|
||||
desc=os.path.basename(sys.argv[1])
|
||||
with progress.read(sys.argv[1], description=desc) as src:
|
||||
with open(sys.argv[2], "wb") as dst:
|
||||
shutil.copyfileobj(src, dst)
|
||||
else:
|
||||
print("Usage:\n\tpython cp_progress.py SRC DST")
|
Loading…
Add table
Add a link
Reference in a new issue