Skip to content

Instantly share code, notes, and snippets.

@dpjanes
Created August 15, 2022 22:34
Show Gist options
  • Save dpjanes/d26de9e0cbd54d990037a8d7bc6b98fc to your computer and use it in GitHub Desktop.
Save dpjanes/d26de9e0cbd54d990037a8d7bc6b98fc to your computer and use it in GitHub Desktop.
Flutter Doctor-like Python code
import asyncio
from yachalk import chalk
async def check_1():
await asyncio.sleep(2)
return True, "all good man"
check_1.LABEL = "doing check 1"
async def check_2():
await asyncio.sleep(2)
return False, "many such cases, sad"
check_2.LABEL = "doing check 2"
async def check_3():
await asyncio.sleep(1)
return None, "IDK"
check_3.LABEL = "doing check 3"
async def spin(message):
spinner = "⣾⣽⣻⢿⡿⣟⣯⣷"
print(chalk.blue(f"_ {message}"), end="\r")
try:
x = -1
while True:
x += 1
print(chalk.yellow(spinner[x % len(spinner)]), end="\r")
await asyncio.sleep(0.1)
except asyncio.CancelledError:
pass
async def run(f):
task_f = asyncio.create_task(f())
task_spin = asyncio.create_task(spin(f.LABEL))
result, message = await task_f
task_spin.cancel()
print(" " * (len(f.LABEL) + 2), end="\r")
if result is None:
print(chalk.yellow("[!] "), end="")
elif result:
print(chalk.green("[✓] "), end="")
else:
print(chalk.red("[✗] "), end="")
print(message)
async def the_async_part():
await run(check_1)
await run(check_2)
await run(check_3)
asyncio.run(the_async_part())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment