Skip to content

Commit 5682219

Browse files
committed
Enumerate all ANSI colors, improve typing
1 parent a96eea4 commit 5682219

1 file changed

Lines changed: 52 additions & 9 deletions

File tree

Lib/_colorize.py

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,64 @@
1+
from __future__ import annotations
12
import io
23
import os
34
import sys
45

56
COLORIZE = True
67

8+
# types
9+
if False:
10+
from typing import IO
11+
712

813
class ANSIColors:
9-
BACKGROUND_YELLOW = "\x1b[43m"
10-
BOLD_GREEN = "\x1b[1;32m"
11-
BOLD_MAGENTA = "\x1b[1;35m"
12-
BOLD_RED = "\x1b[1;31m"
14+
RESET = "\x1b[0m"
15+
1316
BLACK = "\x1b[30m"
17+
BLUE = "\x1b[34m"
18+
CYAN = "\x1b[36m"
1419
GREEN = "\x1b[32m"
15-
GREY = "\x1b[90m"
1620
MAGENTA = "\x1b[35m"
1721
RED = "\x1b[31m"
18-
RESET = "\x1b[0m"
22+
WHITE = "\x1b[37m" # more like LIGHT GRAY
1923
YELLOW = "\x1b[33m"
2024

25+
BOLD_BLACK = "\x1b[1;30m" # DARK GRAY
26+
BOLD_BLUE = "\x1b[1;34m"
27+
BOLD_CYAN = "\x1b[1;36m"
28+
BOLD_GREEN = "\x1b[1;32m"
29+
BOLD_MAGENTA = "\x1b[1;35m"
30+
BOLD_RED = "\x1b[1;31m"
31+
BOLD_WHITE = "\x1b[1;37m" # actual WHITE
32+
BOLD_YELLOW = "\x1b[1;33m"
33+
34+
# intense = like bold but without being bold
35+
INTENSE_BLACK = "\x1b[90m"
36+
INTENSE_BLUE = "\x1b[94m"
37+
INTENSE_CYAN = "\x1b[96m"
38+
INTENSE_GREEN = "\x1b[92m"
39+
INTENSE_MAGENTA = "\x1b[95m"
40+
INTENSE_RED = "\x1b[91m"
41+
INTENSE_WHITE = "\x1b[97m"
42+
INTENSE_YELLOW = "\x1b[93m"
43+
44+
BACKGROUND_BLACK = "\x1b[40m"
45+
BACKGROUND_BLUE = "\x1b[44m"
46+
BACKGROUND_CYAN = "\x1b[46m"
47+
BACKGROUND_GREEN = "\x1b[42m"
48+
BACKGROUND_MAGENTA = "\x1b[45m"
49+
BACKGROUND_RED = "\x1b[41m"
50+
BACKGROUND_WHITE = "\x1b[47m"
51+
BACKGROUND_YELLOW = "\x1b[43m"
52+
53+
INTENSE_BACKGROUND_BLACK = "\x1b[100m"
54+
INTENSE_BACKGROUND_BLUE = "\x1b[104m"
55+
INTENSE_BACKGROUND_CYAN = "\x1b[106m"
56+
INTENSE_BACKGROUND_GREEN = "\x1b[102m"
57+
INTENSE_BACKGROUND_MAGENTA = "\x1b[105m"
58+
INTENSE_BACKGROUND_RED = "\x1b[101m"
59+
INTENSE_BACKGROUND_WHITE = "\x1b[107m"
60+
INTENSE_BACKGROUND_YELLOW = "\x1b[103m"
61+
2162

2263
NoColors = ANSIColors()
2364

@@ -26,14 +67,16 @@ class ANSIColors:
2667
setattr(NoColors, attr, "")
2768

2869

29-
def get_colors(colorize: bool = False, *, file=None) -> ANSIColors:
70+
def get_colors(
71+
colorize: bool = False, *, file: IO[str] | IO[bytes] | None = None
72+
) -> ANSIColors:
3073
if colorize or can_colorize(file=file):
3174
return ANSIColors()
3275
else:
3376
return NoColors
3477

3578

36-
def can_colorize(*, file=None) -> bool:
79+
def can_colorize(*, file: IO[str] | IO[bytes] | None = None) -> bool:
3780
if file is None:
3881
file = sys.stdout
3982

@@ -66,4 +109,4 @@ def can_colorize(*, file=None) -> bool:
66109
try:
67110
return os.isatty(file.fileno())
68111
except io.UnsupportedOperation:
69-
return file.isatty()
112+
return hasattr(file, "isatty") and file.isatty()

0 commit comments

Comments
 (0)