]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Fix console markup escaping issue (#1866)
authorWill McGugan <willmcgugan@gmail.com>
Fri, 4 Feb 2022 12:33:32 +0000 (12:33 +0000)
committerGitHub <noreply@github.com>
Fri, 4 Feb 2022 12:33:32 +0000 (12:33 +0000)
* 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>
httpx/_main.py

index 237875a0ae602862890e76d01cb7598f0daeb58b..24ca30ddfc3e2342a3acac8c84b1aa8fdbdfa97c 100644 (file)
@@ -8,8 +8,10 @@ import httpcore
 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
@@ -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
@@ -233,11 +235,8 @@ 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}%",
@@ -245,8 +244,12 @@ def download_response(response: Response, download: typing.BinaryIO) -> 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)