This commit is contained in:
Joey Ballentine 2023-11-29 11:51:18 -07:00 committed by GitHub
commit 1dbcf322db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 5 deletions

1
news/11171.feature.rst Normal file
View File

@ -0,0 +1 @@
Add ability to force progress bars to be used, for allowing progress bars to be shown in subprocesses.

View File

@ -226,9 +226,12 @@ progress_bar: Callable[..., Option] = partial(
"--progress-bar",
dest="progress_bar",
type="choice",
choices=["on", "off"],
choices=["on", "off", "forced"],
default="on",
help="Specify whether the progress bar should be used [on, off] (default: on)",
help=(
"Specify whether the progress bar should be used [on, off, forced]"
" (default: on)"
),
)
log: Callable[..., Option] = partial(

View File

@ -1,6 +1,7 @@
import functools
from typing import Callable, Generator, Iterable, Iterator, Optional, Tuple
from pip._vendor.rich.console import Console
from pip._vendor.rich.progress import (
BarColumn,
DownloadColumn,
@ -25,7 +26,9 @@ def _rich_progress_bar(
bar_type: str,
size: int,
) -> Generator[bytes, None, None]:
assert bar_type == "on", "This should only be used in the default mode."
assert (
bar_type == "on" or bar_type == "forced"
), "This should only be used in the default mode or if forced."
if not size:
total = float("inf")
@ -47,7 +50,13 @@ def _rich_progress_bar(
TimeRemainingColumn(),
)
progress = Progress(*columns, refresh_per_second=30)
progress = Progress(
*columns,
refresh_per_second=30,
console=Console(
force_terminal=bar_type == "forced",
),
)
task_id = progress.add_task(" " * (get_indentation() + 2), total=total)
with progress:
for chunk in iterable:
@ -62,7 +71,7 @@ def get_download_progress_renderer(
Returns a callable, that takes an iterable to "wrap".
"""
if bar_type == "on":
if bar_type == "on" or bar_type == "forced":
return functools.partial(_rich_progress_bar, bar_type=bar_type, size=size)
else:
return iter # no-op, when passed an iterator