Spaces:
Runtime error
Runtime error
File size: 3,080 Bytes
82a7a28 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
import json
import textwrap
from datetime import datetime
from typing import Union
from tinytroupe.utils import logger
################################################################################
# Rendering and markup
################################################################################
def inject_html_css_style_prefix(html, style_prefix_attributes):
"""
Injects a style prefix to all style attributes in the given HTML string.
For example, if you want to add a style prefix to all style attributes in the HTML string
``<div style="color: red;">Hello</div>``, you can use this function as follows:
inject_html_css_style_prefix('<div style="color: red;">Hello</div>', 'font-size: 20px;')
"""
return html.replace('style="', f'style="{style_prefix_attributes};')
def break_text_at_length(text: Union[str, dict], max_length: int=None) -> str:
"""
Breaks the text (or JSON) at the specified length, inserting a "(...)" string at the break point.
If the maximum length is `None`, the content is returned as is.
"""
if isinstance(text, dict):
text = json.dumps(text, indent=4)
if max_length is None or len(text) <= max_length:
return text
else:
return text[:max_length] + " (...)"
def pretty_datetime(dt: datetime) -> str:
"""
Returns a pretty string representation of the specified datetime object.
"""
return dt.strftime("%Y-%m-%d %H:%M")
def dedent(text: str) -> str:
"""
Dedents the specified text, removing any leading whitespace and identation.
"""
return textwrap.dedent(text).strip()
def wrap_text(text: str, width: int=100) -> str:
"""
Wraps the text at the specified width.
"""
return textwrap.fill(text, width=width)
class RichTextStyle:
# Consult color options here: https://rich.readthedocs.io/en/stable/appendix/colors.html
STIMULUS_CONVERSATION_STYLE = "bold italic cyan1"
STIMULUS_THOUGHT_STYLE = "dim italic cyan1"
STIMULUS_DEFAULT_STYLE = "italic"
ACTION_DONE_STYLE = "grey82"
ACTION_TALK_STYLE = "bold green3"
ACTION_THINK_STYLE = "green"
ACTION_DEFAULT_STYLE = "purple"
INTERVENTION_DEFAULT_STYLE = "bright_magenta"
@classmethod
def get_style_for(cls, kind:str, event_type:str=None):
if kind == "stimulus" or kind=="stimuli":
if event_type == "CONVERSATION":
return cls.STIMULUS_CONVERSATION_STYLE
elif event_type == "THOUGHT":
return cls.STIMULUS_THOUGHT_STYLE
else:
return cls.STIMULUS_DEFAULT_STYLE
elif kind == "action":
if event_type == "DONE":
return cls.ACTION_DONE_STYLE
elif event_type == "TALK":
return cls.ACTION_TALK_STYLE
elif event_type == "THINK":
return cls.ACTION_THINK_STYLE
else:
return cls.ACTION_DEFAULT_STYLE
elif kind == "intervention":
return cls.INTERVENTION_DEFAULT_STYLE
|