Skip to content

Commit 1cafa16

Browse files
hamdanalichard26
authored andcommitted
Fix ansi codes on legacy windows console
1 parent d49ec29 commit 1cafa16

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/pip/_internal/cli/parser.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import textwrap
1010
from collections.abc import Generator
1111
from contextlib import suppress
12-
from typing import Any, NoReturn
12+
from typing import IO, Any, Dict, List, NoReturn, Optional
1313

14-
from pip._vendor.rich.console import Console, RenderableType
14+
from pip._vendor.rich.console import Console, RenderableType, detect_legacy_windows
1515
from pip._vendor.rich.markup import escape
1616
from pip._vendor.rich.style import StyleType
1717
from pip._vendor.rich.text import Text
@@ -244,6 +244,22 @@ def option_list_all(self) -> list[optparse.Option]:
244244

245245
return res
246246

247+
def _print_ansi(self, text: str, file: Optional[IO[str]] = None) -> None:
248+
if file is None:
249+
file = sys.stdout
250+
if detect_legacy_windows():
251+
console = Console(file=file)
252+
console.print(Text.from_ansi(text), soft_wrap=True)
253+
else:
254+
file.write(text)
255+
256+
def print_usage(self, file: Optional[IO[str]] = None) -> None:
257+
if self.usage:
258+
self._print_ansi(self.get_usage(), file=file)
259+
260+
def print_help(self, file: Optional[IO[str]] = None) -> None:
261+
self._print_ansi(self.format_help(), file=file)
262+
247263

248264
class ConfigOptionParser(CustomOptionParser):
249265
"""Custom option parser which updates its defaults by checking the

0 commit comments

Comments
 (0)