def style_output(data, headers, style=None, header_token='Token.Output.Header', odd_row_token='Token.Output.OddRow', even_row_token='Token.Output.EvenRow', **_): """Style the *data* and *headers* (e.g. bold, italic, and colors) .. NOTE:: This requires the `Pygments <http://pygments.org/>`_ library to be installed. You can install it with CLI Helpers as an extra:: $ pip install cli_helpers[styles] Example usage:: from cli_helpers.tabular_output.preprocessors import style_output from pygments.style import Style from pygments.token import Token class YourStyle(Style): default_style = "" styles = { Token.Output.Header: 'bold #ansired', Token.Output.OddRow: 'bg:#eee #111', Token.Output.EvenRow: '#0f0' } headers = ('First Name', 'Last Name') data = [['Fred', 'Roberts'], ['George', 'Smith']] data, headers = style_output(data, headers, style=YourStyle) :param iterable data: An :term:`iterable` (e.g. list) of rows. :param iterable headers: The column headers. :param str/pygments.style.Style style: A Pygments style. You can `create your own styles <http://pygments.org/docs/styles/#creating-own-styles>`_. :param str header_token: The token type to be used for the headers. :param str odd_row_token: The token type to be used for odd rows. :param str even_row_token: The token type to be used for even rows. :return: The styled data and headers. :rtype: tuple """ if style and HAS_PYGMENTS: formatter = Terminal256Formatter(style=style) def style_field(token, field): """Get the styled text for a *field* using *token* type.""" s = StringIO() formatter.format(((token, field), ), s) return s.getvalue() headers = [style_field(header_token, header) for header in headers] data = [[ style_field(odd_row_token if i % 2 else even_row_token, f) for f in r ] for i, r in enumerate(data, 1)] return data, headers
def style_output(data, headers, style=None, table_separator_token='Token.Output.TableSeparator', **_): """Style the *table* (e.g. bold, italic, and colors) .. NOTE:: This requires the `Pygments <http://pygments.org/>`_ library to be installed. You can install it with CLI Helpers as an extra:: $ pip install cli_helpers[styles] Example usage:: from cli_helpers.tabular_output import terminaltables_adapter from pygments.style import Style from pygments.token import Token class YourStyle(Style): default_style = "" styles = { Token.Output.TableSeparator: '#ansigray' } headers = ('First Name', 'Last Name') data = [['Fred', 'Roberts'], ['George', 'Smith']] style_output_table = terminaltables_adapter.style_output_table('psql') style_output_table(data, headers, style=CliStyle) output = terminaltables_adapter.adapter(data, headers, style=YourStyle) :param iterable data: An :term:`iterable` (e.g. list) of rows. :param iterable headers: The column headers. :param str/pygments.style.Style style: A Pygments style. You can `create your own styles <https://pygments.org/docs/styles#creating-own-styles>`_. :param str table_separator_token: The token type to be used for the table separator. :return: data and headers. :rtype: tuple """ if style and HAS_PYGMENTS and format_name in supported_formats: formatter = Terminal256Formatter(style=style) def style_field(token, field): """Get the styled text for a *field* using *token* type.""" s = StringIO() formatter.format(((token, field), ), s) return s.getvalue() clss = table_format_handler[format_name] for char in [ char for char in terminaltables.base_table.BaseTable.__dict__ if char.startswith("CHAR_") ]: setattr( clss, char, style_field(table_separator_token, getattr(clss, char))) return iter(data), headers
def style_output(data, headers, style=None, table_separator_token='Token.Output.TableSeparator', **_): """Style the *table* a(e.g. bold, italic, and colors) .. NOTE:: This requires the `Pygments <http://pygments.org/>`_ library to be installed. You can install it with CLI Helpers as an extra:: $ pip install cli_helpers[styles] Example usage:: from cli_helpers.tabular_output import tabulate_adapter from pygments.style import Style from pygments.token import Token class YourStyle(Style): default_style = "" styles = { Token.Output.TableSeparator: '#ansigray' } headers = ('First Name', 'Last Name') data = [['Fred', 'Roberts'], ['George', 'Smith']] style_output_table = tabulate_adapter.style_output_table('psql') style_output_table(data, headers, style=CliStyle) data, headers = style_output(data, headers, style=YourStyle) output = tabulate_adapter.adapter(data, headers, style=YourStyle) :param iterable data: An :term:`iterable` (e.g. list) of rows. :param iterable headers: The column headers. :param str/pygments.style.Style style: A Pygments style. You can `create your own styles <https://pygments.org/docs/styles#creating-own-styles>`_. :param str table_separator_token: The token type to be used for the table separator. :return: data and headers. :rtype: tuple """ if style and HAS_PYGMENTS and format_name in supported_table_formats: formatter = Terminal256Formatter(style=style) def style_field(token, field): """Get the styled text for a *field* using *token* type.""" s = StringIO() formatter.format(((token, field), ), s) return s.getvalue() def addColorInElt(elt): if not elt: return elt if elt.__class__ == tabulate.Line: return tabulate.Line( *(style_field(table_separator_token, val) for val in elt)) if elt.__class__ == tabulate.DataRow: return tabulate.DataRow( *(style_field(table_separator_token, val) for val in elt)) return elt srcfmt = tabulate._table_formats[format_name] newfmt = tabulate.TableFormat(*(addColorInElt(val) for val in srcfmt)) tabulate._table_formats[format_name] = newfmt return iter(data), headers
def _get_formatter(style) -> Terminal256Formatter: return Terminal256Formatter(style=style)
def style_field(token, field, style): """Get the styled text for a *field* using *token* type.""" formatter = Terminal256Formatter(style=style) s = StringIO() formatter.format(((token, field), ), s) return s.getvalue()