Skip to content

prompt_toolkit 3.0 compatibility. #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ language: python
matrix:
include:
- python: 3.6
- python: 3.5
- python: 3.4
- python: 3.3
- python: 2.7
- python: 2.6
- python: pypy
- python: pypy3
- python: 3.7

install:
- travis_retry pip install . pytest
- travis_retry pip install . pytest isort black
- pip list

script:
- echo "$TRAVIS_PYTHON_VERSION"
- ./tests/run_tests.py

# Check wheather the imports were sorted correctly.
- isort -c -rc ptpython tests setup.py examples

- black --check ptpython setup.py examples
15 changes: 10 additions & 5 deletions examples/asyncio-python-embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
prompt.
"""
from __future__ import unicode_literals
from ptpython.repl import embed

import asyncio

from ptpython.repl import embed

loop = asyncio.get_event_loop()
counter = [0]

Expand All @@ -26,7 +27,7 @@ def print_counter():
Coroutine that prints counters and saves it in a global variable.
"""
while True:
print('Counter: %i' % counter[0])
print("Counter: %i" % counter[0])
counter[0] += 1
yield from asyncio.sleep(3)

Expand All @@ -37,9 +38,13 @@ def interactive_shell():
Coroutine that starts a Python REPL from which we can access the global
counter variable.
"""
print('You should be able to read and update the "counter[0]" variable from this shell.')
print(
'You should be able to read and update the "counter[0]" variable from this shell.'
)
try:
yield from embed(globals=globals(), return_asyncio_coroutine=True, patch_stdout=True)
yield from embed(
globals=globals(), return_asyncio_coroutine=True, patch_stdout=True
)
except EOFError:
# Stop the loop when quitting the repl. (Ctrl-D press.)
loop.stop()
Expand All @@ -53,5 +58,5 @@ def main():
loop.close()


if __name__ == '__main__':
if __name__ == "__main__":
main()
16 changes: 10 additions & 6 deletions examples/asyncio-ssh-python-embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
Run this example and then SSH to localhost, port 8222.
"""
import asyncio
import asyncssh
import logging

import asyncssh

from ptpython.contrib.asyncssh_repl import ReplSSHServerSession

logging.basicConfig()
Expand All @@ -19,6 +20,7 @@ class MySSHServer(asyncssh.SSHServer):
"""
Server without authentication, running `ReplSSHServerSession`.
"""

def __init__(self, get_namespace):
self.get_namespace = get_namespace

Expand All @@ -37,22 +39,24 @@ def main(port=8222):
loop = asyncio.get_event_loop()

# Namespace exposed in the REPL.
environ = {'hello': 'world'}
environ = {"hello": "world"}

# Start SSH server.
def create_server():
return MySSHServer(lambda: environ)

print('Listening on :%i' % port)
print("Listening on :%i" % port)
print('To connect, do "ssh localhost -p %i"' % port)

loop.run_until_complete(
asyncssh.create_server(create_server, '', port,
server_host_keys=['/etc/ssh/ssh_host_dsa_key']))
asyncssh.create_server(
create_server, "", port, server_host_keys=["/etc/ssh/ssh_host_dsa_key"]
)
)

# Run eventloop.
loop.run_forever()


if __name__ == '__main__':
if __name__ == "__main__":
main()
25 changes: 11 additions & 14 deletions examples/ptpython_config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
Copy this file to $XDG_CONFIG_HOME/ptpython/config.py
"""
from __future__ import unicode_literals

from prompt_toolkit.filters import ViInsertMode
from prompt_toolkit.key_binding.key_processor import KeyPress
from prompt_toolkit.keys import Keys
from pygments.token import Token

from ptpython.layout import CompletionVisualisation

__all__ = (
'configure',
)
__all__ = ("configure",)


def configure(repl):
Expand Down Expand Up @@ -50,7 +49,7 @@ def configure(repl):

# Swap light/dark colors on or off
repl.swap_light_and_dark = False

# Highlight matching parethesis.
repl.highlight_matching_parenthesis = True

Expand All @@ -75,7 +74,7 @@ def configure(repl):
repl.paste_mode = False

# Use the classic prompt. (Display '>>>' instead of 'In [1]'.)
repl.prompt_style = 'classic' # 'classic' or 'ipython'
repl.prompt_style = "classic" # 'classic' or 'ipython'

# Don't insert a blank line after the output.
repl.insert_blank_line_after_output = False
Expand Down Expand Up @@ -108,14 +107,14 @@ def configure(repl):
repl.enable_input_validation = True

# Use this colorscheme for the code.
repl.use_code_colorscheme('pastie')
repl.use_code_colorscheme("pastie")

# Set color depth (keep in mind that not all terminals support true color).

#repl.color_depth = 'DEPTH_1_BIT' # Monochrome.
#repl.color_depth = 'DEPTH_4_BIT' # ANSI colors only.
repl.color_depth = 'DEPTH_8_BIT' # The default, 256 colors.
#repl.color_depth = 'DEPTH_24_BIT' # True color.
# repl.color_depth = 'DEPTH_1_BIT' # Monochrome.
# repl.color_depth = 'DEPTH_4_BIT' # ANSI colors only.
repl.color_depth = "DEPTH_8_BIT" # The default, 256 colors.
# repl.color_depth = 'DEPTH_24_BIT' # True color.

# Syntax.
repl.enable_syntax_highlighting = True
Expand All @@ -142,7 +141,6 @@ def _(event):
event.current_buffer.validate_and_handle()
"""


# Typing 'jj' in Vi Insert mode, should send escape. (Go back to navigation
# mode.)
"""
Expand Down Expand Up @@ -178,8 +176,7 @@ def _(event):
# `ptpython/style.py` for all possible tokens.
_custom_ui_colorscheme = {
# Blue prompt.
Token.Layout.Prompt: 'bg:#eeeeff #000000 bold',

Token.Layout.Prompt: "bg:#eeeeff #000000 bold",
# Make the status toolbar red.
Token.Toolbar.Status: 'bg:#ff0000 #000000',
Token.Toolbar.Status: "bg:#ff0000 #000000",
}
27 changes: 13 additions & 14 deletions examples/python-embed-with-custom-prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"""
from __future__ import unicode_literals

from ptpython.repl import embed
from ptpython.prompt_style import PromptStyle
from pygments.token import Token

from ptpython.prompt_style import PromptStyle
from ptpython.repl import embed


def configure(repl):
# There are several ways to override the prompt.
Expand All @@ -18,25 +19,23 @@ def configure(repl):
class CustomPrompt(PromptStyle):
def in_tokens(self, cli):
return [
(Token.In, 'Input['),
(Token.In.Number, '%s' % repl.current_statement_index),
(Token.In, '] >>: '),
(Token.In, "Input["),
(Token.In.Number, "%s" % repl.current_statement_index),
(Token.In, "] >>: "),
]

def in2_tokens(self, cli, width):
return [
(Token.In, '...: '.rjust(width)),
]
return [(Token.In, "...: ".rjust(width))]

def out_tokens(self, cli):
return [
(Token.Out, 'Result['),
(Token.Out.Number, '%s' % repl.current_statement_index),
(Token.Out, ']: '),
(Token.Out, "Result["),
(Token.Out.Number, "%s" % repl.current_statement_index),
(Token.Out, "]: "),
]

repl.all_prompt_styles['custom'] = CustomPrompt()
repl.prompt_style = 'custom'
repl.all_prompt_styles["custom"] = CustomPrompt()
repl.prompt_style = "custom"

# 2. Assign a new callable to `get_input_prompt_tokens`. This will always take effect.
## repl.get_input_prompt_tokens = lambda cli: [(Token.In, '[hello] >>> ')]
Expand All @@ -52,5 +51,5 @@ def main():
embed(globals(), locals(), configure=configure)


if __name__ == '__main__':
if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion examples/python-embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ def main():
embed(globals(), locals(), vi_mode=False)


if __name__ == '__main__':
if __name__ == "__main__":
main()
4 changes: 2 additions & 2 deletions examples/python-input.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def main():
prompt = PythonInput()

text = prompt.app.run()
print('You said: ' + text)
print("You said: " + text)


if __name__ == '__main__':
if __name__ == "__main__":
main()
1 change: 0 additions & 1 deletion ptpython/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Make `python -m ptpython` an alias for running `./ptpython`.
"""
from __future__ import unicode_literals
from .entry_points.run_ptpython import run

run()
Loading