Skip to content

Commit 52c3334

Browse files
Merge pull request #99 from uright008/feature/confirm-before-exit
feat: confirm before exit if there is still any task
2 parents f050a20 + 20437ab commit 52c3334

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

src/cmd.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import argparse
22
import asyncio
33
import sys
4+
import os
45

56
import grpc.aio
67
from creart import it
@@ -111,8 +112,7 @@ async def command_parser(self, cmd: str):
111112
case "status":
112113
await self.show_status()
113114
case "exit":
114-
self.loop.stop()
115-
sys.exit()
115+
self.handle_exit()
116116

117117
async def do_download(self, raw_urls: list[str], codec: str, force_download: bool, language: str, include: bool = False):
118118
for raw_url in raw_urls:
@@ -171,6 +171,16 @@ def completer(self):
171171
}
172172
return NestedCompleter.from_nested_dict(mycompleter)
173173

174+
def handle_exit(self):
175+
if it(Measurer).tasks_count() > 0:
176+
it(GlobalLogger).logger.info("There is still {} tasks, do you really want to exit? (y/N)".format(it(Measurer).tasks_count()))
177+
response = input().strip().lower()
178+
if response != 'y':
179+
return
180+
it(GlobalLogger).logger.info("Exit.")
181+
self.loop.stop()
182+
os._exit(0)
183+
174184
async def handle_command(self):
175185
session = PromptSession("> ", bottom_toolbar=self.bottom_toolbar, completer=self.completer(), refresh_interval=1)
176186

@@ -186,7 +196,7 @@ async def handle_command(self):
186196
else:
187197
await self.command_parser(command)
188198
except (EOFError, KeyboardInterrupt):
189-
return
199+
self.handle_exit()
190200

191201
async def on_2fa(self, username: str, password: str):
192202
session = PromptSession()
@@ -224,6 +234,5 @@ async def start(self):
224234
try:
225235
await self.handle_command()
226236
finally:
227-
it(GlobalLogger).logger.info("Exit.")
228237
if it(Config).localInstance.enable:
229-
await self.localInstance.terminate()
238+
self.localInstance.terminate()

0 commit comments

Comments
 (0)