add chalk

This commit is contained in:
Yusur 2025-09-19 15:39:44 +02:00
parent a2fdc9166f
commit 83ab616e13
6 changed files with 102 additions and 6 deletions

View file

@ -2,8 +2,9 @@
## 0.7.0 "The Lucky Update" ## 0.7.0 "The Lucky Update"
+ Add RNG/random selection overloads such as `luck()`, `rng_overload()`. + Add RNG/random selection overloads such as `luck()`, `rng_overload()`
+ Add 7 new throwable exceptions. + Add 7 new throwable exceptions
+ Add color utilities: `chalk` module
## 0.6.1 ## 0.6.1

View file

@ -35,6 +35,7 @@ from .strtools import PrefixIdentifier
from .validators import matches from .validators import matches
from .redact import redact_url_password from .redact import redact_url_password
from .http import WantsContentType from .http import WantsContentType
from .color import chalk
__version__ = "0.7.0-dev37" __version__ = "0.7.0-dev37"
@ -46,7 +47,7 @@ __all__ = (
'StringCase', 'TimedDict', 'TomlI18n', 'UserSigner', 'Wanted', 'WantsContentType', 'StringCase', 'TimedDict', 'TomlI18n', 'UserSigner', 'Wanted', 'WantsContentType',
'addattr', 'additem', 'age_and_days', 'alru_cache', 'b2048decode', 'b2048encode', 'addattr', 'additem', 'age_and_days', 'alru_cache', 'b2048decode', 'b2048encode',
'b32ldecode', 'b32lencode', 'b64encode', 'b64decode', 'cb32encode', 'b32ldecode', 'b32lencode', 'b64encode', 'b64decode', 'cb32encode',
'cb32decode', 'count_ones', 'dei_args', 'deprecated', 'ilex', 'join_bits', 'cb32decode', 'chalk', 'count_ones', 'dei_args', 'deprecated', 'ilex', 'join_bits',
'jsonencode', 'kwargs_prefix', 'lex', 'ltuple', 'makelist', 'mask_shift', 'jsonencode', 'kwargs_prefix', 'lex', 'ltuple', 'makelist', 'mask_shift',
'matches', 'mod_ceil', 'mod_floor', 'none_pass', 'not_implemented', 'matches', 'mod_ceil', 'mod_floor', 'none_pass', 'not_implemented',
'redact_url_password', 'rtuple', 'split_bits', 'ssv_list', 'symbol_table', 'redact_url_password', 'rtuple', 'split_bits', 'ssv_list', 'symbol_table',

View file

@ -1,5 +1,17 @@
""" """
ASGI stuff
---
Copyright (c) 2025 Sakuragasaki46.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
See LICENSE for the specific language governing permissions and
limitations under the License.
This software is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
""" """
from typing import Any, Awaitable, Callable, MutableMapping, ParamSpec, Protocol from typing import Any, Awaitable, Callable, MutableMapping, ParamSpec, Protocol

79
src/suou/color.py Normal file
View file

@ -0,0 +1,79 @@
"""
Colors for coding artists
NEW 0.7.0
---
Copyright (c) 2025 Sakuragasaki46.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
See LICENSE for the specific language governing permissions and
limitations under the License.
This software is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
"""
from functools import lru_cache
class Chalk:
"""
ANSI escape codes for terminal colors, similar to JavaScript's `chalk` library.
Best used with Python 3.12+ that allows arbitrary nesting of f-strings.
Yes, I am aware colorama exists.
UNTESTED
NEW 0.7.0
"""
CSI = '\x1b['
RED = CSI + "31m"
GREEN = CSI + "32m"
YELLOW = CSI + "33m"
BLUE = CSI + "34m"
CYAN = CSI + "36m"
PURPLE = CSI + "35m"
GREY = CSI + "90m"
END_COLOR = CSI + "39m"
BOLD = CSI + "1m"
END_BOLD = CSI + "22m"
FAINT = CSI + "2m"
def __init__(self, flags = (), ends = ()):
self._flags = tuple(flags)
self._ends = tuple(ends)
@lru_cache()
def _wrap(self, beg, end):
return Chalk(self._flags + (beg,), self._ends + (end,))
def __call__(self, s: str) -> str:
return ''.join(self._flags) + s + ''.join(reversed(self._ends))
def red(self):
return self._wrap(self.RED, self.END_COLOR)
def green(self):
return self._wrap(self.GREEN, self.END_COLOR)
def blue(self):
return self._wrap(self.BLUE, self.END_COLOR)
def yellow(self):
return self._wrap(self.YELLOW, self.END_COLOR)
def cyan(self):
return self._wrap(self.CYAN, self.END_COLOR)
def purple(self):
return self._wrap(self.PURPLE, self.END_COLOR)
def grey(self):
return self._wrap(self.GREY, self.END_COLOR)
gray = grey
marine = blue
def bold(self):
return self._wrap(self.BOLD, self.END_BOLD)
def faint(self):
return self._wrap(self.FAINT, self.END_BOLD)
## TODO make it lazy?
chalk = Chalk()

View file

@ -1,5 +1,7 @@
""" """
Fortune and esoterism helpers. Fortune' RNG and esoterism.
NEW 0.7.0
--- ---
@ -108,5 +110,5 @@ def rng_overload(prev_func: RngCallable[_T, _U] | int | None, /, *, weight: int
return decorator return decorator
# This module is experimental and therefore not re-exported into __init__
__all__ = ('lucky', 'rng_overload')

View file

@ -54,4 +54,5 @@ def ko(status: int, /, content = None, **ka):
return PlainTextResponse(content, status_code=status, **ka) return PlainTextResponse(content, status_code=status, **ka)
return content return content
# This module is experimental and therefore not re-exported into __init__
__all__ = ('ko', 'ok', 'Waiter') __all__ = ('ko', 'ok', 'Waiter')