Skip to content

Commit

Permalink
✨ split text styles for minecraft & telegram
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Aug 19, 2024
1 parent 1c12bd8 commit 93d66e9
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ target-version = "py39"

[tool.ruff.lint]
select = ["E", "W", "F", "UP", "C", "T", "PYI", "PT", "Q"]
ignore = ["C901", "T201", "E731", "E402"]
ignore = ["C901", "T201", "E731", "E402", "PT023"]

[tool.pyright]
pythonVersion = "3.9"
Expand Down
8 changes: 6 additions & 2 deletions src/nonebot_plugin_alconna/uniseg/adapters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@
except Exception as e:
warn(f"Failed to import uniseg adapter {name}: {e}", RuntimeWarning, 15)

EXPORTER_MAPPING: dict[str, MessageExporter] = {SupportAdapter.nonebug.value: loaders["nonebug"].get_exporter()}
BUILDER_MAPPING: dict[str, MessageBuilder] = {SupportAdapter.nonebug.value: loaders["nonebug"].get_builder()}
EXPORTER_MAPPING: dict[str, MessageExporter] = {
SupportAdapter.nonebug.value: loaders[SupportAdapter.nonebug.value].get_exporter()
}
BUILDER_MAPPING: dict[str, MessageBuilder] = {
SupportAdapter.nonebug.value: loaders[SupportAdapter.nonebug.value].get_builder()
}
FETCHER_MAPPING: dict[str, TargetFetcher] = {}
adapters = {}
try:
Expand Down
67 changes: 43 additions & 24 deletions src/nonebot_plugin_alconna/uniseg/adapters/minecraft/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@
"strikethrough": "strikethrough",
"obf": "obfuscated",
"obfuscated": "obfuscated",
"black": "black",
"dark_blue": "dark_blue",
"dark_green": "dark_green",
"dark_aqua": "dark_aqua",
"dark_red": "dark_red",
"dark_purple": "dark_purple",
"gold": "gold",
"gray": "gray",
"dark_gray": "dark_gray",
"blue": "blue",
"green": "green",
"aqua": "aqua",
"red": "red",
"light_purple": "light_purple",
"yellow": "yellow",
"white": "white",
}

for color in TextColor.__members__.values():
Expand Down Expand Up @@ -54,30 +70,33 @@ def get_target(self, event: Event, bot: Union[Bot, None] = None) -> Target:
)

@export
async def text(self, seg: Text, bot: Union[Bot, None]) -> "MessageSegment":
styles = seg.extract_most_styles()
kwargs = {}
for style in styles:
if style not in STYLE_TYPE_MAP:
continue
style = STYLE_TYPE_MAP[style]
if style == "bold":
kwargs["bold"] = True
elif style == "italic":
kwargs["italic"] = True
elif style == "underline":
kwargs["underlined"] = True
elif style == "strikethrough":
kwargs["strikethrough"] = True
elif style == "obfuscated":
kwargs["obfuscated"] = True
async def text(self, seg: Text, bot: Union[Bot, None]) -> "list[MessageSegment]":
res = []
for part in seg.style_split():
kwargs = {}
for style in part.extract_most_styles():
if style not in STYLE_TYPE_MAP:
continue
style = STYLE_TYPE_MAP[style]
if style == "bold":
kwargs["bold"] = True
elif style == "italic":
kwargs["italic"] = True
elif style == "underline":
kwargs["underlined"] = True
elif style == "strikethrough":
kwargs["strikethrough"] = True
elif style == "obfuscated":
kwargs["obfuscated"] = True
elif "color" not in kwargs:
kwargs["color"] = style
if "actionbar" in part.styles:
res.append(MessageSegment.actionbar(part.text, **kwargs))
elif "title" in part.styles:
res.append(MessageSegment.title(BaseComponent(text=part.text, **kwargs)))
else:
kwargs["color"] = style
if "actionbar" in styles:
return MessageSegment.actionbar(seg.text, **kwargs)
if "title" in styles:
return MessageSegment.title(BaseComponent(text=seg.text, **kwargs))
return MessageSegment.text(seg.text, **kwargs)
res.append(MessageSegment.text(part.text, **kwargs))
return res

@export
async def button(self, seg: Button, bot: Union[Bot, None]):
Expand All @@ -98,7 +117,7 @@ async def button(self, seg: Button, bot: Union[Bot, None]):
kwargs["strikethrough"] = True
elif style == "obfuscated":
kwargs["obfuscated"] = True
else:
elif "color" not in kwargs:
kwargs["color"] = style
if seg.clicked_label:
kwargs["hover_event"] = HoverEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def get_message_id(self, event: Event) -> str:
return f"{event.message_id}"

@export
async def text(self, seg: Text, bot: Union[Bot, None]) -> "MessageSegment":
async def text(self, seg: Text, bot: Union[Bot, None]):
if not seg.styles:
return Entity.text(seg.text)
else:
Expand All @@ -85,7 +85,11 @@ async def text(self, seg: Text, bot: Union[Bot, None]) -> "MessageSegment":
return Entity.url(seg.text)
else:
return Entity.text_link(seg._children[0].text, seg.text) # type: ignore
return Entity(STYLE_TYPE_MAP.get(style, style), {"text": seg.text})
res = []
for part in seg.style_split():
style = part.extract_most_style()
res.append(Entity(STYLE_TYPE_MAP.get(style, style), {"text": part.text}))
return res

@export
async def at(self, seg: At, bot: Union[Bot, None]) -> "MessageSegment":
Expand Down
15 changes: 15 additions & 0 deletions tests/test_mc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def test_mc_style_text():
from nonebot.adapters.minecraft.model import TextColor
from nonebot.adapters.minecraft.message import Message, MessageSegment

from nonebot_plugin_alconna import Text, UniMessage

msg = UniMessage([Text("1234").color("red", 0, 2).color("yellow"), Text("456").color("blue")])

assert msg.export_sync(adapter="Minecraft") == Message(
[
MessageSegment.text("12", color=TextColor.RED),
MessageSegment.text("34", color=TextColor.YELLOW),
MessageSegment.text("456", color=TextColor.BLUE),
]
)

0 comments on commit 93d66e9

Please sign in to comment.