Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/autocorrection.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def _(event):

# Read input.
print('Say something')
code_obj = get_input(key_bindings_registry=key_bindings_manager.registry)
print('You said: %s' % code_obj)
text = get_input(key_bindings_registry=key_bindings_manager.registry)
print('You said: %s' % text)


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions examples/clock-input.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def on_read_start():
# instance.
def run():
# Send every second a redraw request.
while cli.is_reading_input:
while cli.eventloop is not None:
time.sleep(1)
cli.request_redraw()

cli.run_in_executor(run)
cli.eventloop.run_in_executor(run)
cli.onReadInputStart += on_read_start

code_obj = cli.read_input()
Expand Down
9 changes: 3 additions & 6 deletions examples/custom-key-binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""
Example of adding a custom key binding to a prompt.
"""
from prompt_toolkit import CommandLineInterface
from prompt_toolkit.contrib.shortcuts import get_input
from prompt_toolkit.key_binding.manager import KeyBindingManager
from prompt_toolkit.keys import Keys

Expand Down Expand Up @@ -32,13 +32,10 @@ def _(event):
"""
event.cli.current_buffer.insert_text('z')

# Create a CLI with the key bindings registry of this manager.
cli = CommandLineInterface(key_bindings_registry=key_bindings_manager.registry)

# Read input.
print('Press F4 to insert "hello world", type "xy" to insert "z":')
code_obj = cli.read_input()
print('You said: %s' % code_obj.text)
text = get_input(key_bindings_registry=key_bindings_manager.registry)
print('You said: %s' % text)


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions examples/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from prompt_toolkit.layout.processors import AfterInput
from prompt_toolkit.layout.menus import CompletionsMenu
from prompt_toolkit.completion import Completion, Completer
from prompt_toolkit.filters import AlwaysOn
from prompt_toolkit.filters import Always

from pygments.style import Style
from pygments.styles.default import DefaultStyle
Expand Down Expand Up @@ -84,7 +84,7 @@ def main():
Window(width=D.exact(1),
content=FillControl('|', token=Token.Line)),
Window(content=BufferControl(lexer=PythonLexer,
show_line_numbers=AlwaysOn(),
show_line_numbers=Always(),
input_processors=[
DefaultPrompt('python> '),
AfterInput.static(' <python', token=Token.AfterInput),
Expand Down
58 changes: 58 additions & 0 deletions examples/telnet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python
from __future__ import unicode_literals

from prompt_toolkit.contrib.completers import WordCompleter
from prompt_toolkit.contrib.shortcuts import create_cli
from prompt_toolkit.contrib.telnet.application import TelnetApplication
from prompt_toolkit.contrib.telnet.server import TelnetServer

from pygments.lexers import HtmlLexer
from pygments.style import Style
from pygments.token import Token

import logging

# Set up logging
logging.basicConfig()
logging.getLogger().setLevel(logging.INFO)


class AnimalStyle(Style):
styles = {
Token.Menu.Completions.Completion.Current: 'bg:#00aaaa #000000',
Token.Menu.Completions.Completion: 'bg:#008888 #ffffff',
Token.Menu.Completions.ProgressButton: 'bg:#003333',
Token.Menu.Completions.ProgressBar: 'bg:#00aaaa',
}


class ExampleApplication(TelnetApplication):
def create_cli(self, telnet_connection):
"""
Return the new CommandLineInterface to be used for an incoming
connection.
"""
animal_completer = WordCompleter([ 'alligator', 'ant',])
return create_cli(message='Say something: ', lexer=HtmlLexer, style=AnimalStyle, completer=animal_completer)

def client_connected(self, telnet_connection):
# When a client is connected, erase the screen from the client and say
# Hello.
telnet_connection.vt100_output.erase_screen()
telnet_connection.vt100_output.cursor_goto(0, 0)
telnet_connection.send('Welcome!\n')

def handle_command(self, telnet_connection, document):
# When the client enters a command, just reply.
if document.text == 'exit':
telnet_connection.close()
else:
telnet_connection.send('You said: %s\n\n' % document.text)

def client_leaving(self, telnet_connection):
# Say 'bye' when the client quits.
telnet_connection.send('Bye.\n')


if __name__ == '__main__':
TelnetServer(application=ExampleApplication(), port=2323).run()
4 changes: 2 additions & 2 deletions prompt_toolkit/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .utils import EventHook
from .validation import ValidationError
from .clipboard import ClipboardData
from .filters import AlwaysOn
from .filters import Always

import os
import six
Expand Down Expand Up @@ -110,7 +110,7 @@ class Buffer(object):
returns either True or False,
"""
def __init__(self, completer=None, history=None, validator=None, tempfile_suffix='',
is_multiline=None, initial_document=None, focussable=AlwaysOn(), returnable=AlwaysOn()):
is_multiline=None, initial_document=None, focussable=Always(), returnable=Always()):
assert is_multiline is None or callable(is_multiline) or isinstance(is_multiline, bool)

self.completer = completer
Expand Down
25 changes: 19 additions & 6 deletions prompt_toolkit/contrib/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
"""
from __future__ import unicode_literals

from prompt_toolkit import CommandLineInterface, AbortAction
from prompt_toolkit.buffer import Buffer
from prompt_toolkit.filters import IsDone, HasFocus
from prompt_toolkit.history import History
from prompt_toolkit.interface import CommandLineInterface, AbortAction, AcceptAction
from prompt_toolkit.key_binding.manager import KeyBindingManager
from prompt_toolkit.layout import Window, HSplit, FloatContainer, Float
from prompt_toolkit.layout.controls import BufferControl, TokenListControl
Expand Down Expand Up @@ -109,8 +109,14 @@ def create_cli(message='',
style=None,
history=None,
get_bottom_toolbar_tokens=None,
key_bindings_registry=None):

key_bindings_registry=None,
output=None,
on_abort=AbortAction.RAISE_EXCEPTION,
on_exit=AbortAction.RAISE_EXCEPTION,
on_accept=AcceptAction.RETURN_DOCUMENT):
"""
Create a `CommandLineInterface` instance.
"""
# Create history instance.
if history is None:
history = History()
Expand All @@ -134,12 +140,16 @@ def create_cli(message='',
completer=completer,
),
key_bindings_registry=key_bindings_registry,
style=style)
style=style,
output=output,
on_abort=on_abort,
on_exit=on_exit)


def get_input(message='',
on_abort=AbortAction.RAISE_EXCEPTION,
on_exit=AbortAction.RAISE_EXCEPTION,
on_accept=AcceptAction.RETURN_DOCUMENT,
multiline=False,
is_password=False,
vi_mode=False,
Expand Down Expand Up @@ -192,10 +202,13 @@ def get_input(message='',
style=style,
history=history,
get_bottom_toolbar_tokens=get_bottom_toolbar_tokens,
key_bindings_registry=key_bindings_registry)
key_bindings_registry=key_bindings_registry,
on_abort=on_abort,
on_exit=on_exit,
on_accept=on_accept)

# Read input and return it.
document = cli.read_input(on_abort=on_abort, on_exit=on_exit)
document = cli.read_input()

if document:
return document.text
Empty file.
43 changes: 43 additions & 0 deletions prompt_toolkit/contrib/telnet/application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
Interface for Telnet applications.
"""
from __future__ import unicode_literals
from abc import ABCMeta, abstractmethod
from six import with_metaclass

__all__ = (
'TelnetApplication',
)


class TelnetApplication(with_metaclass(ABCMeta, object)):
"""
The interface which has to be implemented for any telnet application.
An instance of this class has to be passed to `TelnetServer`.
"""
@abstractmethod
def create_cli(self, telnet_connection):
"""
Return a new CommandLineInterface instance from here. This method is
called for every new connection.

Hint: Use the following shortcut: `prompt_toolkit.shortcuts.create_cli`
"""

@abstractmethod
def client_connected(self, telnet_connection):
"""
Called when a new client was connected.
"""

@abstractmethod
def handle_command(self, telnet_connection, document):
"""
Called when the user accepted input on the command line.
"""

@abstractmethod
def client_leaving(self, telnet_connection):
"""
Called when a client quits.
"""
11 changes: 11 additions & 0 deletions prompt_toolkit/contrib/telnet/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""
Python logger for the telnet server.
"""
from __future__ import unicode_literals
import logging

logger = logging.getLogger(__package__)

__all__ = (
'logger',
)
Loading