midgard.dev.console

Simpler dealing with the console

Description:

Utilities for using the console. Mainly wrappers around other libraries to make them easier and more intuitive to use.

Size of console: The two functions lines() and columns() report the current size of the console.

Textwrapping: The function fill() can be used to rewrap a text-string so that it fits inside the console.

Color: The sub-module color can be used to set the foreground and background colors. Note that the color functionality depends on the external package colorama. If colorama is not installed, color gracefully falls back to not showing any color.

Examples:

>>> from midgard.dev import console
>>> console.columns()  # doctest: +SKIP
86

>>> print(console.fill(a_very_long_string))  # doctest: +SKIP
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras tempus eleifend feugiat.
Maecenas vitae posuere metus. Sed sit amet fermentum velit. Aenean vitae turpis at
risus sollicitudin fringilla in in nisi. Maecenas vitae ante libero. Aenean ut eros
consequat, ornare erat at, tempus arcu. Suspendisse velit leo, eleifend eget mi non,
vehicula ultricies erat. Vestibulum id nisi eget nisl venenatis dignissim. Duis cursus
quam dui, vel hendrerit nibh lacinia id.

>>> print(console.color.Fore.YELLOW + console.color.Back.BLUE + 'I am YELLOW text on BLUE backdrop!')  # doctest: +SKIP
I am YELLOW text on a BLUE background!

columns()

columns() -> int

The width of the console

Returns:

The width of the console in characters.

dedent()

dedent(text:str, num_spaces:Union[int, NoneType]=None) -> str

Wrapper around textwrap.dedent

Dedents at most num_spaces. If num_spaces is not specified, dedents as much as possible.

Args:

Returns:

Dedented string.

fill()

fill(text:str, *, width:Union[int, NoneType]=None, hanging:Union[int, NoneType]=None, **tw_args:Any) -> str

Wrapper around textwrap.fill

The tw_args are passed on to textwrap.fill. See textwrap.TextWrapper for available keyword arguments.

The default value for width is console.columns(), while the new argument hanging, if defined, will try to set (although not override) the textwrap-arguments initial_indent and subsequent_indent to create a hanging indent (no indent on the first line) of hanging spaces.

Args:

Returns:

Wrapped string.

indent()

indent(text:str, num_spaces:int, **tw_args:Any) -> str

Wrapper around textwrap.indent

The tw_args are passed on to textwrap.indent.

Args:

Returns:

Indented string.

lines()

lines() -> int

The height of the console

Returns:

The heigth of the console in characters.

num_leading_spaces()

num_leading_spaces(text:str, space_char:str=' ') -> int

Count number of leading spaces in a string

Args:

Returns:

Number of leading spaces.