consolekit.utils
Utility functions.
Changed in version 1.0.0: traceback_handler() and handle_tracebacks()
moved to consolekit.tracebacks.
They will still be importable from here until v2.0.0
Classes:
|
Mistletoe markdown renderer for terminals. |
Functions:
|
Aborts the program execution. |
|
Compare two sequences of lines; generate the delta as a unified diff. |
|
Print the given line(s), separated by newlines, to stdout or a file. |
Context manager to hide the cursor for the scope of the |
|
Hide the cursor. |
|
|
Returns a list of all commands. |
|
Return whether |
|
Echo |
|
Print |
Show the cursor. |
Data:
|
|
|
|
|
-
class
TerminalRenderer(*extras, **kwargs)[source] Bases:
BaseRendererMistletoe markdown renderer for terminals.
Tested in Gnome Terminal and Terminator (both libVTE-based), and PyCharm. libVTE has the best support. PyCharm’s support for italics and strikethrough is poor. Support on Windows is, as expected, poor.
Not tested on other terminals, but contributions are welcome to improve support.
New in version 0.8.0.
Methods:
render(token)Render the given token for display in a terminal.
render_emphasis(token)Render emphasis (
*emphasis*).render_inline_code(token)Render inline code (
`code`).render_line_break(token)Render a line break in a multiline paragraph.
render_list(token)Render a markdown list.
render_list_item(token)Render a markdown list item.
render_paragraph(token)Render a paragraph.
render_strikethrough(token)Render strikethrough (
~~strikethrough~~).render_strong(token)Render strong (
**strong**).-
render(token)[source] Render the given token for display in a terminal.
- Parameters
token
- Return type
-
render_emphasis(token)[source] Render emphasis (
*emphasis*).- Parameters
token (
Emphasis) – The token to render.- Return type
-
render_inline_code(token)[source] Render inline code (
`code`).- Parameters
token (
InlineCode) – The token to render.- Return type
-
static
render_line_break(token)[source] Render a line break in a multiline paragraph.
- Parameters
token
- Return type
-
render_list(token)[source] Render a markdown list.
- Parameters
token (
List) – The token to render.- Return type
-
render_list_item(token)[source] Render a markdown list item.
- Parameters
token (
ListItem)- Return type
-
render_paragraph(token)[source] Render a paragraph.
- Parameters
token (
Paragraph) – The token to render.- Return type
-
-
abort(message, colour=None)[source] Aborts the program execution.
- Parameters
Changed in version 1.0.1: Added the
colouroption.- Return type
-
braille_spinner= <itertools.cycle object> Type:
cycle()itertools.cycle()of braille characters to use as a loading spinner.New in version 0.7.0.
-
coloured_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\n', removed_colour=<Colour('\x1b[31m', name='RED')>, added_colour=<Colour('\x1b[32m', name='GREEN')>)[source] Compare two sequences of lines; generate the delta as a unified diff.
Unified diffs are a compact way of showing line changes and a few lines of context. The number of context lines is set by
nwhich defaults to three.By default, the diff control lines (those with
---,+++, or@@) are created with a trailing newline. This is helpful so that inputs created fromfile.readlines()result in diffs that are suitable forfile.writelines()since both the inputs and outputs have trailing newlines.For inputs that do not have trailing newlines, set the lineterm argument to
''so that the output will be uniformly newline free.The unidiff format normally has a header for filenames and modification times. Any or all of these may be specified using strings for
fromfile,tofile,fromfiledate, andtofiledate. The modification times are normally expressed in the ISO 8601 format.New in version 0.3.0.
Example:
>>> for line in coloured_diff( ... 'one two three four'.split(), ... 'zero one tree four'.split(), 'Original', 'Current', ... '2005-01-26 23:30:50', '2010-04-02 10:20:52', ... lineterm='', ... ): ... print(line) --- Original 2005-01-26 23:30:50 +++ Current 2010-04-02 10:20:52 @@ -1,4 +1,4 @@ +zero one -two -three +tree four
- Parameters
fromfile (
str) – Default''.tofile (
str) – Default''.fromfiledate (
str) – Default''.tofiledate (
str) – Default''.n (
int) – Default3.lineterm (
str) – Default'\n'.removed_colour (
Colour) – TheColourto use for lines that were removed. Default<Colour('\x1b[31m', name='RED')>.added_colour (
Colour) – TheColourto use for lines that were added. Default<Colour('\x1b[32m', name='GREEN')>.
- Return type
-
echo(message, *lines, file=None, err=False, color=None)[source] Print the given line(s), separated by newlines, to stdout or a file.
- Parameters
*lines (
str) – Additional lines to print.file (
Optional[IO]) – The file to write to. Defaults tostdout. DefaultNone.err (
bool) – Write tostderrinstead ofstdout. DefaultFalse.color (
Optional[bool]) – Force showing or hiding colors and other styles. By default Click will remove color if the output does not look like an interactive terminal. DefaultNone.
New in version 1.12.0.
Context manager to hide the cursor for the scope of the
withblock.New in version 0.7.0.
Changed in version 0.9.0: Moved to
consolekit.utils.- Return type
-
hide_cursor()[source] Hide the cursor.
To show it again use
show_cursor(), or use thehidden_cursor()context manager.New in version 0.7.0.
-
import_commands(source=None, entry_point=None)[source] Returns a list of all commands.
Commands can be defined locally in the module given in
source, or by third party extensions who define an entry point in the following format:<name (can be anything)> = <module name>:<command>
-
long_echo(text, use_pager=None, colour=None)[source] Echo
textto the terminal, optionally via a pager.New in version 1.2.0.
- Parameters
text (
Union[str,StringList,Iterable[str]])use_pager (
Optional[bool]) – IfTrue, forces the use of the pager. IfFalsethe pager is never used. IfNonethe pager is used if sys.stdout` is a TTY and the number of lines is less than the terminal height. DefaultNone.colour (
Optional[bool]) – Whether to use coloured output. Default auto-detect.
Tip
Allow the user to control the value of
use_pagerwith theno_pager_option()decorator.
-
overtype(*objects, sep=' ', end='', file=None, flush=False)[source] Print
objectsto the text streamfile, starting with"\r", separated bysepand followed byend.sep,end,fileandflush, if present, must be given as keyword argumentsAll non-keyword arguments are converted to strings like
strdoes and written to the stream, separated by sep and followed by end. If no such arguments are given,overtype()will just write"\r".- Parameters
*objects – A list of strings or string-like objects to write to the terminal.
sep (
str) – String to separate the objects with. Default'␣'.end (
str) – String to end with. Default''.file (
Optional[IO]) – An object with awrite(string)method. Defaultsys.stdout.flush (
bool) – IfTrue, the stream is forcibly flushed. DefaultFalse.
-
show_cursor()[source] Show the cursor.
See also
The
hidden_cursor()context manager.New in version 0.7.0.
-
snake_spinner= <itertools.cycle object> Type:
cycle()itertools.cycle()of braille characters to use as a loading spinner which looks like a snake.New in version 1.1.0.
-
solidus_spinner= <itertools.cycle object> Type:
cycle()itertools.cycle()of characters to use as a loading spinner.New in version 0.7.0.