Skip to content

Commit

Permalink
Fix console markup escaping issue (#1866)
Browse files Browse the repository at this point in the history
* fix for markup escaping in progress

* whitespace

* restore new line

* Update _main.py

Co-authored-by: Tom Christie <tom@tomchristie.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
  • Loading branch information
3 people committed Feb 4, 2022
1 parent 2146c75 commit 2814fd3
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions httpx/_main.py
Expand Up @@ -8,8 +8,10 @@
import pygments.lexers
import pygments.util
import rich.console
import rich.markup
import rich.progress
import rich.syntax
import rich.table

from ._client import Client
from ._exceptions import RequestError
Expand Down Expand Up @@ -173,7 +175,7 @@ def print_response(response: Response) -> None:
syntax = rich.syntax.Syntax(text, lexer_name, theme="ansi_dark", word_wrap=True)
console.print(syntax)
else: # pragma: nocover
console.print(response.text)
console.print(rich.markup.escape(response.text))


def format_certificate(cert: dict) -> str: # pragma: nocover
Expand Down Expand Up @@ -233,20 +235,21 @@ def trace(name: str, info: dict, verbose: bool = False) -> None:

def download_response(response: Response, download: typing.BinaryIO) -> None:
console = rich.console.Console()
syntax = rich.syntax.Syntax("", "http", theme="ansi_dark", word_wrap=True)
console.print(syntax)

console.print()
content_length = response.headers.get("Content-Length")
kwargs = {"total": int(content_length)} if content_length else {}
with rich.progress.Progress(
"[progress.description]{task.description}",
"[progress.percentage]{task.percentage:>3.0f}%",
rich.progress.BarColumn(bar_width=None),
rich.progress.DownloadColumn(),
rich.progress.TransferSpeedColumn(),
) as progress:
description = f"Downloading [bold]{download.name}"
download_task = progress.add_task(description, **kwargs) # type: ignore
description = f"Downloading [bold]{rich.markup.escape(download.name)}"
download_task = progress.add_task(
description,
total=int(content_length or 0),
start=content_length is not None,
)
for chunk in response.iter_bytes():
download.write(chunk)
progress.update(download_task, completed=response.num_bytes_downloaded)
Expand Down

0 comments on commit 2814fd3

Please sign in to comment.