Skip to content

Commit c639af1

Browse files
authored
✨ Feature: CLI 用户体验优化 (#150)
1 parent b2f621e commit c639af1

File tree

44 files changed

+4264
-996
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+4264
-996
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ docs_build/_build
88
.docusaurus
99
website/docs/api/**/*.md
1010
website/static/packages/*.whl
11-
!nb_cli/project/{{cookiecutter.project_slug}}/.env
11+
!nb_cli/template/project/*/{{cookiecutter.computed.project_slug}}/.env*
12+
!nb_cli/template/project/simple/{{cookiecutter.computed.project_slug}}/.vscode
1213

1314
# Created by https://www.toptal.com/developers/gitignore/api/node,macos,linux,python,windows,jetbrains,visualstudiocode
1415
# Edit at https://www.toptal.com/developers/gitignore?templates=node,macos,linux,python,windows,jetbrains,visualstudiocode

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ repos:
3131
- id: prettier
3232
types_or: [javascript, jsx, ts, tsx, markdown, yaml, json]
3333
stages: [pre-commit]
34+
exclude: ^nb_cli/template/project/.*\.json$
3435

3536
- repo: https://github.com/nonebot/nonemoji
3637
rev: v0.1.4

nb_cli/cli/__init__.py

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Optional, cast
33

44
import click
5-
from noneprompt import Choice, ListPrompt, CancelledError
5+
from noneprompt import Choice, ListPrompt, ConfirmPrompt, CancelledError
66

77
from nb_cli import _, __version__
88
from nb_cli.handlers import draw_logo
@@ -16,6 +16,22 @@
1616
from .customize import ClickAliasedCommand as ClickAliasedCommand
1717

1818

19+
@click.command # this command only appears in interactive mode.
20+
@click.pass_context
21+
@run_async
22+
async def exit_(ctx: click.Context):
23+
if await ConfirmPrompt(_("Are you sure to exit NB CLI?"), True).prompt_async(
24+
style=CLI_DEFAULT_STYLE
25+
):
26+
ctx.exit()
27+
28+
29+
@click.command # this command only appears in interactive mode.
30+
def back_():
31+
# nothing to do, just to keep a command to avoid type violation
32+
return
33+
34+
1935
def _set_global_working_dir(
2036
ctx: click.Context, param: click.Option, value: Optional[Path]
2137
):
@@ -93,28 +109,44 @@ async def cli(ctx: click.Context):
93109
sub_cmd,
94110
)
95111
)
112+
_exit_choice = Choice(_("Exit NB CLI."), exit_)
113+
choices.append(_exit_choice)
96114

97115
click.secho(draw_logo(), fg="cyan", bold=True)
98116
click.echo("\n\b")
99117
click.secho(_("Welcome to NoneBot CLI!"), fg="green", bold=True)
100118

101-
# prompt user to choose
102-
try:
103-
result = await ListPrompt(
104-
_("What do you want to do?"), choices=choices
105-
).prompt_async(style=CLI_DEFAULT_STYLE)
106-
except CancelledError:
107-
ctx.exit()
108-
109-
sub_cmd = result.data
110-
await run_sync(ctx.invoke)(sub_cmd)
111-
112-
113-
from .commands import run, self, create, driver, plugin, adapter, generate
119+
while True:
120+
# prompt user to choose
121+
try:
122+
result = await ListPrompt(
123+
_("What do you want to do?"), choices=choices
124+
).prompt_async(style=CLI_DEFAULT_STYLE)
125+
except CancelledError:
126+
result = _exit_choice
127+
128+
sub_cmd = result.data
129+
ctx.params["can_back_to_parent"] = True
130+
await run_sync(ctx.invoke)(sub_cmd)
131+
132+
133+
from .commands import (
134+
run,
135+
self,
136+
create,
137+
driver,
138+
plugin,
139+
adapter,
140+
generate,
141+
upgrade_format,
142+
downgrade_format,
143+
)
114144

115145
cli.add_command(create)
116146
cli.add_command(run)
117147
cli.add_command(generate)
148+
cli.add_command(upgrade_format)
149+
cli.add_command(downgrade_format)
118150

119151
cli.add_command(plugin)
120152

nb_cli/cli/commands/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
from .project import create as create
66
from .adapter import adapter as adapter
77
from .project import generate as generate
8+
from .project import upgrade_format as upgrade_format
9+
from .project import downgrade_format as downgrade_format

0 commit comments

Comments
 (0)