1
0
Fork 0

Add pretty printing of table data.

This commit is contained in:
Amjith Ramanujam 2014-11-23 23:30:17 -08:00
parent 4fa728e929
commit d3f88eb65c
4 changed files with 8 additions and 4 deletions

2
TODO
View File

@ -1,4 +1,4 @@
* [ ] Prettify the output into nice columnar tables.
* [X] Prettify the output into nice columnar tables.
* [ ] Add a lot more SQL keywords to auto-completion.
* [ ] Add context sensitive auto-completion.
* Check how sqlpython is doing it.

View File

@ -16,7 +16,8 @@ from prompt_toolkit.layout.prompt import DefaultPrompt
from prompt_toolkit.layout.menus import CompletionsMenu
from prompt_toolkit.history import FileHistory
from pygments.lexers import SqlLexer
from pygments.lexers.sql import SqlLexer
from tabulate import tabulate
@click.command()
@click.option('-h', '--host', default='localhost')
@ -45,7 +46,8 @@ def pgcli(database, user, password, host, port):
while True:
document = cli.read_input(on_exit=AbortAction.RAISE_EXCEPTION)
try:
print(pgexecute.run(document.text))
print(tabulate(*pgexecute.run(document.text),
tablefmt='orgtbl'))
except Exception as e:
click.secho("Does not compute!", fg='red')
click.secho(e.message, err=True, fg='red')

View File

@ -21,7 +21,8 @@ class PGExecute(object):
cur.execute(self.special_commands[sql])
else:
cur.execute(sql)
return cur.fetchall()
headers = [x[0] for x in cur.description]
return cur.fetchall(), headers
def tables(self):
with self.conn.cursor() as cur:

View File

@ -13,6 +13,7 @@ setup(
'Click',
'prompt_toolkit',
'psycopg2',
'tabulate',
],
entry_points='''
[console_scripts]