Skip to content

Commit ce50388

Browse files
committed
Add dependency-free minimal console color functions.
1 parent a67f6e4 commit ce50388

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

openlcb/__init__.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from collections import OrderedDict
88
from typing import (
9+
Any,
910
List, # in case list doesn't support `[` in this Python version
1011
Union, # in case `|` doesn't support 'type' in this Python version
1112
)
@@ -173,3 +174,39 @@ def d_quote(value) -> str:
173174
attribute debug messages or any other technical/literary use.
174175
"""
175176
return hr_repr(value, always_quote=True)
177+
178+
179+
def prBold(text: Any):
180+
"""Print in bold
181+
(or if not available, more intense if available).
182+
See echoC for details"""
183+
# echoC(message, "1:37:40")
184+
ansiP(text, 1)
185+
186+
187+
def prDim(text: Any):
188+
"""Show dim text in console.
189+
(resets to 00 [default colors] for text after prDim).
190+
Note: 0;30;40 is non-bold dim, but that's invisible in cmd.exe.
191+
"""
192+
ansiP(text, 2)
193+
194+
195+
def ansiP(text: Any, ansiColor: Union[int, str]):
196+
"""Print color text to consoles that support ANSI color codes.
197+
Args:
198+
message (Any): Any message (will be cast to str).
199+
ansiColor: Numerical or multi-number (str) ansi
200+
color code (semi-colon separated--same codes but allows
201+
multiple codes in a row). See
202+
<https://en.wikipedia.org/wiki/ANSI_escape_code> for a full
203+
list. Examples:
204+
- "1;30;40" for dim (NOTE: 0;30;40 is gray, but is invisible
205+
in cmd.exe)
206+
- "96": Cyan
207+
"""
208+
# - `\033[` starts the ANSI escape sequence.
209+
# - 91m, 92m, ... etc., are color codes for different foreground colors.
210+
# - \033[00m resets the text style so the terminal returns to normal
211+
# formatting after each print.
212+
print(f"\033[{ansiColor}m {str(text)}\033[00m")

0 commit comments

Comments
 (0)