-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #393 from neutrinoceros/rfc/themes
RFC: refactor theme-handling internals
- Loading branch information
Showing
13 changed files
with
101 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,95 @@ | ||
import sys | ||
import unicodedata | ||
from contextlib import contextmanager | ||
from dataclasses import dataclass | ||
from typing import Literal, TypedDict | ||
|
||
if sys.version_info >= (3, 11): | ||
from typing import assert_never | ||
else: | ||
from typing_extensions import assert_never | ||
__all__ = ["get_symbol", "theme_ctx"] | ||
|
||
|
||
class Theme(TypedDict): | ||
class SymbolSet(TypedDict): | ||
LAUNCH: str | ||
SUCCESS: str | ||
WARNING: str | ||
ERROR: str | ||
HINT: str | ||
|
||
|
||
Default = Theme( | ||
LAUNCH=unicodedata.lookup("ROCKET"), # 🚀 | ||
SUCCESS=unicodedata.lookup("PARTY POPPER"), # 🎉 | ||
WARNING=unicodedata.lookup("HEAVY EXCLAMATION MARK SYMBOL"), # ❗ | ||
ERROR=unicodedata.lookup("COLLISION SYMBOL"), # 💥 | ||
HINT=unicodedata.lookup("LEFT-POINTING MAGNIFYING GLASS"), # 🔍 | ||
@dataclass(frozen=True, slots=True, kw_only=True) | ||
class Theme: | ||
name: str | ||
symbols: SymbolSet | ||
enter_msg: str | None | ||
exit_msg: str | None | ||
|
||
|
||
class ThemeRegistry: | ||
def __init__(self): | ||
self._registry: dict[str, Theme] = {} | ||
|
||
def register( | ||
self, | ||
name: str, | ||
*, | ||
symbols: SymbolSet, | ||
enter: str | None = None, | ||
exit: str | None = None, | ||
): | ||
self._registry[name] = Theme( | ||
name=name, symbols=symbols, enter_msg=enter, exit_msg=exit | ||
) | ||
|
||
def __getitem__(self, item: str) -> Theme: | ||
return self._registry[item] | ||
|
||
|
||
themes = ThemeRegistry() | ||
themes.register( | ||
name="default", | ||
symbols={ | ||
"LAUNCH": unicodedata.lookup("ROCKET"), # 🚀 | ||
"SUCCESS": unicodedata.lookup("PARTY POPPER"), # 🎉 | ||
"WARNING": unicodedata.lookup("HEAVY EXCLAMATION MARK SYMBOL"), # ❗ | ||
"ERROR": unicodedata.lookup("COLLISION SYMBOL"), # 💥 | ||
"HINT": unicodedata.lookup("LEFT-POINTING MAGNIFYING GLASS"), # 🔍 | ||
}, | ||
) | ||
|
||
Baballe = Theme( | ||
LAUNCH=unicodedata.lookup("GUIDE DOG"), # 🦮 | ||
SUCCESS=unicodedata.lookup("POODLE"), # 🐩 | ||
WARNING=unicodedata.lookup("PAW PRINTS"), # 🐾 | ||
ERROR=unicodedata.lookup("HOT DOG"), # 🌭 | ||
HINT=unicodedata.lookup("CRYSTAL BALL"), # 🔮 | ||
themes.register( | ||
name="baballe", | ||
symbols={ | ||
"LAUNCH": unicodedata.lookup("GUIDE DOG"), # 🦮 | ||
"SUCCESS": unicodedata.lookup("POODLE"), # 🐩 | ||
"WARNING": unicodedata.lookup("PAW PRINTS"), # 🐾 | ||
"ERROR": unicodedata.lookup("HOT DOG"), # 🌭 | ||
"HINT": unicodedata.lookup("CRYSTAL BALL"), # 🔮 | ||
}, | ||
enter=unicodedata.lookup("BASEBALL") | ||
+ unicodedata.lookup("BLACK RIGHT-POINTING TRIANGLE"), | ||
exit=unicodedata.lookup("BLACK LEFT-POINTING TRIANGLE") | ||
+ unicodedata.lookup("BASEBALL"), | ||
) | ||
|
||
|
||
THEME = Default | ||
THEME = themes["default"] | ||
|
||
|
||
def get_symbol(key: Literal["LAUNCH", "SUCCESS", "WARNING", "ERROR", "HINT"]) -> str: | ||
return THEME.symbols[key] | ||
|
||
def set_theme(theme: Literal["default", "baballe"]) -> None: | ||
|
||
@contextmanager | ||
def theme_ctx(name: str): | ||
global THEME | ||
if theme == "default": | ||
THEME = Default | ||
elif theme == "baballe": | ||
THEME = Baballe | ||
else: | ||
assert_never(theme) | ||
old_name = THEME.name | ||
THEME = themes[name] | ||
|
||
if THEME.enter_msg is not None: | ||
print(THEME.enter_msg, file=sys.stderr) | ||
try: | ||
yield | ||
finally: | ||
if THEME.exit_msg is not None: | ||
print(THEME.exit_msg, file=sys.stderr) | ||
|
||
def get_symbol(key: Literal["LAUNCH", "SUCCESS", "WARNING", "ERROR", "HINT"]) -> str: | ||
return THEME[key] | ||
THEME = themes[old_name] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters