mirror of
https://github.com/python/cpython.git
synced 2025-09-13 04:08:37 +00:00
gh-131507: Add support for syntax highlighting in PyREPL (GH-133247)
Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
This commit is contained in:
parent
bfcbb28223
commit
fac41f56d4
21 changed files with 654 additions and 99 deletions
|
@ -7,7 +7,22 @@ COLORIZE = True
|
|||
|
||||
# types
|
||||
if False:
|
||||
from typing import IO
|
||||
from typing import IO, Literal
|
||||
|
||||
type ColorTag = Literal[
|
||||
"PROMPT",
|
||||
"KEYWORD",
|
||||
"BUILTIN",
|
||||
"COMMENT",
|
||||
"STRING",
|
||||
"NUMBER",
|
||||
"OP",
|
||||
"DEFINITION",
|
||||
"SOFT_KEYWORD",
|
||||
"RESET",
|
||||
]
|
||||
|
||||
theme: dict[ColorTag, str]
|
||||
|
||||
|
||||
class ANSIColors:
|
||||
|
@ -23,6 +38,7 @@ class ANSIColors:
|
|||
WHITE = "\x1b[37m" # more like LIGHT GRAY
|
||||
YELLOW = "\x1b[33m"
|
||||
|
||||
BOLD = "\x1b[1m"
|
||||
BOLD_BLACK = "\x1b[1;30m" # DARK GRAY
|
||||
BOLD_BLUE = "\x1b[1;34m"
|
||||
BOLD_CYAN = "\x1b[1;36m"
|
||||
|
@ -120,3 +136,28 @@ def can_colorize(*, file: IO[str] | IO[bytes] | None = None) -> bool:
|
|||
return os.isatty(file.fileno())
|
||||
except io.UnsupportedOperation:
|
||||
return hasattr(file, "isatty") and file.isatty()
|
||||
|
||||
|
||||
def set_theme(t: dict[ColorTag, str] | None = None) -> None:
|
||||
global theme
|
||||
|
||||
if t:
|
||||
theme = t
|
||||
return
|
||||
|
||||
colors = get_colors()
|
||||
theme = {
|
||||
"PROMPT": colors.BOLD_MAGENTA,
|
||||
"KEYWORD": colors.BOLD_BLUE,
|
||||
"BUILTIN": colors.CYAN,
|
||||
"COMMENT": colors.RED,
|
||||
"STRING": colors.GREEN,
|
||||
"NUMBER": colors.YELLOW,
|
||||
"OP": colors.RESET,
|
||||
"DEFINITION": colors.BOLD,
|
||||
"SOFT_KEYWORD": colors.BOLD_BLUE,
|
||||
"RESET": colors.RESET,
|
||||
}
|
||||
|
||||
|
||||
set_theme()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue