summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIrina Truong <i.chernyavska@gmail.com>2018-03-31 13:20:28 -0700
committerGitHub <noreply@github.com>2018-03-31 13:20:28 -0700
commit0feffcb7789df57b44fd1824d648d73689fe7c1c (patch)
treec634be367c413412e3f1e64f4d26b24b96b7a893
parente20d4754fab0b4aac7e797ef4e87327093447161 (diff)
parentdd0c721ad583777328d3654f561f8d7902c11d00 (diff)
Merge pull request #834 from dbcli/fraoustin/colors_of_table
Add feature Color of table.
-rw-r--r--changelog.rst2
-rw-r--r--pgcli/main.py15
-rw-r--r--pgcli/pgclirc6
-rw-r--r--pgcli/pgstyle.py31
4 files changed, 46 insertions, 8 deletions
diff --git a/changelog.rst b/changelog.rst
index 10ad2089..8ee1cc3d 100644
--- a/changelog.rst
+++ b/changelog.rst
@@ -7,7 +7,7 @@ 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:
-----------------
diff --git a/pgcli/main.py b/pgcli/main.py
index 254cdba1..523bbf31 100644
--- a/pgcli/main.py
+++ b/pgcli/main.py
@@ -42,7 +42,7 @@ 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
@@ -83,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'
+ 'table_format dcmlfmt floatfmt missingval expanded max_width case_function style_output'
)
OutputSettings.__new__.__defaults__ = (
- None, None, None, '<null>', False, None, lambda x: x
+ None, None, None, '<null>', False, None, lambda x: x, None
)
@@ -165,6 +165,9 @@ class PGCli(object):
self.pgspecial.pset_pager(self.config['main'].as_bool(
'enable_pager') and "on" or "off")
+ self.style_output = style_factory_output(
+ self.syntax_style, c['colors'])
+
self.now = dt.datetime.today()
self.completion_refresher = CompletionRefresher()
@@ -707,7 +710,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)
@@ -1066,7 +1070,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 f6e4d8d9..70f10ee7 100644
--- a/pgcli/pgclirc
+++ b/pgcli/pgclirc
@@ -158,6 +158,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 = "#00ff5f bold"
+Token.Output.OddRow = ""
+Token.Output.EvenRow = ""
+
# Named queries are queries you can execute by name.
[named queries]
diff --git a/pgcli/pgstyle.py b/pgcli/pgstyle.py
index 2ff92ed1..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:
@@ -10,8 +11,34 @@ 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)
+
+
+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