Skip to content

Commit

Permalink
feat: 新增风格化文本的append与print功能
Browse files Browse the repository at this point in the history
  • Loading branch information
weiduhuo committed Nov 10, 2023
1 parent 9561e87 commit 48af8d6
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion utils/calculated.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from pynput.keyboard import Controller as KeyboardController
from pynput.keyboard import Key
from pynput.mouse import Controller as MouseController
from questionary import Validator, ValidationError
from questionary import Validator, ValidationError, Style
from collections.abc import Iterable

from .config import CONFIG_FILE_NAME, _, get_file, sra_config_obj
Expand Down Expand Up @@ -1052,6 +1052,30 @@ def validate(self, document):
raise ValidationError(message=_("存在命名冲突"), cursor_position=len(document.text))


class StyledText(List[Tuple[str, str]]):
"""
说明:
风格化文本,继承`List`的`(style_class, text)`序列,重载了`append()`方法
"""
def append(self, text: Union[str, Tuple[str, str]], style_class: str="") -> None:
if isinstance(text, str):
if style_class and "class:" not in style_class:
style_class = "class:" + style_class
return super().append((style_class, text))
else:
return super().append(text)

def print_styled_text(text: StyledText, style: Style, **kwargs: Any) -> None:
"""
说明:
打印风格化文本
"""
from prompt_toolkit import print_formatted_text
from prompt_toolkit.formatted_text import FormattedText

print_formatted_text(FormattedText(text), style=style, **kwargs)


def get_data_hash(data: Any, key_filter: Optional[List[str]]=None, speed_modified=False) -> str:
"""
说明:
Expand Down

0 comments on commit 48af8d6

Please sign in to comment.