From 5438d508bebc3e3e6b5d9ec008fd1e5918d891a7 Mon Sep 17 00:00:00 2001 From: fraoustin Date: Thu, 11 Jan 2018 20:58:07 +0100 Subject: [PATCH 1/8] add color in table and token parameter --- pgcli/main.py | 33 ++++++++++++++++++++++++++++----- pgcli/pgclirc | 6 ++++++ pgcli/pgstyle.py | 10 +++++++--- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/pgcli/main.py b/pgcli/main.py index db839f57..4a10c753 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -36,7 +36,10 @@ from prompt_toolkit.layout.processors import (ConditionalProcessor, from prompt_toolkit.history import FileHistory from prompt_toolkit.auto_suggest import AutoSuggestFromHistory from pygments.lexers.sql import PostgresLexer -from pygments.token import Token +from pygments.style import Style +from pygments.styles import get_style_by_name +from pygments.token import Token, string_to_tokentype +from pygments.util import ClassNotFound from pgspecial.main import (PGSpecial, NO_QUERY) import pgspecial as special @@ -83,10 +86,10 @@ MetaQuery.__new__.__defaults__ = ('', False, 0, False, False, False, False) OutputSettings = namedtuple( 'OutputSettings', - 'table_format dcmlfmt floatfmt missingval expanded max_width case_function' + 'table_format dcmlfmt floatfmt missingval expanded max_width case_function style_output' ) OutputSettings.__new__.__defaults__ = ( - None, None, None, '', False, None, lambda x: x + None, None, None, '', False, None, lambda x: x, None ) @@ -162,6 +165,24 @@ class PGCli(object): self.decimal_format = c['data_formats']['decimal'] self.float_format = c['data_formats']['float'] + try: + own_styles = get_style_by_name(self.syntax_style).styles + except ClassNotFound: + own_styles = get_style_by_name('native').styles + for token in c['colors']: + try: + own_styles.update({string_to_tokentype( + token): own_styles[string_to_tokentype(c['colors'][token])], }) + except AttributeError as err: + own_styles.update( + {string_to_tokentype(token): c['colors'][token], }) + + class OutputStyle(Style): + default_style = "" + styles = own_styles + + self.style_output = OutputStyle + self.now = dt.datetime.today() self.completion_refresher = CompletionRefresher() @@ -704,7 +725,8 @@ class PGCli(object): case_function=( self.completer.case if self.settings['case_column_headers'] else lambda x: x - ) + ), + style_output=self.style_output ) formatted = format_output(title, cur, headers, status, settings) @@ -1041,7 +1063,8 @@ def format_output(title, cur, headers, status, settings): 'float_format': settings.floatfmt, 'preprocessors': (format_numbers, format_arrays), 'disable_numparse': True, - 'preserve_whitespace': True + 'preserve_whitespace': True, + 'style': settings.style_output } if not settings.floatfmt: output_kwargs['preprocessors'] = (align_decimals, ) diff --git a/pgcli/pgclirc b/pgcli/pgclirc index bd8dd075..8478839d 100644 --- a/pgcli/pgclirc +++ b/pgcli/pgclirc @@ -154,6 +154,12 @@ Token.Toolbar.Arg.Text = 'nobold' Token.Toolbar.Transaction.Valid = 'bg:#222222 #00ff5f bold' Token.Toolbar.Transaction.Failed = 'bg:#222222 #ff005f bold' +# color of table +# you can use token or custom colors +Token.Output.Header = "Token.Name.Tag" +Token.Output.OddRow = "" +Token.Output.EvenRow = "Token.Literal.String" + # Named queries are queries you can execute by name. [named queries] diff --git a/pgcli/pgstyle.py b/pgcli/pgstyle.py index 2ff92ed1..0a6ac393 100644 --- a/pgcli/pgstyle.py +++ b/pgcli/pgstyle.py @@ -10,8 +10,12 @@ def style_factory(name, cli_style): except ClassNotFound: style = pygments.styles.get_style_by_name('native') - custom_styles = dict([(string_to_tokentype(x), y) - for x, y in cli_style.items()]) - + custom_styles = {} + for token in cli_style: + try: + custom_styles[string_to_tokentype( + token)] = style.styles[string_to_tokentype(cli_style[token])] + except AttributeError as err: + custom_styles[string_to_tokentype(token)] = cli_style[token] return PygmentsStyle.from_defaults(style_dict=custom_styles, pygments_style_cls=style) From f5f28dd8c72b9749ab576ae6546ef0128ae1af43 Mon Sep 17 00:00:00 2001 From: fraoustin Date: Thu, 11 Jan 2018 21:40:45 +0100 Subject: [PATCH 2/8] correction of my mistake ... revert -2 --- pgcli/main.py | 33 +++++---------------------------- pgcli/pgclirc | 6 ------ pgcli/pgstyle.py | 10 +++------- 3 files changed, 8 insertions(+), 41 deletions(-) diff --git a/pgcli/main.py b/pgcli/main.py index 4a10c753..db839f57 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -36,10 +36,7 @@ from prompt_toolkit.layout.processors import (ConditionalProcessor, from prompt_toolkit.history import FileHistory from prompt_toolkit.auto_suggest import AutoSuggestFromHistory from pygments.lexers.sql import PostgresLexer -from pygments.style import Style -from pygments.styles import get_style_by_name -from pygments.token import Token, string_to_tokentype -from pygments.util import ClassNotFound +from pygments.token import Token from pgspecial.main import (PGSpecial, NO_QUERY) import pgspecial as special @@ -86,10 +83,10 @@ MetaQuery.__new__.__defaults__ = ('', False, 0, False, False, False, False) OutputSettings = namedtuple( 'OutputSettings', - 'table_format dcmlfmt floatfmt missingval expanded max_width case_function style_output' + 'table_format dcmlfmt floatfmt missingval expanded max_width case_function' ) OutputSettings.__new__.__defaults__ = ( - None, None, None, '', False, None, lambda x: x, None + None, None, None, '', False, None, lambda x: x ) @@ -165,24 +162,6 @@ class PGCli(object): self.decimal_format = c['data_formats']['decimal'] self.float_format = c['data_formats']['float'] - try: - own_styles = get_style_by_name(self.syntax_style).styles - except ClassNotFound: - own_styles = get_style_by_name('native').styles - for token in c['colors']: - try: - own_styles.update({string_to_tokentype( - token): own_styles[string_to_tokentype(c['colors'][token])], }) - except AttributeError as err: - own_styles.update( - {string_to_tokentype(token): c['colors'][token], }) - - class OutputStyle(Style): - default_style = "" - styles = own_styles - - self.style_output = OutputStyle - self.now = dt.datetime.today() self.completion_refresher = CompletionRefresher() @@ -725,8 +704,7 @@ class PGCli(object): case_function=( self.completer.case if self.settings['case_column_headers'] else lambda x: x - ), - style_output=self.style_output + ) ) formatted = format_output(title, cur, headers, status, settings) @@ -1063,8 +1041,7 @@ def format_output(title, cur, headers, status, settings): 'float_format': settings.floatfmt, 'preprocessors': (format_numbers, format_arrays), 'disable_numparse': True, - 'preserve_whitespace': True, - 'style': settings.style_output + 'preserve_whitespace': True } if not settings.floatfmt: output_kwargs['preprocessors'] = (align_decimals, ) diff --git a/pgcli/pgclirc b/pgcli/pgclirc index 8478839d..bd8dd075 100644 --- a/pgcli/pgclirc +++ b/pgcli/pgclirc @@ -154,12 +154,6 @@ Token.Toolbar.Arg.Text = 'nobold' Token.Toolbar.Transaction.Valid = 'bg:#222222 #00ff5f bold' Token.Toolbar.Transaction.Failed = 'bg:#222222 #ff005f bold' -# color of table -# you can use token or custom colors -Token.Output.Header = "Token.Name.Tag" -Token.Output.OddRow = "" -Token.Output.EvenRow = "Token.Literal.String" - # Named queries are queries you can execute by name. [named queries] diff --git a/pgcli/pgstyle.py b/pgcli/pgstyle.py index 0a6ac393..2ff92ed1 100644 --- a/pgcli/pgstyle.py +++ b/pgcli/pgstyle.py @@ -10,12 +10,8 @@ def style_factory(name, cli_style): except ClassNotFound: style = pygments.styles.get_style_by_name('native') - custom_styles = {} - for token in cli_style: - try: - custom_styles[string_to_tokentype( - token)] = style.styles[string_to_tokentype(cli_style[token])] - except AttributeError as err: - custom_styles[string_to_tokentype(token)] = cli_style[token] + custom_styles = dict([(string_to_tokentype(x), y) + for x, y in cli_style.items()]) + return PygmentsStyle.from_defaults(style_dict=custom_styles, pygments_style_cls=style) From 4e75e35262f0eb2337ee9f77007e440e0115c404 Mon Sep 17 00:00:00 2001 From: fraoustin Date: Thu, 11 Jan 2018 21:47:56 +0100 Subject: [PATCH 3/8] add color for table --- changelog.rst | 4 +++- pgcli/main.py | 33 ++++++++++++++++++++++++++++----- pgcli/pgclirc | 6 ++++++ pgcli/pgstyle.py | 9 +++++++-- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/changelog.rst b/changelog.rst index 5358c8ea..15d7b419 100644 --- a/changelog.rst +++ b/changelog.rst @@ -4,7 +4,9 @@ Upcoming Features: --------- -* Add support for `\T` command to change format output. (Thanks: `Frederic Aoustin`_). +* Add support for `\T` command to change format output. (Thanks: `Frederic Aoustin`_) +* Add support color for table. (Thanks: `Frederic Aoustin`_) +* For color, you can use name of token. (Thanks: `Frederic Aoustin`_) 1.8.2 ===== diff --git a/pgcli/main.py b/pgcli/main.py index db839f57..4a10c753 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -36,7 +36,10 @@ from prompt_toolkit.layout.processors import (ConditionalProcessor, from prompt_toolkit.history import FileHistory from prompt_toolkit.auto_suggest import AutoSuggestFromHistory from pygments.lexers.sql import PostgresLexer -from pygments.token import Token +from pygments.style import Style +from pygments.styles import get_style_by_name +from pygments.token import Token, string_to_tokentype +from pygments.util import ClassNotFound from pgspecial.main import (PGSpecial, NO_QUERY) import pgspecial as special @@ -83,10 +86,10 @@ MetaQuery.__new__.__defaults__ = ('', False, 0, False, False, False, False) OutputSettings = namedtuple( 'OutputSettings', - 'table_format dcmlfmt floatfmt missingval expanded max_width case_function' + 'table_format dcmlfmt floatfmt missingval expanded max_width case_function style_output' ) OutputSettings.__new__.__defaults__ = ( - None, None, None, '', False, None, lambda x: x + None, None, None, '', False, None, lambda x: x, None ) @@ -162,6 +165,24 @@ class PGCli(object): self.decimal_format = c['data_formats']['decimal'] self.float_format = c['data_formats']['float'] + try: + own_styles = get_style_by_name(self.syntax_style).styles + except ClassNotFound: + own_styles = get_style_by_name('native').styles + for token in c['colors']: + try: + own_styles.update({string_to_tokentype( + token): own_styles[string_to_tokentype(c['colors'][token])], }) + except AttributeError as err: + own_styles.update( + {string_to_tokentype(token): c['colors'][token], }) + + class OutputStyle(Style): + default_style = "" + styles = own_styles + + self.style_output = OutputStyle + self.now = dt.datetime.today() self.completion_refresher = CompletionRefresher() @@ -704,7 +725,8 @@ class PGCli(object): case_function=( self.completer.case if self.settings['case_column_headers'] else lambda x: x - ) + ), + style_output=self.style_output ) formatted = format_output(title, cur, headers, status, settings) @@ -1041,7 +1063,8 @@ def format_output(title, cur, headers, status, settings): 'float_format': settings.floatfmt, 'preprocessors': (format_numbers, format_arrays), 'disable_numparse': True, - 'preserve_whitespace': True + 'preserve_whitespace': True, + 'style': settings.style_output } if not settings.floatfmt: output_kwargs['preprocessors'] = (align_decimals, ) diff --git a/pgcli/pgclirc b/pgcli/pgclirc index bd8dd075..8478839d 100644 --- a/pgcli/pgclirc +++ b/pgcli/pgclirc @@ -154,6 +154,12 @@ Token.Toolbar.Arg.Text = 'nobold' Token.Toolbar.Transaction.Valid = 'bg:#222222 #00ff5f bold' Token.Toolbar.Transaction.Failed = 'bg:#222222 #ff005f bold' +# color of table +# you can use token or custom colors +Token.Output.Header = "Token.Name.Tag" +Token.Output.OddRow = "" +Token.Output.EvenRow = "Token.Literal.String" + # Named queries are queries you can execute by name. [named queries] diff --git a/pgcli/pgstyle.py b/pgcli/pgstyle.py index 2ff92ed1..0f473412 100644 --- a/pgcli/pgstyle.py +++ b/pgcli/pgstyle.py @@ -10,8 +10,13 @@ def style_factory(name, cli_style): except ClassNotFound: style = pygments.styles.get_style_by_name('native') - custom_styles = dict([(string_to_tokentype(x), y) - for x, y in cli_style.items()]) + custom_styles = {} + for token in cli_style: + try: + custom_styles[string_to_tokentype( + token)] = style.styles[string_to_tokentype(cli_style[token])] + except AttributeError as err: + custom_styles[string_to_tokentype(token)] = cli_style[token] return PygmentsStyle.from_defaults(style_dict=custom_styles, pygments_style_cls=style) From ddcf2c9621807a63d05311734cbba74806e88afe Mon Sep 17 00:00:00 2001 From: fraoustin Date: Tue, 16 Jan 2018 20:24:30 +0100 Subject: [PATCH 4/8] load travis From ade6142ef8f27e50fc2fe55bbd2436a97f561eed Mon Sep 17 00:00:00 2001 From: fraoustin Date: Thu, 11 Jan 2018 21:47:56 +0100 Subject: [PATCH 5/8] add color for table --- pgcli/main.py | 33 ++++++++++++++++++++++++++++----- pgcli/pgclirc | 6 ++++++ pgcli/pgstyle.py | 9 +++++++-- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/pgcli/main.py b/pgcli/main.py index d2e63fec..2591f7f9 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -36,7 +36,10 @@ from prompt_toolkit.layout.processors import (ConditionalProcessor, from prompt_toolkit.history import FileHistory from prompt_toolkit.auto_suggest import AutoSuggestFromHistory from pygments.lexers.sql import PostgresLexer -from pygments.token import Token +from pygments.style import Style +from pygments.styles import get_style_by_name +from pygments.token import Token, string_to_tokentype +from pygments.util import ClassNotFound from pgspecial.main import (PGSpecial, NO_QUERY, PAGER_OFF) import pgspecial as special @@ -83,10 +86,10 @@ MetaQuery.__new__.__defaults__ = ('', False, 0, False, False, False, False) OutputSettings = namedtuple( 'OutputSettings', - 'table_format dcmlfmt floatfmt missingval expanded max_width case_function' + 'table_format dcmlfmt floatfmt missingval expanded max_width case_function style_output' ) OutputSettings.__new__.__defaults__ = ( - None, None, None, '', False, None, lambda x: x + None, None, None, '', False, None, lambda x: x, None ) @@ -164,6 +167,24 @@ class PGCli(object): self.pgspecial.pset_pager(self.config['main'].as_bool( 'enable_pager') and "on" or "off") + + try: + own_styles = get_style_by_name(self.syntax_style).styles + except ClassNotFound: + own_styles = get_style_by_name('native').styles + for token in c['colors']: + try: + own_styles.update({string_to_tokentype( + token): own_styles[string_to_tokentype(c['colors'][token])], }) + except AttributeError as err: + own_styles.update( + {string_to_tokentype(token): c['colors'][token], }) + + class OutputStyle(Style): + default_style = "" + styles = own_styles + + self.style_output = OutputStyle self.now = dt.datetime.today() @@ -707,7 +728,8 @@ class PGCli(object): case_function=( self.completer.case if self.settings['case_column_headers'] else lambda x: x - ) + ), + style_output=self.style_output ) formatted = format_output(title, cur, headers, status, settings) @@ -1063,7 +1085,8 @@ def format_output(title, cur, headers, status, settings): 'float_format': settings.floatfmt, 'preprocessors': (format_numbers, format_arrays), 'disable_numparse': True, - 'preserve_whitespace': True + 'preserve_whitespace': True, + 'style': settings.style_output } if not settings.floatfmt: output_kwargs['preprocessors'] = (align_decimals, ) diff --git a/pgcli/pgclirc b/pgcli/pgclirc index 91a01a1b..41b970b9 100644 --- a/pgcli/pgclirc +++ b/pgcli/pgclirc @@ -157,6 +157,12 @@ Token.Toolbar.Arg.Text = 'nobold' Token.Toolbar.Transaction.Valid = 'bg:#222222 #00ff5f bold' Token.Toolbar.Transaction.Failed = 'bg:#222222 #ff005f bold' +# color of table +# you can use token or custom colors +Token.Output.Header = "Token.Name.Tag" +Token.Output.OddRow = "" +Token.Output.EvenRow = "Token.Literal.String" + # Named queries are queries you can execute by name. [named queries] diff --git a/pgcli/pgstyle.py b/pgcli/pgstyle.py index 2ff92ed1..0f473412 100644 --- a/pgcli/pgstyle.py +++ b/pgcli/pgstyle.py @@ -10,8 +10,13 @@ def style_factory(name, cli_style): except ClassNotFound: style = pygments.styles.get_style_by_name('native') - custom_styles = dict([(string_to_tokentype(x), y) - for x, y in cli_style.items()]) + custom_styles = {} + for token in cli_style: + try: + custom_styles[string_to_tokentype( + token)] = style.styles[string_to_tokentype(cli_style[token])] + except AttributeError as err: + custom_styles[string_to_tokentype(token)] = cli_style[token] return PygmentsStyle.from_defaults(style_dict=custom_styles, pygments_style_cls=style) From 88b12c1e2c2363ead3746bccb299a9715fe21901 Mon Sep 17 00:00:00 2001 From: fraoustin Date: Sun, 4 Mar 2018 11:41:10 +0100 Subject: [PATCH 6/8] move style_factory_output in pgstyle --- pgcli/main.py | 26 ++++---------------------- pgcli/pgstyle.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/pgcli/main.py b/pgcli/main.py index 2591f7f9..2f3de4f3 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -36,16 +36,13 @@ from prompt_toolkit.layout.processors import (ConditionalProcessor, from prompt_toolkit.history import FileHistory from prompt_toolkit.auto_suggest import AutoSuggestFromHistory from pygments.lexers.sql import PostgresLexer -from pygments.style import Style -from pygments.styles import get_style_by_name -from pygments.token import Token, string_to_tokentype -from pygments.util import ClassNotFound +from pygments.token import Token from pgspecial.main import (PGSpecial, NO_QUERY, PAGER_OFF) import pgspecial as special from .pgcompleter import PGCompleter from .pgtoolbar import create_toolbar_tokens_func -from .pgstyle import style_factory +from .pgstyle import style_factory, style_factory_output from .pgexecute import PGExecute from .pgbuffer import PGBuffer from .completion_refresher import CompletionRefresher @@ -167,24 +164,9 @@ class PGCli(object): self.pgspecial.pset_pager(self.config['main'].as_bool( 'enable_pager') and "on" or "off") - - try: - own_styles = get_style_by_name(self.syntax_style).styles - except ClassNotFound: - own_styles = get_style_by_name('native').styles - for token in c['colors']: - try: - own_styles.update({string_to_tokentype( - token): own_styles[string_to_tokentype(c['colors'][token])], }) - except AttributeError as err: - own_styles.update( - {string_to_tokentype(token): c['colors'][token], }) - class OutputStyle(Style): - default_style = "" - styles = own_styles - - self.style_output = OutputStyle + self.style_output = style_factory_output( + self.syntax_style, c['colors']) self.now = dt.datetime.today() diff --git a/pgcli/pgstyle.py b/pgcli/pgstyle.py index 0f473412..54ae1bbe 100644 --- a/pgcli/pgstyle.py +++ b/pgcli/pgstyle.py @@ -3,6 +3,7 @@ from pygments.util import ClassNotFound from prompt_toolkit.styles import PygmentsStyle import pygments.styles +from pygments.style import Style def style_factory(name, cli_style): try: @@ -20,3 +21,24 @@ def style_factory(name, cli_style): return PygmentsStyle.from_defaults(style_dict=custom_styles, pygments_style_cls=style) + + +def style_factory_output(name, cli_style): + try: + style = pygments.styles.get_style_by_name(name).styles + except ClassNotFound: + style = pygments.styles.get_style_by_name('native').styles + + for token in cli_style: + try: + style.update({string_to_tokentype( + token): style[string_to_tokentype(cli_style[token])], }) + except AttributeError as err: + style.update( + {string_to_tokentype(token): cli_style[token], }) + + class OutputStyle(pygments.style.Style): + default_style = "" + styles = style + + return OutputStyle From 344317df34c1ebf5be4876f08240b3296dc8e487 Mon Sep 17 00:00:00 2001 From: fraoustin Date: Fri, 23 Mar 2018 23:24:35 +0100 Subject: [PATCH 7/8] explicit sample in pgclirc --- pgcli/pgclirc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pgcli/pgclirc b/pgcli/pgclirc index 41b970b9..1ff1a1b8 100644 --- a/pgcli/pgclirc +++ b/pgcli/pgclirc @@ -159,9 +159,9 @@ Token.Toolbar.Transaction.Failed = 'bg:#222222 #ff005f bold' # color of table # you can use token or custom colors -Token.Output.Header = "Token.Name.Tag" +Token.Output.Header = "#00ff5f bold" Token.Output.OddRow = "" -Token.Output.EvenRow = "Token.Literal.String" +Token.Output.EvenRow = "" # Named queries are queries you can execute by name. [named queries] From 7563f514af66ffecc62818588557610b3ae3c933 Mon Sep 17 00:00:00 2001 From: fraoustin Date: Thu, 29 Mar 2018 18:58:43 +0200 Subject: [PATCH 8/8] add feature in changelog --- changelog.rst | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/changelog.rst b/changelog.rst index c462ee79..8ee1cc3d 100644 --- a/changelog.rst +++ b/changelog.rst @@ -1,7 +1,22 @@ Upcoming: ========= -TODO +Features: +--------- + +* Change ``\h`` format string in prompt to only return the first part of the hostname, + up to the first '.' character. Add ``\H`` that returns the entire hostname (#858). + (Thanks: `Andrew Kuchling`_) +* Add Color of table by parameter. The color of table is function of syntax style + +Internal changes: +----------------- + +* Add tests, AUTHORS and changelog.rst to release. (Thanks: `Dick Marinus`_) + +Bug Fixes: +---------- +* Fix broken pgcli --list command line option (#850). (Thanks: `Dmitry B`_) 1.9.0 ===== @@ -728,6 +743,7 @@ Improvements: * Integration tests with Postgres!! (Thanks: https://github.com/macobo) .. _`Amjith Ramanujam`: https://github.com/amjith +.. _`Andrew Kuchling`: https://github.com/akuchling .. _`Darik Gamble`: https://github.com/darikg .. _`Daniel Rocco`: https://github.com/drocco007 .. _`Jay Zeng`: https://github.com/jayzeng