1
0
Fork 0

Move expanded code out, hook up \x special key

This commit is contained in:
Stuart Quin 2015-01-07 10:21:40 +00:00 committed by Amjith Ramanujam
parent 49ce0d0802
commit 5684048255
3 changed files with 55 additions and 45 deletions

View File

@ -16,8 +16,9 @@ from prompt_toolkit.key_bindings.emacs import emacs_bindings
from pygments.lexers.sql import SqlLexer
from .packages.tabulate import tabulate
from .packages.expanded import expanded_table
from .packages.pgspecial import (CASE_SENSITIVE_COMMANDS,
NON_CASE_SENSITIVE_COMMANDS)
NON_CASE_SENSITIVE_COMMANDS, is_expanded_output)
from .pgcompleter import PGCompleter
from .pgtoolbar import PGToolbar
from .pgstyle import PGStyle
@ -32,47 +33,6 @@ from psycopg2 import OperationalError
_logger = logging.getLogger(__name__)
def pad(field, total, char=u" "):
return field + (char * (total - len(field)))
def get_separator(num, header_len, data_len):
total_len = header_len + data_len + 1
sep = u"-[ RECORD {0} ]".format(unicode(num))
if len(sep) < header_len:
sep = pad(sep, header_len - 1, u"-") + u"+"
if len(sep) < total_len:
sep = pad(sep, total_len, u"-")
return sep + u"\n"
def expanded_table(rows, headers):
header_len = max([len(x) for x in headers])
max_row_len = 0
results = []
padded_headers = [pad(x, header_len) + u" |" for x in headers]
header_len += 2
for row in rows:
row_len = max([len(str(x)) for x in row])
row_result = ""
if row_len > max_row_len:
max_row_len = row_len
for item in zip(padded_headers, row):
row_result += item[0] + u" " + unicode(item[1]) + u"\n"
results.append(row_result)
output = ""
for i, result in enumerate(results):
output += get_separator(i, header_len, max_row_len)
output += result
return output
@click.command()
# Default host is '' so psycopg2 can default to either localhost or unix socket
@ -190,8 +150,10 @@ def cli(database, user, host, port, prompt_passwd, never_prompt):
_logger.debug("headers: %r", headers)
_logger.debug("rows: %r", rows)
if rows:
# output.append(tabulate(rows, headers, tablefmt='fancy_grid'))
output.append(expanded_table(rows, headers))
if is_expanded_output():
output.append(expanded_table(rows, headers))
else:
output.append(tabulate(rows, headers, tablefmt='psql'))
if status: # Only print the status if it's not None.
output.append(status)
_logger.debug("status: %r", status)

View File

@ -0,0 +1,41 @@
def pad(field, total, char=u" "):
return field + (char * (total - len(field)))
def get_separator(num, header_len, data_len):
total_len = header_len + data_len + 1
sep = u"-[ RECORD {0} ]".format(unicode(num))
if len(sep) < header_len:
sep = pad(sep, header_len - 1, u"-") + u"+"
if len(sep) < total_len:
sep = pad(sep, total_len, u"-")
return sep + u"\n"
def expanded_table(rows, headers):
header_len = max([len(x) for x in headers])
max_row_len = 0
results = []
padded_headers = [pad(x, header_len) + u" |" for x in headers]
header_len += 2
for row in rows:
row_len = max([len(str(x)) for x in row])
row_result = ""
if row_len > max_row_len:
max_row_len = row_len
for item in zip(padded_headers, row):
row_result += item[0] + u" " + unicode(item[1]) + u"\n"
results.append(row_result)
output = ""
for i, result in enumerate(results):
output += get_separator(i, header_len, max_row_len)
output += result
return output

View File

@ -19,6 +19,9 @@ class MockLogging(object):
print ()
#log = MockLogging()
use_expanded_output = False
def is_expanded_output():
return use_expanded_output
def parse_special_command(sql):
command, _, arg = sql.partition(' ')
@ -768,7 +771,11 @@ def change_db(cur, arg, verbose):
raise NotImplementedError
def expanded_output(cur, arg, verbose):
import ipdb; ipdb.set_trace()
global use_expanded_output
use_expanded_output = not use_expanded_output
message = u"Expanded display is "
message += u"on" if use_expanded_output else u"off"
return [(None, None, message + u".")]
CASE_SENSITIVE_COMMANDS = {
'\?': (show_help, ['\?', 'Help on pgcli commands.']),