API Reference

echo

Echo message.

lexers can be passed in, and if they are, the resulting output will attempt to be setup

param messages:messages to echo
param verbose:verbosity level
param end:text to print at end of message, defaults to newline
param flush:flush output after write
param lexer:message lexical analyzer
param file:file to echo message to
param json:Dump out a structured text
param time_format:
 string to control time formatting
param add_timestamp:
 Controls whether the timestamp is dumped out
param color:control color output
returns:beautified messages
rtype:Tuple[str]

colors

Color module

Contains resources to build 256 (standard) and 16m (true-color) text output.

class termlog.colors.Color(red: int = 0, green: int = 0, blue: int = 0, term_color: Optional[int] = None, color_prefix: str = '', true_color_prefix: str = 'x1b[38;2;{red};{green};{blue}m', suffix: str = 'x1b[0m', truecolor: bool = False, name: str = '', dim: bool = False, bright: bool = False, italics: bool = False, underlined: bool = False, double_underlined: bool = False, blinking: bool = False, strobing: bool = False, inverted: bool = False, hidden: bool = False, strike_through: bool = False)[source]

Bases: object

A data structure for packaging up Color display information

Example

>>> from termlog import Color, echo
>>> solarized_red = Color(red=220, green=50, blue=47)
>>> solarized_magenta = Color(red=221, green=54, blue=130)
>>> msg = ...
>>> echo(f'{solarized_red("ERROR")}: {solarized_magenta(msg)}')
red

the value for the red aspect of the color [0-255]

green

the value for the green aspect of the color [0-255]

blue

the value for the blue aspect of the color [0-255]

color_prefix

the string prefix when true-color is not enabled

true_color_prefix

the string prefix for when true-color is enabled

suffix

the reset suffix

truecolor

forces output to use truecolor

prefix

calculated prefix based on flags

Flags:
bright: adds bold/bright flag dim: adds dim flag italics: adds italics flag underlined: adds underlined flag double_underlined: adds double underlined flag strobing: adds strobing flag blinking: adds blinking flag inverted: flag that inverts foreground and background colors strike_through: adds strike-through flag hidden: adds hidden flag
blinking = False
blue = 0
bright = False
color_prefix = ''
dim = False
double_underlined = False
static generate_postfix(dim: Optional[bool] = None, bright: Optional[bool] = None, italics: Optional[bool] = None, underlined: Optional[bool] = None, double_underlined: Optional[bool] = None, hidden: Optional[bool] = None, inverted: Optional[bool] = None, strike_through: Optional[bool] = None, blinking: Optional[bool] = None, strobing: Optional[bool] = None) → str[source]

Creates a prefix given the constraints above

Notes

The following options are grouped and only one flag will be honored for each grouping:

  • color, truecolor
  • strobing, blinking
  • dim, bright
  • underlined, double_underlined

Additionally, if the hidden flag is set, none of the other flags will be honored

Parameters:
  • dim – sets the dim flag
  • bright – sets the bright flag
  • italics – sets the italics flag
  • hidden – hide the output
  • inverted – invert the foreground and background colors
  • strike_through – set the strike_through flag
  • underlined – set the underline flag
  • double_underlined – set the underline flag
  • blinking – set the blinking flag
  • strobing – set the strobing flag
Returns:

message with escape sequences for terminal colors

green = 0
hidden = False
inverted = False
italics = False
static keys()[source]
name = ''
postfix = ''
red = 0
strike_through = False
strobing = False
suffix = '\x1b[0m'
term_color = None
true_color_prefix = '\x1b[38;2;{red};{green};{blue}m'
truecolor = False
underlined = False
termlog.colors.rgb(message: Any, red: int = 0, green: int = 0, blue: int = 0, color: bool = True, truecolor: Optional[bool] = None) → str[source]

Add any true color to message.

Example

>>> from termlog import echo, rgb, Color
>>> c = Color(red=223, green=81, blue=127)
>>> print(f'{c!r}')
Color(red=223, green=81, blue=127, color_prefix='', true_color_prefix='{TRUEPREFIX}{red};{green};{blue}{SUFFIX}', suffix='{RESET}', truecolor=False)
>>> f'A {rgb("colored", **c)} message'
'A {TRUEPREFIX}223;81;127{SUFFIX}colored{RESET} message'
Note: this only works if the terminal supports true color and there
is no way to definitively determine if truecolor is supported by the terminal.
Parameters:
  • message – text to color
  • red – red portion of color [0 to 255]
  • green – green portion of color [0 to 255]
  • blue – blue portion of color [0 to 255]
  • color – enable color on output
Returns:

Colored text if color is enabled

formatting

termlog.formatting.beautify(message: Any, indent: int = 0, lexer: Union[pygments.lexer.Lexer, str, None] = None) → str[source]

Beautify message.

Parameters:
  • message – message to beautify
  • indent – number of spaces to indent
  • lexer – message lexical analyzer
Returns:

beautified message

Return type:

str

termlog.formatting.format(*messages, lexer: Union[pygments.lexer.Lexer, str, None] = None, color: bool = None, json: bool = None, time_format: Optional[str] = None, add_timestamp: Optional[bool] = None) → str[source]

Creates message, but does not echo it to a stdout, log, or other

lexers can be passed in, and if they are, the resulting output will attempt to be setup

Parameters:
  • messages – messages to echo
  • color – colorize output
  • lexer – message lexical analyzer
  • json – Dump out a structured text
  • time_format – control time format
  • add_timestamp – add a timestamp to the output
Returns:

beautified messages

Return type:

Tuple[str]

message

class termlog.message.Message(data: Any, timestamp: datetime.datetime = <factory>, json: bool = False, color: bool = False, type: Any = <class 'str'>, lexer: str = '', time_format: Union[str, NoneType] = '%Y%m%d%H%M%S', include_timestamp: bool = True, fields: Dict = <factory>)[source]

Bases: object

calling_frame
calling_frame_code
calling_frame_data
color = False
include_timestamp = True
json = False
lexer = ''
nested_calling_frame_pairs
time_format = '%Y%m%d%H%M%S'
type

alias of builtins.str

termlog.message.fix_json(obj)[source]
termlog.message.strip_escape(text: str) → str[source]

Remove terminal ascii escape sequences from text.

Parameters:text – text to strip escape sequences from
Returns:text stripped of escape sequences

palettes

Palettes allow someone to create a specific color scheme for the terminal log.

termlog.palettes.get_palette() → termlog.palettes.Palette[source]
termlog.palettes.set_palette(palette: termlog.palettes.Palette)[source]
class termlog.palettes.Palette(name: str = '', colors: Dict[KT, VT] = <factory>)[source]

Bases: object

A data structure to capture colors.

Palettes can be defined which will modify termlog’s color and output.

Example

>>> import termlog
>>> termlog.red('hi', truecolor=True)
'hi'
>>> from dataclasses import dataclass, field
>>> @dataclass
    class NewPalette(termlog.Palette):
        name: str = 'My special palette'
red: termlog.Color = field(default=termlog.Color(185, 10, 10, term_color=31))
>>> p = NewPalette()
>>> termlog.set_palette(p)
>>> termlog.red('hi', truecolor=True)
'hi'
name = ''
static strip(string) → str[source]

Strips out escape characters from string