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 ansibrightred', 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 <https://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: headers = [ utils.style_field(header_token, header, style) for header in headers ] data = ([ utils.style_field(odd_row_token if i % 2 else even_row_token, f, style) for f in r ] for i, r in enumerate(data, 1)) return iter(data), headers
def fields(): for row in data: processed = [] for field in row: if field is None and style and HAS_PYGMENTS: styled = utils.style_field(missing_value_token, missing_value, style) processed.append(styled) elif field is None: processed.append(missing_value) else: processed.append(field) yield processed