Skip to content
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

[WIP] Upgrade to prompt_toolkit 2.0 #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
76 changes: 33 additions & 43 deletions haxor_news/haxor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@
import sys

import click
from prompt_toolkit import AbortAction, Application, CommandLineInterface
from prompt_toolkit.filters import Always
from prompt_toolkit.interface import AcceptAction
from prompt_toolkit import Application
from prompt_toolkit.buffer import Buffer
from prompt_toolkit.shortcuts import create_default_layout, create_eventloop
from prompt_toolkit.history import FileHistory
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.shortcuts.prompt import PromptSession

from .__init__ import __version__
from .completer import Completer
Expand All @@ -41,8 +39,8 @@
class Haxor(object):
"""Encapsulate the Hacker News CLI.

:type cli: :class:`prompt_toolkit.CommandLineInterface`
:param cli: An instance of `prompt_toolkit.CommandLineInterface`.
:type prompt: :class:`prompt_toolkit.shortcuts.prompt.PromptSession`
:param prompt: An instance of `prompt_toolkit.shortcuts.prompt.PromptSession`.

:type CMDS_ENABLE_PAGINATE: list (const)
:param CMDS_ENABLE_PAGINATE: A list of commands that kick off pagination.
Expand Down Expand Up @@ -99,7 +97,7 @@ class Haxor(object):
PAGINATE_CMD_WIN = ' | more'

def __init__(self):
self.cli = None
self.prompt = None
self.key_manager = None
self.theme = 'vim'
self.paginate_comments = True
Expand Down Expand Up @@ -137,33 +135,24 @@ def _create_cli(self):
"""Create the prompt_toolkit's CommandLineInterface."""
history = FileHistory(os.path.expanduser('~/.haxornewshistory'))
toolbar = Toolbar(lambda: self.paginate_comments)
layout = create_default_layout(
self.key_manager = self._create_key_manager()
style_factory = StyleFactory(self.theme)
self.prompt = PromptSession(
message=u'haxor> ',
reserve_space_for_menu=8,
get_bottom_toolbar_tokens=toolbar.handler,
)
cli_buffer = Buffer(
bottom_toolbar=toolbar.handler,
mouse_support=False,
style=style_factory.style,
history=history,
auto_suggest=AutoSuggestFromHistory(),
enable_history_search=True,
completer=self.completer,
complete_while_typing=Always(),
accept_action=AcceptAction.RETURN_DOCUMENT)
self.key_manager = self._create_key_manager()
style_factory = StyleFactory(self.theme)
application = Application(
mouse_support=False,
style=style_factory.style,
layout=layout,
buffer=cli_buffer,
key_bindings_registry=self.key_manager.manager.registry,
on_exit=AbortAction.RAISE_EXCEPTION,
on_abort=AbortAction.RETRY,
ignore_case=True)
eventloop = create_eventloop()
self.cli = CommandLineInterface(
application=application,
eventloop=eventloop)
complete_while_typing=True,

enable_open_in_editor=True,
enable_system_prompt=True,
enable_suspend=True,
key_bindings=self.key_manager.key_bindings)

def _add_comment_pagination(self, document_text):
"""Add the command to enable comment pagination where applicable.
Expand All @@ -185,24 +174,19 @@ def _add_comment_pagination(self, document_text):
document_text += self.PAGINATE_CMD
return document_text

def handle_exit(self, document):
"""Exits if the user typed exit or quit

:type document: :class:`prompt_toolkit.document.Document`
:param document: An instance of `prompt_toolkit.document.Document`.
def handle_exit(self, text):
"""
if document.text in ('exit', 'quit'):
Exits if the user typed exit or quit
"""
if text in ('exit', 'quit'):
sys.exit()

def run_command(self, document):
"""Run the given command.

:type document: :class:`prompt_toolkit.document.Document`
:param document: An instance of `prompt_toolkit.document.Document`.
def run_command(self, text):
"""
Run the given command.
"""
try:
if self.paginate_comments:
text = document.text
text = self._add_comment_pagination(text)
subprocess.call(text, shell=True)
except Exception as e:
Expand All @@ -213,6 +197,12 @@ def run_cli(self):
click.echo('Version: ' + __version__)
click.echo('Syntax: hn <command> [params] [options]')
while True:
document = self.cli.run(reset_current_buffer=True)
self.handle_exit(document)
self.run_command(document)
try:
text = self.prompt.prompt()
except KeyboardInterrupt:
continue # Control-C was pressed.
except EOFError:
break

self.handle_exit(text)
self.run_command(text)
28 changes: 11 additions & 17 deletions haxor_news/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# language governing permissions and limitations under the License.

from __future__ import print_function
from prompt_toolkit.key_binding.manager import KeyBindingManager
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.keys import Keys


Expand All @@ -24,14 +24,12 @@ class KeyManager(object):
Handle togging of:
* Comment pagination.

:type manager: :class:`prompt_toolkit.key_binding.manager.
KeyBindingManager`
:param manager: An instance of `prompt_toolkit.key_binding.manager.
KeyBindingManager`.
:type key_bindings: :class:`prompt_toolkit.key_binding.KeyBindings`
:param key_bindings: An instance of `prompt_toolkit.key_binding.KeyBindings`
"""

def __init__(self, set_paginate_comments, get_paginate_comments):
self.manager = None
self.key_bindings = None
self._create_key_manager(set_paginate_comments, get_paginate_comments)

def _create_key_manager(self, set_paginate_comments, get_paginate_comments):
Expand All @@ -50,13 +48,9 @@ def _create_key_manager(self, set_paginate_comments, get_paginate_comments):
"""
assert callable(set_paginate_comments)
assert callable(get_paginate_comments)
self.manager = KeyBindingManager(
enable_search=True,
enable_abort_and_exit_bindings=True,
enable_system_bindings=True,
enable_auto_suggest_bindings=True)
self.key_bindings = KeyBindings()

@self.manager.registry.add_binding(Keys.F2)
@self.key_bindings.add('f2')
def handle_f2(_):
"""Enable/Disable paginate comments mode.

Expand All @@ -70,18 +64,18 @@ def handle_f2(_):
# set_paginate_comments(not get_paginate_comments())
pass

@self.manager.registry.add_binding(Keys.F10)
def handle_f10(_):
@self.key_bindings.add('f10')
def handle_f10(event):
"""Quit when the `F10` key is pressed.

:type _: :class:`prompt_toolkit.Event`
:param _: (Unused)

:raises: :class:`EOFError` to quit the app.
"""
raise EOFError
event.app.exit()

@self.manager.registry.add_binding(Keys.ControlSpace)
@self.key_bindings.add('c-space')
def handle_ctrl_space(event):
"""Initialize autocompletion at the cursor.

Expand All @@ -97,4 +91,4 @@ def handle_ctrl_space(event):
if b.complete_state:
b.complete_next()
else:
event.cli.start_completion(select_first=False)
b.start_completion(select_first=False)
7 changes: 2 additions & 5 deletions haxor_news/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@

def cli():
"""Creates and calls Haxor."""
try:
haxor = Haxor()
haxor.run_cli()
except (EOFError, KeyboardInterrupt):
haxor.cli.set_return_value(None)
haxor = Haxor()
haxor.run_cli()


if __name__ == "__main__":
Expand Down
51 changes: 25 additions & 26 deletions haxor_news/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

from pygments.token import Token
from pygments.util import ClassNotFound
from prompt_toolkit.styles import default_style_extensions, style_from_dict
from prompt_toolkit.styles.pygments import style_from_pygments_dict, style_from_pygments_cls
from prompt_toolkit.styles import merge_styles
import pygments.styles


Expand All @@ -41,31 +42,29 @@ def style_factory(self, name):
:return: An instance of `pygments.style.StyleMeta`.
"""
try:
style = pygments.styles.get_style_by_name(name)
pygments_style = pygments.styles.get_style_by_name(name)
except ClassNotFound:
style = pygments.styles.get_style_by_name('native')
pygments_style = pygments.styles.get_style_by_name('native')

# Create styles dictionary.
styles = {}
styles.update(style.styles)
styles.update(default_style_extensions)
styles.update({
Token.Menu.Completions.Completion.Current: 'bg:#00aaaa #000000',
Token.Menu.Completions.Completion: 'bg:#008888 #ffffff',
Token.Menu.Completions.Meta.Current: 'bg:#00aaaa #000000',
Token.Menu.Completions.Meta: 'bg:#00aaaa #ffffff',
Token.Menu.Completions.ProgressButton: 'bg:#003333',
Token.Menu.Completions.ProgressBar: 'bg:#00aaaa',
Token.Scrollbar: 'bg:#00aaaa',
Token.Scrollbar.Button: 'bg:#003333',
Token.Toolbar: 'bg:#222222 #cccccc',
Token.Toolbar.Off: 'bg:#222222 #696969',
Token.Toolbar.On: 'bg:#222222 #ffffff',
Token.Toolbar.Search: 'noinherit bold',
Token.Toolbar.Search.Text: 'nobold',
Token.Toolbar.System: 'noinherit bold',
Token.Toolbar.Arg: 'noinherit bold',
Token.Toolbar.Arg.Text: 'nobold'
})

return style_from_dict(styles)
return merge_styles([
style_from_pygments_cls(pygments_style),
style_from_pygments_dict({
Token.Menu.Completions.Completion.Current: 'bg:#00aaaa #000000',
Token.Menu.Completions.Completion: 'bg:#008888 #ffffff',
Token.Menu.Completions.Meta.Current: 'bg:#00aaaa #000000',
Token.Menu.Completions.Meta: 'bg:#00aaaa #ffffff',
Token.Menu.Completions.ProgressButton: 'bg:#003333',
Token.Menu.Completions.ProgressBar: 'bg:#00aaaa',
Token.Scrollbar: 'bg:#00aaaa',
Token.Scrollbar.Button: 'bg:#003333',
Token.Toolbar: 'bg:#222222 #cccccc',
Token.Toolbar.Off: 'bg:#222222 #696969',
Token.Toolbar.On: 'bg:#222222 #ffffff',
Token.Toolbar.Search: 'noinherit bold',
Token.Toolbar.Search.Text: 'nobold',
Token.Toolbar.System: 'noinherit bold',
Token.Toolbar.Arg: 'noinherit bold',
Token.Toolbar.Arg.Text: 'nobold'
})
])
7 changes: 4 additions & 3 deletions haxor_news/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from __future__ import print_function
from pygments.token import Token
from prompt_toolkit.formatted_text import PygmentsTokens


class Toolbar(object):
Expand All @@ -38,7 +39,7 @@ def _create_toolbar_handler(self, paginate_comments_cfg):
"""
assert callable(paginate_comments_cfg)

def get_toolbar_items(_):
def get_toolbar_items():
"""Return the toolbar items.

:type _: :class:`prompt_toolkit.Cli`
Expand All @@ -53,10 +54,10 @@ def get_toolbar_items(_):
# else:
# paginate_comments_token = Token.Toolbar.Off
# paginate_comments = 'OFF'
return [
return PygmentsTokens([
# (paginate_comments_token,
# ' [F2] Paginate Comments: {0} '.format(paginate_comments)),
(Token.Toolbar, ' [F10] Exit ')
]
])

return get_toolbar_items
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'colorama>=0.3.3,<1.0.0',
'requests>=2.4.3,<3.0.0',
'pygments>=2.0.2,<3.0.0',
'prompt-toolkit>=1.0.0,<1.1.0',
'prompt-toolkit>=2.0.0,<2.1.0',
'six>=1.9.0,<2.0.0',
],
extras_require={
Expand Down