projects
/
python_utils.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Keep requirements up-to-date.
[python_utils.git]
/
ansi.py
diff --git
a/ansi.py
b/ansi.py
index 6897291ba252f91c866b967cd18e0aa13f0f151d..03f8fd27473c4e295cdafbb9e7122b4cec296453 100755
(executable)
--- a/
ansi.py
+++ b/
ansi.py
@@
-1,16
+1,22
@@
#!/usr/bin/env python3
#!/usr/bin/env python3
-from abc import abstractmethod
+"""A bunch of color names mapped into RGB tuples and some methods for
+setting the text color, background, etc... using ANSI escape
+sequences."""
+
import difflib
import io
import logging
import re
import sys
import difflib
import io
import logging
import re
import sys
+from abc import abstractmethod
from typing import Any, Callable, Dict, Iterable, Optional, Tuple
from overrides import overrides
import logging_utils
from typing import Any, Callable, Dict, Iterable, Optional, Tuple
from overrides import overrides
import logging_utils
+import string_utils
+
logger = logging.getLogger(__name__)
logger = logging.getLogger(__name__)
@@
-1641,7
+1647,7
@@
def strike_through() -> str:
def is_16color(num: int) -> bool:
def is_16color(num: int) -> bool:
- return num
== 255 or num == 128
+ return num
in (255, 128)
def is_216color(num: int) -> bool:
def is_216color(num: int) -> bool:
@@
-1751,8
+1757,6
@@
def fg(
b'G1szODs1OzIxbQ==\\n'
"""
b'G1szODs1OzIxbQ==\\n'
"""
- import string_utils
-
if name is not None and name == 'reset':
return '\033[39m'
if name is not None and name == 'reset':
return '\033[39m'
@@
-1776,9
+1780,7
@@
def fg(
if (is_16color(red) and is_16color(green) and is_16color(blue)) or force_16color:
logger.debug("Using 16-color strategy")
return fg_16color(red, green, blue)
if (is_16color(red) and is_16color(green) and is_16color(blue)) or force_16color:
logger.debug("Using 16-color strategy")
return fg_16color(red, green, blue)
- if (
- is_216color(red) and is_216color(green) and is_216color(blue)
- ) or force_216color:
+ if (is_216color(red) and is_216color(green) and is_216color(blue)) or force_216color:
logger.debug("Using 216-color strategy")
return fg_216color(red, green, blue)
logger.debug("Using 24-bit color strategy")
logger.debug("Using 216-color strategy")
return fg_216color(red, green, blue)
logger.debug("Using 24-bit color strategy")
@@
-1812,8
+1814,6
@@
def pick_contrasting_color(
(0, 0, 0)
"""
(0, 0, 0)
"""
- import string_utils
-
if name is not None and string_utils.is_full_string(name):
rgb = _find_color_by_name(name)
else:
if name is not None and string_utils.is_full_string(name):
rgb = _find_color_by_name(name)
else:
@@
-1834,10
+1834,11
@@
def guess_name(name: str) -> str:
max_ratio = r
best_guess = possibility
assert best_guess is not None
max_ratio = r
best_guess = possibility
assert best_guess is not None
- logger.debug(
f"Best guess at color name is {best_guess}"
)
+ logger.debug(
"Best guess at color name is %s", best_guess
)
return best_guess
return best_guess
+@logging_utils.squelch_repeated_log_messages(1)
def bg(
name: Optional[str] = "",
red: Optional[int] = None,
def bg(
name: Optional[str] = "",
red: Optional[int] = None,
@@
-1855,8
+1856,6
@@
def bg(
b'G1s0ODs1OzE5Nm0=\\n'
"""
b'G1s0ODs1OzE5Nm0=\\n'
"""
- import string_utils
-
if name is not None and name == 'reset':
return '\033[49m'
if name is not None and name == 'reset':
return '\033[49m'
@@
-1879,9
+1878,7
@@
def bg(
if (is_16color(red) and is_16color(green) and is_16color(blue)) or force_16color:
logger.debug("Using 16-color strategy")
return bg_16color(red, green, blue)
if (is_16color(red) and is_16color(green) and is_16color(blue)) or force_16color:
logger.debug("Using 16-color strategy")
return bg_16color(red, green, blue)
- if (
- is_216color(red) and is_216color(green) and is_216color(blue)
- ) or force_216color:
+ if (is_216color(red) and is_216color(green) and is_216color(blue)) or force_216color:
logger.debug("Using 216-color strategy")
return bg_216color(red, green, blue)
logger.debug("Using 24-bit color strategy")
logger.debug("Using 216-color strategy")
return bg_216color(red, green, blue)
logger.debug("Using 24-bit color strategy")
@@
-1889,7
+1886,11
@@
def bg(
class StdoutInterceptor(io.TextIOBase):
class StdoutInterceptor(io.TextIOBase):
+ """An interceptor for data written to stdout. Use as a context.
+
+ """
def __init__(self):
def __init__(self):
+ super().__init__()
self.saved_stdout: io.TextIO = None
self.buf = ''
self.saved_stdout: io.TextIO = None
self.buf = ''
@@
-1909,12
+1910,16
@@
class StdoutInterceptor(io.TextIOBase):
class ProgrammableColorizer(StdoutInterceptor):
class ProgrammableColorizer(StdoutInterceptor):
+ """A colorizing interceptor; pass it re.Patterns -> methods that do
+ something (usually add color to) the match.
+
+ """
def __init__(
self,
patterns: Iterable[Tuple[re.Pattern, Callable[[Any, re.Pattern], str]]],
):
super().__init__()
def __init__(
self,
patterns: Iterable[Tuple[re.Pattern, Callable[[Any, re.Pattern], str]]],
):
super().__init__()
- self.patterns =
[_ for _ in patterns]
+ self.patterns =
list(patterns)
@overrides
def write(self, s: str):
@overrides
def write(self, s: str):
@@
-1924,7
+1929,6
@@
class ProgrammableColorizer(StdoutInterceptor):
if __name__ == '__main__':
if __name__ == '__main__':
-
def main() -> None:
import doctest
def main() -> None:
import doctest
@@
-1938,9
+1942,6
@@
if __name__ == '__main__':
_ = pick_contrasting_color(possibility)
xf = fg(None, _[0], _[1], _[2])
xb = bg(None, _[0], _[1], _[2])
_ = pick_contrasting_color(possibility)
xf = fg(None, _[0], _[1], _[2])
xb = bg(None, _[0], _[1], _[2])
- print(
- f'{f}{xb}{possibility}{reset()}\t\t\t'
- f'{b}{xf}{possibility}{reset()}'
- )
+ print(f'{f}{xb}{possibility}{reset()}\t\t\t' f'{b}{xf}{possibility}{reset()}')
main()
main()