mirror of https://github.com/dbcli/pgcli
Merge pull request #89 from macobo/karl-update-keybindings
Support CTRL+arrow shortcuts, bump prompt_toolkit to 0.26
This commit is contained in:
commit
447c54ea74
|
@ -1,14 +1,18 @@
|
|||
import logging
|
||||
from prompt_toolkit import filters
|
||||
from prompt_toolkit.keys import Keys
|
||||
from prompt_toolkit.enums import InputMode
|
||||
from prompt_toolkit.key_binding.registry import Registry
|
||||
from prompt_toolkit.key_binding.bindings.emacs import load_emacs_bindings
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
def pgcli_bindings(registry, cli_ref):
|
||||
def pgcli_bindings():
|
||||
"""
|
||||
Custom key bindings for pgcli.
|
||||
"""
|
||||
line = cli_ref().line
|
||||
registry = Registry()
|
||||
load_emacs_bindings(registry)
|
||||
|
||||
handle = registry.add_binding
|
||||
|
||||
@handle(Keys.F2)
|
||||
|
@ -17,7 +21,8 @@ def pgcli_bindings(registry, cli_ref):
|
|||
Enable/Disable SmartCompletion Mode.
|
||||
"""
|
||||
_logger.debug('Detected F2 key.')
|
||||
line.completer.smart_completion = not line.completer.smart_completion
|
||||
buf = event.cli.current_buffer
|
||||
buf.completer.smart_completion = not buf.completer.smart_completion
|
||||
|
||||
@handle(Keys.F3)
|
||||
def _(event):
|
||||
|
@ -25,12 +30,15 @@ def pgcli_bindings(registry, cli_ref):
|
|||
Enable/Disable Multiline Mode.
|
||||
"""
|
||||
_logger.debug('Detected F3 key.')
|
||||
line.always_multiline = not line.always_multiline
|
||||
buf = event.cli.current_buffer
|
||||
buf.always_multiline = not buf.always_multiline
|
||||
|
||||
@handle(Keys.ControlSpace, in_mode=InputMode.INSERT)
|
||||
@handle(Keys.ControlSpace, filter=~filters.HasSelection())
|
||||
def _(event):
|
||||
"""
|
||||
Force autocompletion at cursor.
|
||||
"""
|
||||
_logger.debug('Detected <C-Space> key.')
|
||||
line.complete_next()
|
||||
event.cli.current_buffer.complete_next()
|
||||
|
||||
return registry
|
||||
|
|
|
@ -12,7 +12,6 @@ from prompt_toolkit.layout import Layout
|
|||
from prompt_toolkit.layout.prompt import DefaultPrompt
|
||||
from prompt_toolkit.layout.menus import CompletionsMenu
|
||||
from prompt_toolkit.history import FileHistory
|
||||
from prompt_toolkit.key_bindings.emacs import emacs_bindings
|
||||
from pygments.lexers.sql import SqlLexer
|
||||
|
||||
from .packages.tabulate import tabulate
|
||||
|
@ -23,7 +22,7 @@ from .pgcompleter import PGCompleter
|
|||
from .pgtoolbar import PGToolbar
|
||||
from .pgstyle import PGStyle
|
||||
from .pgexecute import PGExecute
|
||||
from .pgline import PGLine
|
||||
from .pgbuffer import PGBuffer
|
||||
from .config import write_default_config, load_config
|
||||
from .key_bindings import pgcli_bindings
|
||||
|
||||
|
@ -130,10 +129,10 @@ def cli(database, user, host, port, prompt_passwd, never_prompt):
|
|||
completer.extend_special_commands(CASE_SENSITIVE_COMMANDS.keys())
|
||||
completer.extend_special_commands(NON_CASE_SENSITIVE_COMMANDS.keys())
|
||||
refresh_completions(pgexecute, completer)
|
||||
line = PGLine(always_multiline=multi_line, completer=completer,
|
||||
buf = PGBuffer(always_multiline=multi_line, completer=completer,
|
||||
history=FileHistory(os.path.expanduser('~/.pgcli-history')))
|
||||
cli = CommandLineInterface(style=PGStyle, layout=layout, line=line,
|
||||
key_binding_factories=[emacs_bindings, pgcli_bindings])
|
||||
cli = CommandLineInterface(style=PGStyle, layout=layout, buffer=buf,
|
||||
key_bindings_registry=pgcli_bindings())
|
||||
|
||||
try:
|
||||
while True:
|
||||
|
|
|
@ -1,26 +1,10 @@
|
|||
from prompt_toolkit.line import Line
|
||||
from prompt_toolkit.buffer import Buffer
|
||||
|
||||
class PGLine(Line):
|
||||
class PGBuffer(Buffer):
|
||||
def __init__(self, always_multiline, *args, **kwargs):
|
||||
self.always_multiline = always_multiline
|
||||
super(self.__class__, self).__init__(*args, **kwargs)
|
||||
|
||||
@property
|
||||
def is_multiline(self):
|
||||
"""
|
||||
Dynamically determine whether we're in multiline mode.
|
||||
"""
|
||||
if not self.always_multiline:
|
||||
return False
|
||||
|
||||
if self.always_multiline and _multiline_exception(self.text):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
@is_multiline.setter
|
||||
def is_multiline(self, value):
|
||||
pass
|
||||
is_multiline = lambda doc: self.always_multiline and not _multiline_exception(doc.text)
|
||||
super(self.__class__, self).__init__(*args, is_multiline=is_multiline, **kwargs)
|
||||
|
||||
def _multiline_exception(text):
|
||||
text = text.strip()
|
|
@ -10,17 +10,17 @@ class PGToolbar(Toolbar):
|
|||
def get_tokens(self, cli, width):
|
||||
result = TokenList()
|
||||
result.append((self.token, ' '))
|
||||
if cli.line.completer.smart_completion:
|
||||
if cli.current_buffer.completer.smart_completion:
|
||||
result.append((self.token.On, '[F2] Smart Completion: ON '))
|
||||
else:
|
||||
result.append((self.token.Off, '[F2] Smart Completion: OFF '))
|
||||
|
||||
if cli.line.always_multiline:
|
||||
if cli.current_buffer.always_multiline:
|
||||
result.append((self.token.On, '[F3] Multiline: ON'))
|
||||
else:
|
||||
result.append((self.token.Off, '[F3] Multiline: OFF'))
|
||||
|
||||
if cli.line.always_multiline:
|
||||
if cli.current_buffer.always_multiline:
|
||||
result.append((self.token,
|
||||
' (Semi-colon [;] will end the line)'))
|
||||
|
||||
|
|
4
setup.py
4
setup.py
|
@ -25,8 +25,8 @@ setup(
|
|||
install_requires=[
|
||||
'click >= 3.2',
|
||||
'Pygments >= 2.0', # Pygments has to be Capitalcased. WTF?
|
||||
'jedi == 0.8.1', # Temporary fix for installation woes.
|
||||
'prompt_toolkit==0.25', # Need to pin this to 0.25 since APIs change quite a bit after this.
|
||||
'jedi == 0.8.1', # Temporary fix for installation woes.
|
||||
'prompt_toolkit==0.26',
|
||||
'psycopg2 >= 2.5.4',
|
||||
'sqlparse >= 0.1.14',
|
||||
],
|
||||
|
|
Loading…
Reference in New Issue