Skip to content

Commit

Permalink
✨ version 0.2.0
Browse files Browse the repository at this point in the history
more setting
  • Loading branch information
RF-Tar-Railt committed Jul 17, 2023
1 parent fe92d1d commit c157762
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 64 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ repos:
- id: isort
stages: [commit]

- repo: https://github.com/psf/black
- repo: https://ghproxy.com/github.com/psf/black
rev: 23.3.0
hooks:
- id: black
stages: [commit]

- repo: https://github.com/pre-commit/mirrors-prettier
- repo: https://ghproxy.com/github.com/pre-commit/mirrors-prettier
rev: v3.0.0-alpha.9-for-vscode
hooks:
- id: prettier
stages: [commit]

- repo: https://github.com/nonebot/nonemoji
- repo: https://ghproxy.com/github.com/nonebot/nonemoji
rev: v0.1.4
hooks:
- id: nonemoji
Expand Down
16 changes: 1 addition & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@
## 使用

```python
from nonechat.info import Event
from nonechat.app import Frontend
from nonechat.backend import Backend
from nonechat.info import User, MessageEvent, Event
from nonechat.message import ConsoleMessage, Text
from datetime import datetime


class ExampleBackend(Backend):

def on_console_init(self):
print("on_console_init")

def on_console_load(self):
print("on_console_load")

Expand All @@ -26,15 +21,6 @@ class ExampleBackend(Backend):
def on_console_unmount(self):
print("on_console_unmount")

async def build_message_event(self, message: str, user: User) -> MessageEvent:
return MessageEvent(
time=datetime.now(),
self_id="robot",
type="console.message",
user=user,
message=ConsoleMessage([Text(message)])
)

async def post_event(self, event: Event):
print("post_event")

Expand Down
17 changes: 3 additions & 14 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sys
from datetime import datetime
from asyncio import gather, create_task

from loguru import logger
Expand All @@ -9,7 +8,7 @@
from nonechat.backend import Backend
from nonechat.setting import ConsoleSetting
from nonechat.message import Text, ConsoleMessage
from nonechat.info import User, Event, Robot, MessageEvent
from nonechat.info import Event, Robot, MessageEvent


class ExampleBackend(Backend):
Expand All @@ -20,9 +19,6 @@ def __init__(self, frontend: "Frontend"):
self._stderr = sys.stderr
self._logger_id = None

def on_console_init(self):
print("on_console_init")

def on_console_load(self):
print("on_console_load")
logger.remove()
Expand All @@ -47,15 +43,6 @@ def on_console_unmount(self):
# logger.success("Console exit.")
# logger.warning("Press Ctrl-C for Application exit")

async def build_message_event(self, message: str, user: User) -> MessageEvent:
return MessageEvent(
time=datetime.now(),
self_id="robot",
type="console.message",
user=user,
message=ConsoleMessage([Text(message)]),
)

async def post_event(self, event: Event):
print("post_event")
if isinstance(event, MessageEvent):
Expand All @@ -80,6 +67,8 @@ def wrapper(func):
title_color=Color(229, 192, 123),
header_color=Color(90, 99, 108, 0.6),
icon_color=Color.parse("#22b14c"),
toolbar_exit="❌",
bot_name="Nonebot",
),
)

Expand Down
2 changes: 1 addition & 1 deletion nonechat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
from .message import ConsoleMessage as ConsoleMessage
from .setting import ConsoleSetting as ConsoleSetting

__version__ = "0.1.2"
__version__ = "0.2.0"
20 changes: 13 additions & 7 deletions nonechat/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from .views.log_view import LogView
from .components.footer import Footer
from .components.header import Header
from .info import Event, MessageEvent
from .message import Text, ConsoleMessage
from .info import User, Event, MessageEvent
from .views.horizontal import HorizontalView

TB = TypeVar("TB", bound=Backend)
Expand All @@ -32,15 +33,14 @@ class Frontend(App, Generic[TB]):

def __init__(self, backend: Type[TB], setting: ConsoleSetting = ConsoleSetting()):
super().__init__()
self.backend: TB = backend(self)
self.setting = setting
self.title = setting.title # type: ignore
self.sub_title = setting.sub_title # type: ignore
self.storage = Storage()
self.backend.on_console_init()
self.storage = Storage(User("console", setting.user_avatar, setting.user_name))
self._fake_output = cast(TextIO, FakeIO(self.storage))
self._redirect_stdout: Optional[contextlib.redirect_stdout[TextIO]] = None
self._redirect_stderr: Optional[contextlib.redirect_stderr[TextIO]] = None
self.backend: TB = backend(self)

def compose(self):
yield Header()
Expand Down Expand Up @@ -78,9 +78,9 @@ async def call(self, api: str, data: Dict[str, Any]):
MessageEvent(
type="console.message",
time=datetime.now(),
self_id=data["info"].id,
self_id=self.backend.bot.id,
message=data["message"],
user=data["info"],
user=self.backend.bot,
)
)
elif api == "bell":
Expand All @@ -91,7 +91,13 @@ def action_focus_input(self):
self.query_one(Input).focus()

async def action_post_message(self, message: str):
msg = await self.backend.build_message_event(message, self.storage.current_user)
msg = MessageEvent(
time=datetime.now(),
self_id=self.backend.bot.id,
type="console.message",
user=self.storage.current_user,
message=ConsoleMessage([Text(message)]),
)
self.storage.write_chat(msg)
await self.backend.post_event(msg)

Expand Down
13 changes: 4 additions & 9 deletions nonechat/backend.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import TYPE_CHECKING
from abc import ABC, abstractmethod

from .info import User, Event, MessageEvent
from .info import Event, Robot

if TYPE_CHECKING:
from .app import Frontend
Expand All @@ -12,10 +12,9 @@ class Backend(ABC):

def __init__(self, frontend: "Frontend"):
self.frontend = frontend

@abstractmethod
def on_console_init(self):
...
self.bot = Robot(
"robot", self.frontend.setting.bot_avatar, self.frontend.setting.bot_name
)

@abstractmethod
def on_console_load(self):
Expand All @@ -29,10 +28,6 @@ def on_console_mount(self):
def on_console_unmount(self):
...

@abstractmethod
async def build_message_event(self, message: str, user: User) -> MessageEvent:
...

@abstractmethod
async def post_event(self, event: Event):
...
Empty file added nonechat/components/__init__.py
Empty file.
File renamed without changes.
2 changes: 1 addition & 1 deletion nonechat/components/chatroom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .history import ChatHistory

if TYPE_CHECKING:
from ..app import Frontend
from ...app import Frontend


class ChatRoom(Widget):
Expand Down
20 changes: 13 additions & 7 deletions nonechat/components/chatroom/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from textual.widget import Widget
from textual.widgets import Static

from ..action import Action
from ...router import RouteChange
from ..general.action import Action

if TYPE_CHECKING:
from .history import ChatHistory
Expand Down Expand Up @@ -44,11 +44,13 @@ class Toolbar(Widget):

def __init__(self, setting: "ConsoleSetting"):
super().__init__()
self.exit_button = Action("⛔", id="exit", classes="left")
self.clear_button = Action("🗑️", id="clear", classes="left ml")
self.exit_button = Action(setting.toolbar_exit, id="exit", classes="left")
self.clear_button = Action(setting.toolbar_clear, id="clear", classes="left ml")
self.center_title = Static(setting.room_title, classes="center")
self.settings_button = Action("⚙️", id="settings", classes="right mr")
self.log_button = Action("📝", id="log", classes="right")
self.settings_button = Action(
setting.toolbar_setting, id="settings", classes="right mr"
)
self.log_button = Action(setting.toolbar_log, id="log", classes="right")

def compose(self):
yield self.exit_button
Expand All @@ -64,12 +66,16 @@ async def on_action_pressed(self, event: Action.Pressed):
if event.action == self.exit_button:
self.app.exit()
elif event.action == self.clear_button:
history = cast("ChatHistory", self.app.query_one("ChatHistory"))
history: "ChatHistory" = cast(
"ChatHistory", self.app.query_one("ChatHistory")
)
history.action_clear_history()
elif event.action == self.settings_button:
...
elif event.action == self.log_button:
view = cast("HorizontalView", self.app.query_one("HorizontalView"))
view: "HorizontalView" = cast(
"HorizontalView", self.app.query_one("HorizontalView")
)
if view.can_show_log:
view.action_toggle_log_panel()
else:
Expand Down
10 changes: 6 additions & 4 deletions nonechat/components/log/toolbar.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from textual.widget import Widget
from textual.widgets import Static

from ..action import Action
from ...router import RouteChange
from ..general.action import Action
from ...setting import ConsoleSetting


Expand Down Expand Up @@ -35,9 +35,11 @@ class Toolbar(Widget):

def __init__(self, settings: ConsoleSetting):
super().__init__()
self.exit_button = Action("⛔", id="exit", classes="left")
self.back_button = Action("⏪", id="back", classes="left ml")
self.settings_button = Action("⚙️", id="settings", classes="right")
self.exit_button = Action(settings.toolbar_exit, id="exit", classes="left")
self.back_button = Action(settings.toolbar_back, id="back", classes="left ml")
self.settings_button = Action(
settings.toolbar_setting, id="settings", classes="right"
)

def compose(self):
yield self.exit_button
Expand Down
9 changes: 9 additions & 0 deletions nonechat/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ class ConsoleSetting:
icon_color: Optional[Color] = None
bg_color: Optional[Color] = None
header_color: Optional[Color] = None
toolbar_exit: str = "⛔"
toolbar_clear: str = "🗑️"
toolbar_setting: str = "⚙️"
toolbar_log: str = "📝"
toolbar_back: str = "⏪"
user_avatar: str = "👤"
user_name: str = "User"
bot_avatar: str = "🤖"
bot_name: str = "Bot"
2 changes: 1 addition & 1 deletion nonechat/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, data: T) -> None:

@dataclass
class Storage:
current_user: User = field(default_factory=lambda: User(id="console"))
current_user: User

log_history: List[RenderableType] = field(default_factory=list)
log_watchers: List[Widget] = field(default_factory=list)
Expand Down
7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

[project]
name = "nonechat"
version = "0.1.2"
description = "Awesome chat console using Textual"
authors = [
{name = "RF-Tar-Railt", email = "[email protected]"},
Expand All @@ -12,6 +10,7 @@ dependencies = [
requires-python = ">=3.8, <4.0"
readme = "README.md"
license = {text = "MIT"}
dynamic = ["version"]

[project.urls]
homepage = "https://github.com/nonebot/nonechat"
Expand All @@ -30,6 +29,10 @@ dev = [
[tool.pdm.build]
includes = ["nonechat"]

[tool.pdm.version]
source = "file"
path = "nonechat/__init__.py"

[tool.black]
line-length = 88
target-version = ["py38", "py39", "py310", "py311"]
Expand Down

0 comments on commit c157762

Please sign in to comment.