Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: 美化插件树的显示 #27

Merged
merged 4 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/

## [Unreleased]

### Changed

- 美化插件树的显示

## [0.0.2] - 2023-02-06

### Added
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ _✨ NoneBot 树形帮助插件 ✨_
</a>
</p>

## 简介

使用插件元数据获取插件信息,并通过插件与子插件的组织形式,来区分插件的多种功能。

树形帮助插件,最重要的功能当然是显示插件树!

发送 `/help tree`,你将获得如下帮助:

```text
测试 # 一个测试插件
├── 复杂功能 # 测试插件复杂子插件
│ └── 二级功能 # 测试插件二级插件
└── 简单功能 # 测试插件简单子插件
帮助 # 获取插件帮助信息
```

## 使用方式

加载插件后发送 `/help` 或 `/帮助` 获取具体用法。
Expand Down
33 changes: 22 additions & 11 deletions nonebot_plugin_treehelp/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

获取插件的帮助信息,并通过子插件的形式获取次级菜单
"""
from textwrap import indent
from typing import TYPE_CHECKING, Dict, List, Optional, Set

from nonebot import get_loaded_plugins
Expand Down Expand Up @@ -129,20 +128,28 @@ def get_plugin_help(name: str, bot: "Bot") -> Optional[str]:
return "\n\n".join([x for x in [name, usage, sub_plugins_desc] if x])


def get_tree_string(plugins: Set["Plugin"], bot: "Bot") -> str:
def get_tree_string(
docs: List[str],
plugins: Set["Plugin"],
previous_tree_bar: str,
bot: "Bot",
) -> None:
"""通过递归获取树形结构的字符串"""
previous_tree_bar = previous_tree_bar.replace("├", "│")

filtered_plugins = filter(
lambda x: x.metadata is not None and is_supported_adapter(x, bot), plugins
)
sorted_plugins = sorted(filtered_plugins, key=lambda x: x.metadata.name) # type: ignore

docs = []
for plugin in sorted_plugins:
docs.append(f"{plugin.metadata.name} # {plugin.metadata.description}") # type: ignore
sub = get_tree_string(plugin.sub_plugins, bot)
if sub:
docs.append(indent(sub, "--"))
return "\n".join(docs)
tree_bar = previous_tree_bar + "├"
total = len(sorted_plugins)
for i, plugin in enumerate(sorted_plugins, 1):
if i == total:
tree_bar = previous_tree_bar + "└"
docs.append(f"{tree_bar}── {plugin.metadata.name} # {plugin.metadata.description}") # type: ignore
tree_bar = tree_bar.replace("└", " ")
get_tree_string(docs, plugin.sub_plugins, tree_bar + " ", bot)


def get_tree_view(bot: "Bot") -> str:
Expand All @@ -152,5 +159,9 @@ def get_tree_view(bot: "Bot") -> str:
for plugin in get_plugins().values()
if plugin.parent_plugin is None and is_supported_adapter(plugin, bot)
}
docs = get_tree_string(plugins, bot)
return docs
sorted_plugins = sorted(plugins, key=lambda x: x.metadata.name) # type: ignore
docs = []
for plugin in sorted_plugins:
docs.append(f"{plugin.metadata.name} # {plugin.metadata.description}") # type: ignore
get_tree_string(docs, plugin.sub_plugins, "", bot)
return "\n".join(docs)
159 changes: 158 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pytest-cov = "^4.0.0"
pytest-xdist = "^3.0.2"
pytest-mock = "^3.10.0"
pytest-asyncio = "^0.20.2"
nonebot-adapter-console = "^0.3.2"

[tool.black]
line-length = 88
Expand All @@ -38,6 +39,10 @@ asyncio_mode = "auto"
pythonVersion = "3.8"
pythonPlatform = "All"

[tool.nonebot]
adapters = [{name = "Console", module_name = "nonebot.adapters.console", project_link = "nonebot-adapter-console", desc = "基于终端的交互式适配器"}]
plugins = ["nonebot_plugin_treehelp", "tests.plugins.tree"]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
2 changes: 1 addition & 1 deletion tests/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def test_tree_view(app: App):
ctx.receive_event(bot, event)
ctx.should_call_send(
event,
"帮助 # 获取插件帮助信息\n测试 # 一个测试插件\n--复杂功能 # 测试插件复杂子插件\n----二级功能 # 测试插件二级插件\n--简单功能 # 测试插件简单子插件",
"帮助 # 获取插件帮助信息\n测试 # 一个测试插件\n├── 复杂功能 # 测试插件复杂子插件\n│ └── 二级功能 # 测试插件二级插件\n└── 简单功能 # 测试插件简单子插件",
True,
)
ctx.should_finished()