Created
June 18, 2020 20:02
-
-
Save nosklo/d9c08f08e47375ce79850bc43df8b9f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import trio | |
import urwid | |
def exit_on_q(key): | |
if key in ('q', 'Q'): | |
raise urwid.ExitMainLoop() | |
class QuestionBox(urwid.Filler): | |
def keypress(self, size, key): | |
if key != 'enter': | |
return super(QuestionBox, self).keypress(size, key) | |
self.original_widget = urwid.Text(f"Starting task...") | |
trio_event_loop._nursery.start_soon(job, self.original_widget, int(edit.edit_text)) | |
edit = urwid.Edit(u"How many steps?\n") | |
fill = QuestionBox(edit) | |
async def job(text: urwid.Text, iterations: int): | |
"""An async job""" | |
await trio.sleep(1) | |
for x in range(1, iterations + 1): | |
text.set_text(f'Step {x}/{iterations} done') | |
await trio.sleep(0.5) | |
text.set_text(f'FINALIZED! Press Q to exit.') | |
trio_event_loop = urwid.TrioEventLoop() | |
loop = urwid.MainLoop(fill, unhandled_input=exit_on_q, event_loop=trio_event_loop) | |
loop.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment