Module tenlib.flow.messageformatter

This module provides different styles to display msg_* messages.

See msg_info() to check the display functions, and set_message_formatter() to change the display style.

Classes

class MessageFormatter (*, console: rich.console.Console = None)

Wrapper to display information in a pretty way.

The list of possible calls is as follows:

Params

console: Console to write to.

Expand source code
class MessageFormatter(ABC):
    """Wrapper to display information in a pretty way.

    The list of possible calls is as follows:

    * `MessageFormatter.info`
    * `MessageFormatter.success`
    * `MessageFormatter.failure`
    * `MessageFormatter.error`
    * `MessageFormatter.warning`
    * `MessageFormatter.debug`
    """

    CLEAR_LINE = "\r\x1b[K"

    def __init__(self, *, console: Console = None):
        """
        Params:
            console: Console to write to.
        """
        self._console = console

    @property
    def console(self) -> Console:
        return self._console or get_console()

    def _output(self, *objects, **kwargs) -> None:
        self.console.print(*objects, **kwargs)

    def print(self, *objects, **kwargs) -> None:
        """Displays a message."""
        return self._output(*objects, **kwargs)

    def bin_print(self, data: bytes) -> None:
        """Prints binary `data` and flushes the stream.

        Args:
            data (bytes)
        """
        try:
            self.console.file.buffer.write(data)
        except TypeError:
            raise TypeError("MessageFormatter.bin_print() expects a byte-like object")
        self.console.file.buffer.flush()

    @abstractmethod
    def info(self, *objects, **kwargs) -> None:
        """Displays an information message."""

    @abstractmethod
    def warning(self, *objects, **kwargs) -> None:
        """Displays a warning message."""

    @abstractmethod
    def error(self, message, **kwargs) -> None:
        """Displays an error message."""

    @abstractmethod
    def success(self, message, **kwargs) -> None:
        """Displays a success message."""

    @abstractmethod
    def failure(self, message, **kwargs) -> None:
        """Displays a failure message."""

    @abstractmethod
    def debug(self, message, **kwargs) -> None:
        """Displays a debug message."""

    def clear(self) -> None:
        """Clears last line."""
        self.console.file.write(self.CLEAR_LINE)

Ancestors

  • abc.ABC

Subclasses

Class variables

var CLEAR_LINE

Instance variables

prop console : rich.console.Console
Expand source code
@property
def console(self) -> Console:
    return self._console or get_console()

Methods

def print(self, *objects, **kwargs) ‑> None

Displays a message.

def bin_print(self, data: bytes) ‑> None

Prints binary data and flushes the stream.

Args

data (bytes)

def info(self, *objects, **kwargs) ‑> None

Displays an information message.

def warning(self, *objects, **kwargs) ‑> None

Displays a warning message.

def error(self, message, **kwargs) ‑> None

Displays an error message.

def success(self, message, **kwargs) ‑> None

Displays a success message.

def failure(self, message, **kwargs) ‑> None

Displays a failure message.

def debug(self, message, **kwargs) ‑> None

Displays a debug message.

def clear(self) ‑> None

Clears last line.

class NewschoolMessageFormatter (*, console: rich.console.Console = None)

Status is be indicated as a colored symbol at the beginning of every line. Requires UTF-8.

Examples

>>> o = NewschoolOutput()
>>> o.info('Something')
· Something
>>> o.success('Success')
✔ Success
>>> o.failure('Failure')
✖ Failure
>>> o.print('test')
 test

Params

console: Console to write to.

Expand source code
class NewschoolMessageFormatter(PrefixMessageFormatter):
    """Status is be indicated as a colored symbol at the beginning of every
    line. Requires UTF-8.

    Examples:
        >>> o = NewschoolOutput()
        >>> o.info('Something')
        · Something
        >>> o.success('Success')
        ✔ Success
        >>> o.failure('Failure')
        ✖ Failure
        >>> o.print('test')
         test
    """

    _PREFIX = {
        Status.INFO: "[b blue]·[/]",
        Status.FAILURE: "[b red]✖[/]",
        Status.ERROR: "[b red]✖[/]",
        Status.SUCCESS: "[b green]✔[/]",
        Status.WARNING: "[b yellow]▲[/]",
        Status.DEBUG: "[b magenta]⊙[/]",
    }

Ancestors

Inherited members

class OldschoolMessageFormatter (*, console: rich.console.Console = None)

Status is indicated as [+], [-], [*], etc. at the beginning of every line, with the icon colored.

Examples

>>> o = OtherOldschoolMessageFormatter()
>>> o.info('Something')
[*] Something
>>> o.success('Something else')
[+] Something else

Params

console: Console to write to.

Expand source code
class OldschoolMessageFormatter(PrefixMessageFormatter):
    """Status is indicated as `[+]`, `[-]`, `[*]`, etc. at the beginning of
    every line, with the icon colored.

    Examples:
        >>> o = OtherOldschoolMessageFormatter()
        >>> o.info('Something')
        [*] Something
        >>> o.success('Something else')
        [+] Something else
    """

    _PREFIX = {
        Status.INFO: "[[blue]*[/]]",
        Status.FAILURE: "[[red]-[/]]",
        Status.ERROR: "[[red]x[/]]",
        Status.SUCCESS: "[[green]+[/]]",
        Status.WARNING: "[[yellow]![/]]",
        Status.DEBUG: "[[magenta]D[/]]",
    }

Ancestors

Inherited members

class OtherOldschoolMessageFormatter (*, console: rich.console.Console = None)

Status is indicated as [+], [-], [*], etc. at the beginning of every line, colored and bold.

Examples

>>> o = OldschoolMessageFormatter()
>>> o.info('Something')
[i] Something
>>> o.success('Something else')
[+] Something else

Params

console: Console to write to.

Expand source code
class OtherOldschoolMessageFormatter(PrefixMessageFormatter):
    """Status is indicated as `[+]`, `[-]`, `[*]`, etc. at the beginning of
    every line, colored and bold.

    Examples:
        >>> o = OldschoolMessageFormatter()
        >>> o.info('Something')
        [i] Something
        >>> o.success('Something else')
        [+] Something else
    """

    _PREFIX = {
        Status.INFO: "[b blue]\[*][/]",
        Status.FAILURE: "[b red]\[-][/]",
        Status.ERROR: "[b red]\[x][/]",
        Status.SUCCESS: "[b green]\[+][/]",
        Status.WARNING: "[b yellow]\[!][/]",
        Status.DEBUG: "[b magenta]\[D][/]",
    }

Ancestors

Inherited members

class SlickMessageFormatter (*, console: rich.console.Console = None)

Status is indicated as a colored pipe at the beginning of every line.

Examples

>>> o = SlickMessageFormatter()
>>> o.info('Something')
| Something

Params

console: Console to write to.

Expand source code
class SlickMessageFormatter(PrefixMessageFormatter):
    """Status is indicated as a colored pipe at the beginning of every line.

    Examples:
        >>> o = SlickMessageFormatter()
        >>> o.info('Something')
        | Something
    """

    _PREFIX = {
        Status.INFO: "[b blue]|[/]",
        Status.FAILURE: "[b red]|[/]",
        Status.ERROR: "[b red]|[/]",
        Status.SUCCESS: "[b green]|[/]",
        Status.WARNING: "[b yellow]|[/]",
        Status.DEBUG: "[b magenta]|[/]",
    }

Ancestors

Inherited members

class CircleMessageFormatter (*, console: rich.console.Console = None)

Status is be indicated as a colored circle at the beginning of every line. Requires UTF-8.

Params

console: Console to write to.

Expand source code
class CircleMessageFormatter(MessageFormatter):
    """Status is be indicated as a colored circle at the beginning of every
    line. Requires UTF-8.
    """

    _PREFIX = {
        Status.INFO: "[b blue]·[/]",
        Status.FAILURE: "[b red]·[/]",
        Status.ERROR: "[b red]·[/]",
        Status.SUCCESS: "[b green]·[/]",
        Status.WARNING: "[b yellow]·[/]",
        Status.DEBUG: "[b magenta]·[/]",
    }

Ancestors

Inherited members

class IconMessageFormatter (*, console: rich.console.Console = None)

Status is be indicated as a colored symbol at the beginning of every line. Requires UTF-8.

Examples

>>> o = NewschoolOutput()
>>> o.info('Something')
 ·  Something
>>> o.success('Success')
 ✔  Success
>>> o.failure('Failure')
 ✖  Failure
>>> o.print('test')
test

Params

console: Console to write to.

Expand source code
class IconMessageFormatter(PrefixMessageFormatter):
    """Status is be indicated as a colored symbol at the beginning of every
    line. Requires UTF-8.

    Examples:
        >>> o = NewschoolOutput()
        >>> o.info('Something')
         ·  Something
        >>> o.success('Success')
         ✔  Success
        >>> o.failure('Failure')
         ✖  Failure
        >>> o.print('test')
        test
    """

    _PREFIX = {
        Status.INFO: "[b blue on black] · [/]",
        Status.FAILURE: "[b red on black] ✖ [/]",
        Status.ERROR: "[b red on black] ✖ [/]",
        Status.SUCCESS: "[b green on black] ✔ [/]",
        Status.WARNING: "[b yellow on black] ▲ [/]",
        Status.DEBUG: "[b magenta on black] ⊙ [/]",
    }

Ancestors

Inherited members

class BackgroundMessageFormatter (*, console: rich.console.Console = None)

Status is be indicated as a symbol, and the background of the whole line will be colored. Requires UTF-8.

Params

console: Console to write to.

Expand source code
class BackgroundMessageFormatter(MessageFormatter):
    """Status is be indicated as a symbol, and the background of the whole line
    will be colored. Requires UTF-8.
    """

    _FORMAT = {
        Status.INFO: "·",
        Status.FAILURE: "✖",
        Status.ERROR: "✖",
        Status.SUCCESS: "✔",
        Status.WARNING: "▲",
        Status.DEBUG: "⊙",
    }

    _STYLES = {
        Status.INFO: "on blue",
        Status.FAILURE: "on red",
        Status.ERROR: "on red",
        Status.SUCCESS: "on green",
        Status.WARNING: "on yellow",
        Status.DEBUG: "on magenta",
    }

Ancestors

Inherited members