Skip to content

feat(cmp): add dynamic completion support to run command#546

Merged
wangl-cc merged 11 commits into
MaaAssistantArknights:mainfrom
PFiS1737:feat-dynamic-cmp
May 23, 2026
Merged

feat(cmp): add dynamic completion support to run command#546
wangl-cc merged 11 commits into
MaaAssistantArknights:mainfrom
PFiS1737:feat-dynamic-cmp

Conversation

@PFiS1737
Copy link
Copy Markdown
Contributor

@PFiS1737 PFiS1737 commented May 11, 2026

该 PR 使用 clap 的动态补全功能优化了 maa run <task> 的补全提示,现在可以根据配置文件动态显示补全内容。

细节

  • 新增 Tasks,用来获取当前配置的任务文件,并提供打印和补全功能。

参考

Check List

  • 移除了手动维护的 fish shell 的补全文件。
  • 更新了文档。

对软件包维护者

由于使用了新的生成补全脚本的方式(见下文),可能需要更新打包脚本。

Edit: 考虑到兼容性,旧的方法仍然可以使用

快速体验

cargo build

# 对于 fish shell,其他类似
MAA_COMPLETE=fish ./target/debug/maa | source

# 在配置中创建几个 task
# 如果系统中已有 maa 的补全脚本,请先禁用

# 不要用 ./target/debug/maa,就直接 maa,没有这条指令也行
maa run <tab>

生成补全脚本

其中 <shell> 可以是 bashzshfishpowershellelvish

env MAA_COMPLETE=<shell> maa

Summary by Sourcery

run 任务参数添加动态、基于配置的 Shell 补全支持,并简化任务列表输出。

新功能:

  • 基于已配置的任务文件,为 run 命令的任务参数启用动态 Shell 补全。
  • 引入一个 Tasks 帮助器,用于读取已配置的任务名称,并同时提供展示和补全能力。
  • 通过在设置 MAA_COMPLETE 环境变量的情况下调用 CLI,支持为多个 Shell 生成补全脚本。

改进:

  • list 命令中,用新的 Tasks 帮助器替代手动扫描任务目录的逻辑。

文档:

  • 在所有本地化安装指南中记录如何生成补全脚本,并明确说明支持的 Shell 类型。

测试:

  • 删除已废弃的 complete 子命令对应的过时测试。

杂项:

  • 移除手动维护的 Fish Shell 补全文件,改为依赖自动生成的补全。
  • 更新 clap_complete 依赖,以启用不稳定的动态补全功能。
Original summary in English

Summary by Sourcery

Add dynamic, config-driven shell completion support for the run task argument and simplify task listing output.

New Features:

  • Enable dynamic shell completion for the run command's task parameter based on configured task files.
  • Introduce a Tasks helper to read configured task names and provide both display and completion capabilities.
  • Support generating completion scripts for multiple shells by invoking the CLI under the MAA_COMPLETE environment variable.

Enhancements:

  • Replace manual task directory scanning in the list command with reuse of the new Tasks helper.

Documentation:

  • Document how to generate completion scripts in all localized installation guides and clarify supported shells.

Tests:

  • Remove the obsolete test for the deleted complete subcommand.

Chores:

  • Remove the manually maintained Fish shell completion file and rely on generated completions.
  • Update the clap_complete dependency to enable unstable dynamic completion features.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 11, 2026

Codecov Report

❌ Patch coverage is 51.28205% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.97%. Comparing base (5d0638c) to head (f9c3233).

Files with missing lines Patch % Lines
crates/maa-cli/src/tasks.rs 47.05% 14 Missing and 4 partials ⚠️
crates/maa-cli/src/main.rs 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #546      +/-   ##
==========================================
+ Coverage   71.86%   71.97%   +0.10%     
==========================================
  Files          69       70       +1     
  Lines        6412     6437      +25     
  Branches     6412     6437      +25     
==========================================
+ Hits         4608     4633      +25     
+ Misses       1477     1475       -2     
- Partials      327      329       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@PFiS1737 PFiS1737 changed the title feat(cmp): add support to dynamic completion feat(cmp): add dynamic completion support to run command May 12, 2026
@PFiS1737 PFiS1737 marked this pull request as ready for review May 12, 2026 07:37
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我发现了 2 个问题,并给出了一些总体反馈:

  • 考虑对 Tasks.names 进行排序(例如在 Tasks::new 中构造 Self 之前),这样 maa list 和补全建议在不同文件系统遍历顺序下都能保持确定性。
  • 之前的 Command::List 在任务目录缺失时会打印 "No tasks found",而新的 Tasks 实现则什么都不打印;如果你认为这条消息有用,可以在列表为空时保留一个类似的用户提示。
供 AI 代理使用的提示
Please address the comments from this code review:

## Overall Comments
- 考虑对 `Tasks.names` 进行排序(例如在 `Tasks::new` 中构造 `Self` 之前),这样 `maa list` 和补全建议在不同文件系统遍历顺序下都能保持确定性。
- 之前的 `Command::List` 在任务目录缺失时会打印 `"No tasks found"`,而新的 `Tasks` 实现则什么都不打印;如果你认为这条消息有用,可以在列表为空时保留一个类似的用户提示。

## Individual Comments

### Comment 1
<location path="crates/maa-cli/docs/ko-KR/install.md" line_range="90-99" />
<code_context>
 - `git2`: 提供 `libgit2` 资源更新后端,这个特性默认启用;
 - `vendored-openssl`: 自行编译 `openssl` 库,而不是使用系统的 `openssl` 库,这个特性默认禁用,这个特性通常在你的系统没有安装 `openssl` 库或者 `openssl` 版本过低时启用。

+## 生成补全脚本
+
+::: tip
+
+对于使用包管理器安装的用户,这通常是自动进行的,请先检查是否已经有补全提示了。
+
+:::
+
+你可以使用下面的命令为你的 shell 生成补全脚本:
+
+```bash
+env MAA_COMPLETE=<shell> maa
+```
+
+其中 `<shell>` 可以是 `bash``zsh``fish``powershell``elvish`+
 ## 安装 MaaCore 及资源
</code_context>
<issue_to_address>
**issue (typo):** 新增的补全部分在 ko-KR 文档中是中文而不是韩文。

在这个 `ko-KR` 文件中,新补全部分(标题和内容)是用简体中文编写的。请将这一部分翻译成韩文,以保持该文件与其余文档在语言环境上的一致性。
</issue_to_address>

### Comment 2
<location path="crates/maa-cli/docs/zh-TW/install.md" line_range="94-103" />
<code_context>
 - `git2`: 提供 `libgit2` 资源更新后端,这个特性默认启用;
 - `vendored-openssl`: 自行编译 `openssl` 库,而不是使用系统的 `openssl` 库,这个特性默认禁用,这个特性通常在你的系统没有安装 `openssl` 库或者 `openssl` 版本过低时启用。

+## 生成补全脚本
+
+::: tip
+
+对于使用包管理器安装的用户,这通常是自动进行的,请先检查是否已经有补全提示了。
+
+:::
+
+你可以使用下面的命令为你的 shell 生成补全脚本:
+
+```bash
+env MAA_COMPLETE=<shell> maa
+```
+
+其中 `<shell>` 可以是 `bash``zsh``fish``powershell``elvish`+
 ## 安装 MaaCore 及资源
</code_context>
<issue_to_address>
**suggestion (typo):** zh-TW 文档中新补全部分使用的是简体中文,而不是繁体中文。

这一部分使用的是简体中文(例如“生成补全脚本”、“补全”、“脚本”)。对于 `zh-TW`,请将这些内容转换为繁体中文(例如“生成補全腳本”以及其他对应的繁体形式),以匹配该语言环境。

Suggested implementation:

```
## 生成補全腳本

::: tip

對於使用套件管理器安裝的使用者,這通常會自動完成,請先檢查是否已經有補全提示了。

:::

你可以使用下面的指令為你的 shell 生成補全腳本:

```

```
其中 `<shell>` 可以是 `bash`、`zsh`、`fish`、`powershell` 或 `elvish`。

```
</issue_to_address>

Sourcery 对开源项目免费使用——如果你觉得我们的评审有帮助,欢迎分享 ✨
请帮我变得更有用!请对每条评论点击 👍 或 👎,我会根据反馈改进后续评审。
Original comment in English

Hey - I've found 2 issues, and left some high level feedback:

  • Consider sorting Tasks.names (e.g., before constructing Self in Tasks::new) so maa list and completion suggestions are deterministic regardless of filesystem iteration order.
  • The previous Command::List printed "No tasks found" when the task directory was missing, while the new Tasks implementation just prints nothing; if the message was useful, you might want to preserve a similar user-facing hint when the list is empty.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider sorting `Tasks.names` (e.g., before constructing `Self` in `Tasks::new`) so `maa list` and completion suggestions are deterministic regardless of filesystem iteration order.
- The previous `Command::List` printed `"No tasks found"` when the task directory was missing, while the new `Tasks` implementation just prints nothing; if the message was useful, you might want to preserve a similar user-facing hint when the list is empty.

## Individual Comments

### Comment 1
<location path="crates/maa-cli/docs/ko-KR/install.md" line_range="90-99" />
<code_context>
 - `git2`: 提供 `libgit2` 资源更新后端,这个特性默认启用;
 - `vendored-openssl`: 自行编译 `openssl` 库,而不是使用系统的 `openssl` 库,这个特性默认禁用,这个特性通常在你的系统没有安装 `openssl` 库或者 `openssl` 版本过低时启用。

+## 生成补全脚本
+
+::: tip
+
+对于使用包管理器安装的用户,这通常是自动进行的,请先检查是否已经有补全提示了。
+
+:::
+
+你可以使用下面的命令为你的 shell 生成补全脚本:
+
+```bash
+env MAA_COMPLETE=<shell> maa
+```
+
+其中 `<shell>` 可以是 `bash``zsh``fish``powershell``elvish`+
 ## 安装 MaaCore 及资源
</code_context>
<issue_to_address>
**issue (typo):** New completion section is in Chinese rather than Korean in the ko-KR documentation.

In this `ko-KR` file, the new completion section (title and content) is written in Simplified Chinese. Please translate this section into Korean to keep the locale consistent with the rest of the document.
</issue_to_address>

### Comment 2
<location path="crates/maa-cli/docs/zh-TW/install.md" line_range="94-103" />
<code_context>
 - `git2`: 提供 `libgit2` 资源更新后端,这个特性默认启用;
 - `vendored-openssl`: 自行编译 `openssl` 库,而不是使用系统的 `openssl` 库,这个特性默认禁用,这个特性通常在你的系统没有安装 `openssl` 库或者 `openssl` 版本过低时启用。

+## 生成补全脚本
+
+::: tip
+
+对于使用包管理器安装的用户,这通常是自动进行的,请先检查是否已经有补全提示了。
+
+:::
+
+你可以使用下面的命令为你的 shell 生成补全脚本:
+
+```bash
+env MAA_COMPLETE=<shell> maa
+```
+
+其中 `<shell>` 可以是 `bash``zsh``fish``powershell``elvish`+
 ## 安装 MaaCore 及资源
</code_context>
<issue_to_address>
**suggestion (typo):** New completion section in zh-TW uses Simplified Chinese instead of Traditional Chinese.

This section is written in Simplified Chinese (e.g., “生成补全脚本”, “补全”, “脚本”). For `zh-TW`, please convert these to Traditional characters (e.g., “生成補全腳本” and corresponding Traditional forms throughout) to match the locale.

Suggested implementation:

```
## 生成補全腳本

::: tip

對於使用套件管理器安裝的使用者,這通常會自動完成,請先檢查是否已經有補全提示了。

:::

你可以使用下面的指令為你的 shell 生成補全腳本:

```

```
其中 `<shell>` 可以是 `bash`、`zsh`、`fish`、`powershell` 或 `elvish`。

```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +90 to +99
## 生成补全脚本

::: tip

对于使用包管理器安装的用户,这通常是自动进行的,请先检查是否已经有补全提示了。

:::

你可以使用下面的命令为你的 shell 生成补全脚本:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (typo): 新增的补全部分在 ko-KR 文档中是中文而不是韩文。

在这个 ko-KR 文件中,新补全部分(标题和内容)是用简体中文编写的。请将这一部分翻译成韩文,以保持该文件与其余文档在语言环境上的一致性。

Original comment in English

issue (typo): New completion section is in Chinese rather than Korean in the ko-KR documentation.

In this ko-KR file, the new completion section (title and content) is written in Simplified Chinese. Please translate this section into Korean to keep the locale consistent with the rest of the document.

Comment thread crates/maa-cli/docs/zh-TW/install.md
@wangl-cc
Copy link
Copy Markdown
Member

为什么这个要通过 MAA_COMPLETE 这个环境变量来注入? 用 maa complete --dynamic fish | source 这样可以吗?

@PFiS1737
Copy link
Copy Markdown
Contributor Author

不行,只有基于环境变量的方法支持动态补全。

@wangl-cc
Copy link
Copy Markdown
Member

那就这样,但是我觉得旧的模式可以暂时保留,避免 BREAKING CHANGE。然后文档也可以改一下,推荐使用新的模式。

然后 crates/maa-cli/src/main.rs:124 + crates/maa-cli/src/tasks.rs:49 会让 maa list 后面多输出一个空行,这个你可能需要修一下。

@PFiS1737 PFiS1737 force-pushed the feat-dynamic-cmp branch from c4a48d8 to c9eefe9 Compare May 23, 2026 15:27
@PFiS1737
Copy link
Copy Markdown
Contributor Author

PFiS1737 commented May 23, 2026

已修改,并进行了 rebase

@PFiS1737
Copy link
Copy Markdown
Contributor Author

我是否需要为 tasks.rs 增加单元测试?

@wangl-cc
Copy link
Copy Markdown
Member

wangl-cc commented May 23, 2026

可以测一下,不过因为要读配置文件比较麻烦。你可以把 task new 默认接受一个参数接收一个 Path。这样可以在临时文件夹里面测试。然后加一个 helper 使用 maa_dir::config() 作为这个参数的默认值。

@wangl-cc
Copy link
Copy Markdown
Member

LGTM 感谢贡献!

@wangl-cc wangl-cc merged commit 10ee5b5 into MaaAssistantArknights:main May 23, 2026
20 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants