Skip to content

Commit

Permalink
✨ version 0.30.4
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Oct 26, 2023
1 parent 3254297 commit 4d03cbe
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 24 deletions.
2 changes: 1 addition & 1 deletion example/plugins/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ async def statis_h():
Arg("phone", int, Field(completion=lambda: "请输入手机号")),
)

cmd = on_alconna(alc, comp_config={})
cmd = on_alconna(alc, comp_config={"disables": {"tab"}})


@cmd.handle()
Expand Down
2 changes: 1 addition & 1 deletion src/nonebot_plugin_alconna/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
from .consts import ALCONNA_EXEC_RESULT as ALCONNA_EXEC_RESULT
from .extension import add_global_extension as add_global_extension

__version__ = "0.30.3"
__version__ = "0.30.4"

__plugin_meta__ = PluginMetadata(
name="Alconna 插件",
Expand Down
5 changes: 4 additions & 1 deletion src/nonebot_plugin_alconna/model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import field, dataclass
from typing_extensions import NotRequired
from typing import Type, Union, Generic, TypeVar, Optional, TypedDict
from typing import Set, Type, Union, Generic, Literal, TypeVar, Optional, TypedDict

from arclet.alconna import Empty, Alconna, Arparma
from arclet.alconna.duplication import Duplication
Expand Down Expand Up @@ -61,4 +61,7 @@ class CompConfig(TypedDict):
enter: NotRequired[str]
exit: NotRequired[str]
timeout: NotRequired[int]
hide_tabs: NotRequired[bool]
hides: NotRequired[Set[Literal["tab", "enter", "exit"]]]
disables: NotRequired[Set[Literal["tab", "enter", "exit"]]]
lite: NotRequired[bool]
54 changes: 34 additions & 20 deletions src/nonebot_plugin_alconna/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class AlconnaRule:
"_waiter",
"_future",
"_interface",
"_comp_help",
)

def __init__(
Expand Down Expand Up @@ -96,15 +97,36 @@ def __init__(
rule=Rule(lambda: self._session is not None),
)
self._waiter.destroy()
self._comp_help = ""
if self.comp_config is not None:
_tab = self.comp_config.get("tab", ".tab")
_enter = self.comp_config.get("enter", ".enter")
_exit = self.comp_config.get("exit", ".exit")
_tab = self.comp_config.get("tab") or ".tab"
_enter = self.comp_config.get("enter") or ".enter"
_exit = self.comp_config.get("exit") or ".exit"
disables = self.comp_config.get("disables", set())
hides = self.comp_config.get("hides", set())
hide_tabs = self.comp_config.get("hide_tabs", False)
if self.comp_config.get("lite", False):
hide_tabs = True
hides = {"tab", "enter", "exit"}
hides |= disables
if len(hides) < 3:
template = f"\n\n{{}}{{}}{{}}{lang.require('comp/nonebot', 'other')}\n"
self._comp_help = template.format(
(lang.require("comp/nonebot", "tab").format(cmd=_tab) + "\n")
if "tab" not in hides
else "",
(lang.require("comp/nonebot", "enter").format(cmd=_enter) + "\n")
if "enter" not in hides
else "",
(lang.require("comp/nonebot", "exit").format(cmd=_exit) + "\n")
if "exit" not in hides
else "",
)

@self._waiter.handle()
async def _waiter_handle(_bot: Bot, _event: Event, content: Message = EventMessage()):
msg = str(content)
if msg.startswith(_exit):
if msg.startswith(_exit) and "exit" not in disables:
if msg == _exit:
self._future.set_result(False)
await self._waiter.finish()
Expand All @@ -115,7 +137,7 @@ async def _waiter_handle(_bot: Bot, _event: Event, content: Message = EventMessa
target=msg.replace(_exit, "", 1)
)
)
elif msg.startswith(_enter):
elif msg.startswith(_enter) and "enter" not in disables:
if msg == _enter:
self._future.set_result(True)
await self._waiter.finish()
Expand All @@ -126,7 +148,7 @@ async def _waiter_handle(_bot: Bot, _event: Event, content: Message = EventMessa
target=msg.replace(_enter, "", 1)
)
)
elif msg.startswith(_tab):
elif msg.startswith(_tab) and "tab" not in disables:
offset = msg.replace(_tab, "", 1).lstrip() or 1
try:
offset = int(offset)
Expand All @@ -137,12 +159,11 @@ async def _waiter_handle(_bot: Bot, _event: Event, content: Message = EventMessa
)
else:
self._interface.tab(offset)
if self.comp_config is not None and self.comp_config.get("lite", False):
out = f"* {self._interface.current()}"
else:
out = "\n".join(self._interface.lines())
self._future.set_result(None)
await self._waiter.pause(out)
await self._waiter.pause(
f"* {self._interface.current()}"
if hide_tabs
else "\n".join(self._interface.lines())
)
else:
self._future.set_result(content)
await self._waiter.finish()
Expand Down Expand Up @@ -176,15 +197,8 @@ async def handle(self, bot: Bot, event: Event, msg: Message) -> Union[Arparma, L
error_info=SpecialOptionTriggered("completion"),
)

help_text = (
f"{lang.require('comp/nonebot', 'tab').format(cmd=self.comp_config.get('tab', '.tab'))}\n"
f"{lang.require('comp/nonebot', 'enter').format(cmd=self.comp_config.get('enter', '.enter'))}\n"
f"{lang.require('comp/nonebot', 'exit').format(cmd=self.comp_config.get('exit', '.exit'))}\n"
f"{lang.require('comp/nonebot', 'other')}\n"
)

while self._interface.available:
await self.send(f"{str(self._interface)}\n\n{help_text}", bot, event, res)
await self.send(f"{str(self._interface)}{self._comp_help}", bot, event, res)
while True:
self._future = asyncio.get_running_loop().create_future()
try:
Expand Down
2 changes: 1 addition & 1 deletion src/nonebot_plugin_alconna/uniseg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from .params import UniversalMessage as UniversalMessage
from .params import UniversalSegment as UniversalSegment

__version__ = "0.30.3"
__version__ = "0.30.4"

__plugin_meta__ = PluginMetadata(
name="Universal Segment 插件",
Expand Down

0 comments on commit 4d03cbe

Please sign in to comment.