def __init__(self, tags: Dict[str, str] = None, always_reset: bool = True, strict: bool = True, tag_sep: str = '<>', ansistring_cls: type = None, default_line_length: int = 120, default_first_row_bg_color: str = "#E5E5E5", default_second_row_bg_color: str = "#D5D5D5") -> None: """ Constructur :param tags: List of named tags as dictionary :param always_reset: Reset every markup at the end of a single print command :param strict: Within a print command, every opening tags needs a corresponding closing tag :param tag_sep: Seperator of tags, e.g. "{}" if you want print_with_colors(text="{red}Hello World{/red}") to print a red text. :param ansistring_cls: A class for ansistring :param default_line_length: Default maximal line length for printing a line with background color. Every line has the background color defined by print_with_bg_color(...) or print_for_row(...) up to the specified line length. Can be overwritten by a parameter of the aformentioned methods. :param default_first_row_bg_color: A default background-color for the i-th block of rows printed by method print_for_row(...). Default is a light grey. A color name (red, green, blue, ...) or hex-value, e.g. #00AAFF :param default_second_row_bg_color: A default background-color for the (i+1)-th block of rows printed by method print_for_rows(...). Default is a light grey being a bit darker than default_first_row_bg_color. A color name (red, green, blue, ...) or hex-value, e.g. #00AAFF """ self.__tag_open = tag_sep[0] self.__tag_close = tag_sep[1] self.__default_first_row_bg_color = default_first_row_bg_color self.__default_second_row_bg_color = default_second_row_bg_color self.__default_line_length = default_line_length self.__am = ansimarkup.AnsiMarkup(tags=tags, always_reset=always_reset, strict=strict, tag_sep=tag_sep, ansistring_cls=ansistring_cls)
def make_ansimarkup(color): color = ansimarkup.parse(color) custom_markup = dict(level=color, lvl=color) am = ansimarkup.AnsiMarkup(tags=custom_markup, strict=True) return am
import ansimarkup import click import colorama from dicttoxml import dicttoxml from jinja2 import Environment, PackageLoader JINJA2_ENV = Environment(loader=PackageLoader("greynoise.cli")) colorama.init() ANSI_MARKUP = ansimarkup.AnsiMarkup( tags={ "header": ansimarkup.parse("<bold>"), "key": ansimarkup.parse("<blue>"), "value": ansimarkup.parse("<green>"), "noise": ansimarkup.parse("<light-yellow>"), "not-noise": ansimarkup.parse("<dim>"), "malicious": ansimarkup.parse("<light-red>"), "unknown": ansimarkup.parse("<dim>"), "benign": ansimarkup.parse("<light-green>"), }) def colored_output(function): """Decorator that converts ansi markup into ansi escape sequences. :param function: Function that will return text using ansi markup. :type function: callable :returns: Wrapped function that converts markup into escape sequences. :rtype: callable
import ansimarkup import click import colorama from jinja2 import Environment, PackageLoader JINJA2_ENV = Environment(loader=PackageLoader("sublime.cli"), extensions=['jinja2.ext.loopcontrols']) colorama.init() ANSI_MARKUP = ansimarkup.AnsiMarkup( tags={ "header": ansimarkup.parse("<bold>"), "key": ansimarkup.parse("<cyan>"), "value": ansimarkup.parse("<green>"), "not-detected": ansimarkup.parse("<dim>"), "fail": ansimarkup.parse("<light-red>"), "success": ansimarkup.parse("<green>"), "unknown": ansimarkup.parse("<dim>"), "detected": ansimarkup.parse("<light-green>"), "enrichment": ansimarkup.parse("<light-yellow>"), "warning": ansimarkup.parse("<light-yellow>"), "query": ansimarkup.parse("<white>"), }) def colored_output(function): """Decorator that converts ansi markup into ansi escape sequences. :param function: Function that will return text using ansi markup. :type function: callable :returns: Wrapped function that converts markup into escape sequences. :rtype: callable
import pytest import ansimarkup from loguru import logger am = ansimarkup.AnsiMarkup(tags={"empty": ansimarkup.parse("")}) def test_log_int_level(writer): logger.add(writer, format="{level.name} -> {level.no} -> {message}", colorize=False) logger.log(10, "test") assert writer.read() == "Level 10 -> 10 -> test\n" def test_log_str_level(writer): logger.add(writer, format="{level.name} -> {level.no} -> {message}", colorize=False) logger.log("DEBUG", "test") assert writer.read() == "DEBUG -> 10 -> test\n" def test_add_level(writer): name = "L3V3L" icon = "[o]" level = 10 logger.level(name, level, color="<red>", icon=icon)