From 798c398b60a3ad2acfe04e2cc1d1585cfa817c2c Mon Sep 17 00:00:00 2001 From: Amjith Ramanujam Date: Tue, 27 Jan 2015 23:05:00 -0800 Subject: [PATCH] Add table formats and timing to config file. --- TODO | 5 ++--- pgcli/main.py | 15 +++++++++------ pgcli/packages/pgspecial.py | 18 ++++-------------- pgcli/pgclirc | 8 ++++++++ 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/TODO b/TODO index f2de0540..00ff5130 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,6 @@ # vi: ft=vimwiki * [ ] Add coverage. * [ ] Refactor to sqlcompletion to consume the text from left to right and use a state machine to suggest cols or tables instead of relying on hacks. -* [ ] Refactor to sqlcompletion to consume the text from left to right and use a state machine to suggest cols or tables instead of relying on hacks. * [ ] Add a few more special commands. (\l pattern, \dp, \ds, \dy, \z etc) * [ ] Refactor pgspecial.py to a class. * [ ] Show/hide docs for a statement using a keybinding. @@ -15,5 +14,5 @@ * [ ] pgexecute columns(), tables() etc can be just cursors instead of fetchall() * [ ] Add unicode tests. * [ ] Add colorschemes in config file. -* [ ] Add \timing in config file. -* [ ] Add table format to config file. +* [X] Add \timing in config file. +* [X] Add table format to config file. diff --git a/pgcli/main.py b/pgcli/main.py index 22207567..093bfca0 100755 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -18,7 +18,8 @@ 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, is_expanded_output, is_timing_enabled) + NON_CASE_SENSITIVE_COMMANDS, is_expanded_output) +import pgcli.packages.pgspecial as pgspecial from .pgcompleter import PGCompleter from .pgtoolbar import PGToolbar from .pgstyle import PGStyle @@ -59,6 +60,8 @@ class PGCli(object): # Load config. c = self.config = load_config('~/.pgclirc', default_config) self.multi_line = c.getboolean('main', 'multi_line') + pgspecial.TIMING_ENABLED = c.getboolean('main', 'timing') + self.table_format = c.get('main', 'table_format') self.logger = logging.getLogger(__name__) self.initialize_logging() @@ -213,7 +216,8 @@ class PGCli(object): if not click.confirm('Do you want to continue?'): click.secho("Aborted!", err=True, fg='red') break - output.extend(format_output(cur, headers, status)) + output.extend(format_output(cur, headers, status, + self.table_format)) end = time() total += end - start mutating = mutating or is_mutating(status) @@ -234,8 +238,7 @@ class PGCli(object): click.secho(str(e), err=True, fg='red') else: click.echo_via_pager('\n'.join(output)) - - if is_timing_enabled(): + if pgspecial.TIMING_ENABLED: print('Command Time:', duration) print('Format Time:', total) finally: @@ -323,13 +326,13 @@ def cli(database, user, host, port, prompt_passwd, never_prompt, dbname, pgcli.run_cli() -def format_output(cur, headers, status): +def format_output(cur, headers, status, table_format): output = [] if cur: if is_expanded_output(): output.append(expanded_table(cur, headers)) else: - output.append(tabulate(cur, headers, tablefmt='psql')) + output.append(tabulate(cur, headers, tablefmt=table_format)) if status: # Only print the status if it's not None. output.append(status) return output diff --git a/pgcli/packages/pgspecial.py b/pgcli/packages/pgspecial.py index 8d48a519..0de26029 100644 --- a/pgcli/packages/pgspecial.py +++ b/pgcli/packages/pgspecial.py @@ -11,21 +11,11 @@ TableInfo = namedtuple("TableInfo", ['checks', 'relkind', 'hasindex', log = logging.getLogger(__name__) -class MockLogging(object): - def debug(self, string): - print ("***** Query ******") - print (string) - print ("******************") - print () - -#log = MockLogging() use_expanded_output = False def is_expanded_output(): return use_expanded_output -timing_enabled = False -def is_timing_enabled(): - return timing_enabled +TIMING_ENABLED = False def parse_special_command(sql): command, _, arg = sql.partition(' ') @@ -783,10 +773,10 @@ def expanded_output(cur, arg, verbose): return [(None, None, message)] def toggle_timing(cur, arg, verbose): - global timing_enabled - timing_enabled = not timing_enabled + global TIMING_ENABLED + TIMING_ENABLED = not TIMING_ENABLED message = "Timing is " - message += "on." if timing_enabled else "off." + message += "on." if TIMING_ENABLED else "off." return [(None, None, message)] CASE_SENSITIVE_COMMANDS = { diff --git a/pgcli/pgclirc b/pgcli/pgclirc index 6696c1a5..32cb9f6b 100644 --- a/pgcli/pgclirc +++ b/pgcli/pgclirc @@ -17,3 +17,11 @@ log_file = ~/.pgcli.log # Default log level. Possible values: "CRITICAL", "ERROR", "WARNING", "INFO" # and "DEBUG". log_level = INFO + +# Timing of sql statments and table rendering. +timing = True + +# Table format. Possible values: psql, plain, simple, grid, fancy_grid, pipe, +# orgtbl, rst, mediawiki, html, latex, latex_booktabs. +# Recommended: psql, fancy_grid and grid. +table_format = psql