mirror of https://github.com/dbcli/pgcli
Add a toolbar and keybinding F2 for smart completion.
This commit is contained in:
parent
d292e68d31
commit
764b5d5a8f
|
@ -0,0 +1,15 @@
|
|||
from prompt_toolkit.keys import Keys
|
||||
|
||||
def pgcli_bindings(registry, cli_ref):
|
||||
"""
|
||||
Custom key bindings for pgcli.
|
||||
"""
|
||||
line = cli_ref().line
|
||||
handle = registry.add_binding
|
||||
|
||||
@handle(Keys.F2)
|
||||
def _(event):
|
||||
"""
|
||||
Enable/Disable SmartCompletion Mode.
|
||||
"""
|
||||
line.completer.smart_completion = not line.completer.smart_completion
|
|
@ -11,16 +11,18 @@ 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
|
||||
import sqlparse
|
||||
|
||||
from .packages.tabulate import tabulate
|
||||
from .packages.pgspecial import COMMANDS
|
||||
from .pgcompleter import PGCompleter
|
||||
from .pgtoolbar import PGToolbar
|
||||
from .pgstyle import PGStyle
|
||||
from .pgexecute import PGExecute
|
||||
from .pgline import PGLine
|
||||
from .config import write_default_config, load_config
|
||||
from .key_bindings import pgcli_bindings
|
||||
|
||||
@click.command()
|
||||
@click.option('-h', '--host', default='localhost')
|
||||
|
@ -40,23 +42,27 @@ def cli(database, user, password, host, port):
|
|||
|
||||
# Load config.
|
||||
config = load_config('~/.pgclirc')
|
||||
smart_completion = config.getboolean('main', 'smart_completion')
|
||||
|
||||
# Connect to the database.
|
||||
try:
|
||||
pgexecute = PGExecute(database, user, password, host, port)
|
||||
except Exception as e:
|
||||
except Exception as e: # Connecting to a database could fail.
|
||||
click.secho(e.message, err=True, fg='red')
|
||||
exit(1)
|
||||
layout = Layout(before_input=DefaultPrompt('%s> ' % pgexecute.dbname),
|
||||
menus=[CompletionsMenu()],
|
||||
lexer=SqlLexer)
|
||||
completer = PGCompleter(config.getboolean('main', 'smart_completion'))
|
||||
lexer=SqlLexer,
|
||||
bottom_toolbars=[
|
||||
PGToolbar()])
|
||||
completer = PGCompleter(smart_completion)
|
||||
completer.extend_special_commands(COMMANDS.keys())
|
||||
completer.extend_table_names(pgexecute.tables())
|
||||
completer.extend_column_names(pgexecute.all_columns())
|
||||
line = PGLine(completer=completer,
|
||||
history=FileHistory(os.path.expanduser('~/.pgcli-history')))
|
||||
cli = CommandLineInterface(style=PGStyle, layout=layout, line=line)
|
||||
cli = CommandLineInterface(style=PGStyle, layout=layout, line=line,
|
||||
key_binding_factories=[emacs_bindings, pgcli_bindings])
|
||||
|
||||
try:
|
||||
while True:
|
||||
|
|
|
@ -11,5 +11,9 @@ class PGStyle(Style):
|
|||
Token.SelectedText: '#ffffff bg:#6666aa',
|
||||
Token.IncrementalSearchMatch: '#ffffff bg:#4444aa',
|
||||
Token.IncrementalSearchMatch.Current: '#ffffff bg:#44aa44',
|
||||
Token.Toolbar: 'bg:#440044 #ffffff',
|
||||
Token.Toolbar.Status: 'bg:#222222 #aaaaaa',
|
||||
Token.Toolbar.Status.Off: 'bg:#222222 #888888',
|
||||
Token.Toolbar.Status.On: 'bg:#222222 #ffffff',
|
||||
}
|
||||
styles.update(DefaultStyle.styles)
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
from prompt_toolkit.layout.toolbars import Toolbar
|
||||
from prompt_toolkit.layout.utils import TokenList
|
||||
from pygments.token import Token
|
||||
|
||||
class PGToolbar(Toolbar):
|
||||
def __init__(self, token=None):
|
||||
token = token or Token.Toolbar.Status
|
||||
super(self.__class__, self).__init__(token=token)
|
||||
|
||||
def get_tokens(self, cli, width):
|
||||
result = TokenList()
|
||||
result.append((self.token, ' '))
|
||||
if cli.line.completer.smart_completion:
|
||||
result.append((self.token.On, '[F2] Smart Completion (on)'))
|
||||
else:
|
||||
result.append((self.token.Off, '[F2] Smart Completion (off)'))
|
||||
|
||||
#result.append((self.token, ' ' * (width - len(result))))
|
||||
return result
|
Loading…
Reference in New Issue