Skip to content

Instantly share code, notes, and snippets.

@nosklo
Created June 18, 2020 20:02
Show Gist options
  • Save nosklo/d9c08f08e47375ce79850bc43df8b9f5 to your computer and use it in GitHub Desktop.
Save nosklo/d9c08f08e47375ce79850bc43df8b9f5 to your computer and use it in GitHub Desktop.
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