Skip to content

Commit

Permalink
🍻 config
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Jul 26, 2023
1 parent 2a4b4b0 commit 8bd3cb9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
16 changes: 13 additions & 3 deletions nonebot/adapters/console/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import asyncio
from typing import Any, Dict, List, Callable, Optional, Awaitable

from nonechat import Frontend
from textual.color import Color
from nonechat import Frontend, ConsoleSetting
from nonebot.drivers import Driver
from nonebot.typing import overrides

Expand Down Expand Up @@ -40,7 +41,16 @@ def setup(self):
self.driver.on_shutdown(self._shutdown)

async def _start(self) -> None:
self._frontend = Frontend(AdapterConsoleBackend)
self._frontend = Frontend(
AdapterConsoleBackend,
ConsoleSetting(
title="Nonebot",
sub_title="welcome to Console",
toolbar_exit="❌",
toolbar_back="⬅",
icon_color=Color.parse("#EA5252"),
)
)
self._frontend.backend.set_adapter(self)
self._task = asyncio.create_task(self._frontend.run_async())
self.bot_connect(self.bot)
Expand All @@ -57,4 +67,4 @@ def post_event(self, event: Event) -> None:

@overrides(BaseAdapter)
async def _call_api(self, bot: Bot, api: str, **data: Any) -> None:
await self._frontend.call(api, **data)
await self._frontend.call(api, data)
10 changes: 7 additions & 3 deletions nonebot/adapters/console/backend.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
import contextlib
from dataclasses import asdict
from dataclasses import asdict, replace
from typing import TYPE_CHECKING, Optional

from nonechat import Backend
Expand All @@ -21,6 +21,10 @@
class AdapterConsoleBackend(Backend):
def __init__(self, frontend: "Frontend"):
super().__init__(frontend)
self.frontend.storage.current_user = replace(
self.frontend.storage.current_user,
id="User"
)
self._stdout = sys.stdout
self._logger_id: Optional[int] = None
self._should_restore_logger: bool = False
Expand Down Expand Up @@ -66,9 +70,9 @@ async def post_event(self, event: ConsoleEvent):
message = Message()
for elem in event.message:
if isinstance(elem, Text):
message += MessageSegment(type="text", data={"text": elem.text})
message += MessageSegment.text(elem.text)
elif isinstance(elem, Emoji):
message += MessageSegment(type="emoji", data={"name": elem.name})
message += MessageSegment.emoji(elem.name)
else:
message += MessageSegment(
type=elem.__class__.__name__.lower(), data=asdict(elem) # noqa
Expand Down
2 changes: 2 additions & 0 deletions nonebot/adapters/console/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def __radd__(

@overrides(BaseMessageSegment)
def __str__(self) -> str:
if self.type == "text":
return self.data["text"]
params = ", ".join(
[f"{k}={truncate(str(v))}" for k, v in self.data.items() if v is not None]
)
Expand Down

0 comments on commit 8bd3cb9

Please sign in to comment.