Created
January 28, 2017 20:11
-
-
Save hiway/0700e70ff2a527cefffb78090a40a530 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python | |
import asyncio | |
from prompt_toolkit import CommandLineInterface | |
from prompt_toolkit.shortcuts import create_prompt_application | |
from prompt_toolkit.shortcuts import create_asyncio_eventloop | |
def get_cli(event_loop): | |
return CommandLineInterface(application=create_prompt_application(), | |
eventloop=event_loop) | |
class LocalCommand(object): | |
def __init__(self, loop): | |
self.loop = loop | |
self.command = None | |
async def run(self, command): | |
self.command = command | |
await asyncio.sleep(2) | |
print(command) | |
class Shell(object): | |
def __init__(self): | |
ptk_loop = create_asyncio_eventloop() | |
self.loop = ptk_loop.loop | |
self.cli = get_cli(ptk_loop) | |
self._task_shell = None | |
self._task_super = None | |
def run(self): | |
task_start = self.loop.create_task(self.start()) | |
self.loop.run_until_complete(task_start) | |
self.loop.close() | |
async def start(self): | |
self._task_shell = await self.loop.create_task(self.interact()) | |
await asyncio.wait(self._task_shell) | |
async def interact(self): | |
while True: | |
command = await self.cli.run_async() | |
self.loop.create_task(LocalCommand(self.loop).run(command)) | |
await asyncio.sleep(1) | |
if __name__ == '__main__': | |
shell = Shell() | |
shell.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment