2014-10-12 17:31:54 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
from __future__ import unicode_literals
|
2014-10-12 22:07:34 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
import click
|
|
|
|
import psycopg2
|
2014-10-12 17:31:54 +00:00
|
|
|
|
2014-10-12 17:45:35 +00:00
|
|
|
from prompt_toolkit import CommandLineInterface, AbortAction, Exit
|
2014-10-12 17:31:54 +00:00
|
|
|
from prompt_toolkit.completion import Completer, Completion
|
|
|
|
from prompt_toolkit.line import Line
|
|
|
|
from prompt_toolkit.layout import Layout
|
|
|
|
from prompt_toolkit.layout.prompt import DefaultPrompt
|
|
|
|
from prompt_toolkit.layout.menus import CompletionMenu
|
|
|
|
|
|
|
|
from pygments.token import Token
|
|
|
|
from pygments.style import Style
|
|
|
|
from pygments.lexers.sql import SqlLexer
|
2014-10-12 17:38:57 +00:00
|
|
|
from pygments.styles.default import DefaultStyle
|
2014-10-12 17:31:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SqlCompleter(Completer):
|
|
|
|
keywords = [
|
|
|
|
'SELECT',
|
|
|
|
'INSERT',
|
|
|
|
'ALTER',
|
|
|
|
'DROP',
|
|
|
|
'DELETE',
|
|
|
|
'FROM',
|
|
|
|
'WHERE',
|
|
|
|
]
|
|
|
|
|
|
|
|
def get_completions(self, document):
|
|
|
|
word_before_cursor = document.get_word_before_cursor()
|
|
|
|
|
|
|
|
for keyword in self.keywords:
|
|
|
|
if (keyword.startswith(word_before_cursor) or
|
|
|
|
keyword.startswith(word_before_cursor.upper())):
|
|
|
|
yield Completion(keyword, -len(word_before_cursor))
|
|
|
|
|
|
|
|
|
2014-10-12 17:38:57 +00:00
|
|
|
class DocumentStyle(Style):
|
2014-10-12 17:31:54 +00:00
|
|
|
styles = {
|
2014-10-12 17:38:57 +00:00
|
|
|
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',
|
|
|
|
Token.SelectedText: '#ffffff bg:#6666aa',
|
|
|
|
Token.IncrementalSearchMatch: '#ffffff bg:#4444aa',
|
|
|
|
Token.IncrementalSearchMatch.Current: '#ffffff bg:#44aa44',
|
|
|
|
}
|
|
|
|
styles.update(DefaultStyle.styles)
|
2014-10-12 17:31:54 +00:00
|
|
|
|
|
|
|
|
2014-10-12 22:07:34 +00:00
|
|
|
@click.command()
|
|
|
|
def pgcli():
|
2014-10-12 17:45:35 +00:00
|
|
|
layout = Layout(before_input=DefaultPrompt('> '),
|
|
|
|
menus=[CompletionMenu()],
|
|
|
|
lexer=SqlLexer)
|
|
|
|
line = Line(completer=SqlCompleter())
|
|
|
|
cli = CommandLineInterface(style=DocumentStyle, layout=layout, line=line)
|
2014-10-12 17:31:54 +00:00
|
|
|
|
2014-10-12 17:45:35 +00:00
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
document = cli.read_input(on_exit=AbortAction.RAISE_EXCEPTION)
|
|
|
|
print 'You entered:', document.text
|
|
|
|
except Exit:
|
|
|
|
print 'GoodBye!'
|
2014-10-12 17:31:54 +00:00
|
|
|
|
|
|
|
|