diff options
Diffstat (limited to 'examples/telnet')
-rwxr-xr-x | examples/telnet/chat-app.py | 103 | ||||
-rwxr-xr-x | examples/telnet/dialog.py | 34 | ||||
-rwxr-xr-x | examples/telnet/hello-world.py | 39 | ||||
-rwxr-xr-x | examples/telnet/toolbar.py | 44 |
4 files changed, 220 insertions, 0 deletions
diff --git a/examples/telnet/chat-app.py b/examples/telnet/chat-app.py new file mode 100755 index 0000000..2e3508d --- /dev/null +++ b/examples/telnet/chat-app.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python +""" +A simple chat application over telnet. +Everyone that connects is asked for his name, and then people can chat with +each other. +""" +import logging +import random +from asyncio import Future, run + +from prompt_toolkit.contrib.telnet.server import TelnetServer +from prompt_toolkit.formatted_text import HTML +from prompt_toolkit.shortcuts import PromptSession, clear + +# Set up logging +logging.basicConfig() +logging.getLogger().setLevel(logging.INFO) + +# List of connections. +_connections = [] +_connection_to_color = {} + + +COLORS = [ + "ansired", + "ansigreen", + "ansiyellow", + "ansiblue", + "ansifuchsia", + "ansiturquoise", + "ansilightgray", + "ansidarkgray", + "ansidarkred", + "ansidarkgreen", + "ansibrown", + "ansidarkblue", + "ansipurple", + "ansiteal", +] + + +async def interact(connection): + write = connection.send + prompt_session = PromptSession() + + # When a client is connected, erase the screen from the client and say + # Hello. + clear() + write("Welcome to our chat application!\n") + write("All connected clients will receive what you say.\n") + + name = await prompt_session.prompt_async(message="Type your name: ") + + # Random color. + color = random.choice(COLORS) + _connection_to_color[connection] = color + + # Send 'connected' message. + _send_to_everyone(connection, name, "(connected)", color) + + # Prompt. + prompt_msg = HTML('<reverse fg="{}">[{}]</reverse> > ').format(color, name) + + _connections.append(connection) + try: + # Set Application. + while True: + try: + result = await prompt_session.prompt_async(message=prompt_msg) + _send_to_everyone(connection, name, result, color) + except KeyboardInterrupt: + pass + except EOFError: + _send_to_everyone(connection, name, "(leaving)", color) + finally: + _connections.remove(connection) + + +def _send_to_everyone(sender_connection, name, message, color): + """ + Send a message to all the clients. + """ + for c in _connections: + if c != sender_connection: + c.send_above_prompt( + [ + ("fg:" + color, "[%s]" % name), + ("", " "), + ("fg:" + color, "%s\n" % message), + ] + ) + + +async def main(): + server = TelnetServer(interact=interact, port=2323) + server.start() + + # Run forever. + await Future() + + +if __name__ == "__main__": + run(main()) diff --git a/examples/telnet/dialog.py b/examples/telnet/dialog.py new file mode 100755 index 0000000..c674a9d --- /dev/null +++ b/examples/telnet/dialog.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +""" +Example of a telnet application that displays a dialog window. +""" +import logging +from asyncio import Future, run + +from prompt_toolkit.contrib.telnet.server import TelnetServer +from prompt_toolkit.shortcuts.dialogs import yes_no_dialog + +# Set up logging +logging.basicConfig() +logging.getLogger().setLevel(logging.INFO) + + +async def interact(connection): + result = await yes_no_dialog( + title="Yes/no dialog demo", text="Press yes or no" + ).run_async() + + connection.send(f"You said: {result}\n") + connection.send("Bye.\n") + + +async def main(): + server = TelnetServer(interact=interact, port=2323) + server.start() + + # Run forever. + await Future() + + +if __name__ == "__main__": + run(main()) diff --git a/examples/telnet/hello-world.py b/examples/telnet/hello-world.py new file mode 100755 index 0000000..c19c60c --- /dev/null +++ b/examples/telnet/hello-world.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python +""" +A simple Telnet application that asks for input and responds. + +The interaction function is a prompt_toolkit coroutine. +Also see the `hello-world-asyncio.py` example which uses an asyncio coroutine. +That is probably the preferred way if you only need Python 3 support. +""" +import logging +from asyncio import run + +from prompt_toolkit.contrib.telnet.server import TelnetServer +from prompt_toolkit.shortcuts import PromptSession, clear + +# Set up logging +logging.basicConfig() +logging.getLogger().setLevel(logging.INFO) + + +async def interact(connection): + clear() + connection.send("Welcome!\n") + + # Ask for input. + session = PromptSession() + result = await session.prompt_async(message="Say something: ") + + # Send output. + connection.send(f"You said: {result}\n") + connection.send("Bye.\n") + + +async def main(): + server = TelnetServer(interact=interact, port=2323) + await server.run() + + +if __name__ == "__main__": + run(main()) diff --git a/examples/telnet/toolbar.py b/examples/telnet/toolbar.py new file mode 100755 index 0000000..d6ae886 --- /dev/null +++ b/examples/telnet/toolbar.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python +""" +Example of a telnet application that displays a bottom toolbar and completions +in the prompt. +""" +import logging +from asyncio import run + +from prompt_toolkit.completion import WordCompleter +from prompt_toolkit.contrib.telnet.server import TelnetServer +from prompt_toolkit.shortcuts import PromptSession + +# Set up logging +logging.basicConfig() +logging.getLogger().setLevel(logging.INFO) + + +async def interact(connection): + # When a client is connected, erase the screen from the client and say + # Hello. + connection.send("Welcome!\n") + + # Display prompt with bottom toolbar. + animal_completer = WordCompleter(["alligator", "ant"]) + + def get_toolbar(): + return "Bottom toolbar..." + + session = PromptSession() + result = await session.prompt_async( + "Say something: ", bottom_toolbar=get_toolbar, completer=animal_completer + ) + + connection.send(f"You said: {result}\n") + connection.send("Bye.\n") + + +async def main(): + server = TelnetServer(interact=interact, port=2323) + await server.run() + + +if __name__ == "__main__": + run(main()) |