writer#

Provides functionality for making fancy terminal output.

libjam.writer.print(text: str, flush: bool = False)#

Prints text to stdout, without a newline.

libjam.writer.println(text: str, flush: bool = False)#

Prints text to stdout, with a newline.

libjam.writer.eprint(text: str, flush: bool = False)#

Prints text to stderr, without a newline.

libjam.writer.eprintln(text: str, flush: bool = False)#

Prints text to stderr, with a newline.

libjam.writer.indent(string: str, prefix: str = '  ') str#

Indents the given string using the given prefix.

libjam.writer.to_columns(items: list[str], n_columns: int = 0, column_sep: str = '  ', prefix: str = '  ') str#

Arranges a list of strings in columns.

If n_columns is not set, it will be calculated based on the size of the terminal.

class libjam.writer.CSICommand(s: str)#

A Control Sequence Introducer (CSI) command string.

libjam.writer.hide_cursor = '\x1b[?25l'#

Type:    CSICommand

Hides the cursor.

libjam.writer.show_cursor = '\x1b[?25h'#

Type:    CSICommand

Reveals the cursor.

libjam.writer.hidden_cursor(flush: bool = False)#

A context manager that hides the cursor.

libjam.writer.hide_input()#

Hides what the user is typing.

Only works on systems where termios is available.

libjam.writer.show_input()#

Reveals what the user is typing.

Only works on systems where termios is available.

libjam.writer.hidden_input()#

A context manager that hides user input.

class libjam.writer.ClearSequence(char: str, n: int)#

A CSI command that clears some part of the screen when printed.

libjam.writer.clear_line = '\x1b[2K'#

Type:    ClearSequence

Clears the whole line.

libjam.writer.clear_line_from_cursor = '\x1b[0K'#

Type:    ClearSequence

Clears the line starting from the cursor.

libjam.writer.clear_line_before_cursor = '\x1b[1K'#

Type:    ClearSequence

Clears the line up to the cursor.

libjam.writer.clear_page = '\x1b[2J'#

Type:    ClearSequence

Clears the whole page.

libjam.writer.clear_page_from_cursor = '\x1b[0J'#

Type:    ClearSequence

Clears the page starting from the cursor.

libjam.writer.clear_page_before_cursor = '\x1b[1J'#

Type:    ClearSequence

Clears the page up to the cursor.

libjam.writer.clear_history = '\x1b[3J'#

Type:    ClearSequence

Clears the scrollback buffer.

class libjam.writer.StatusBar(status: str)#

A context manager that prints the status to stderr on entry and clears it on exit.

If the status message is bigger than the user’s terminal, then only a part of it will be printed, so that it fits cleanly onto one line in the user’s terminal, maintaining the appearance of a bar.

Usage example:

with StatusBar('Configuring Foo...') as status:
  configure_foo()
  status.update('Configuring Bar')
  configure_bar()
update(status: str = None)#

Updates the status bar.

class libjam.writer.ProgressBar(status: str, done: int = 0, todo: int = 0, symbols: str = '[= ]')#

A context manager that prints a progress bar to stderr on entry and clears it on exit.

If the status message is bigger than the user’s terminal, then only a part of it will be printed, so that it fits cleanly onto one line in the user’s terminal, maintaining the appearance of a bar.

Example usage:

with ProgressBar('Fooing 3 Bars', 0, 3) as progress_bar:
  for i in range(1, 4):
    foo(bar)
    progress_bar.update(i)
update(done: int = None, todo: int = None)#

Updates the progress bar.

class libjam.writer.NavigationSequence(char: str, n: int = 1)#

A CSI command that moves the cursor or view when printed.

You can call instances of this class to get a modified version. For example, to get a string that moves the cursor up by 50 lines, instead of doing something like up * 50, which would produce this:

'[1'

You can simply call up(50) to get this:

''
libjam.writer.up = '\x1b[1A'#

Type:    NavigationSequence

Moves the cursor up.

libjam.writer.down = '\x1b[1B'#

Type:    NavigationSequence

Moves the cursor down.

libjam.writer.left = '\x1b[1D'#

Type:    NavigationSequence

Moves the cursor left.

libjam.writer.right = '\x1b[1C'#

Type:    NavigationSequence

Moves the cursor right.

libjam.writer.prev_line = '\x1b[1F'#

Type:    NavigationSequence

Moves the cursor to the previous line.

libjam.writer.next_line = '\x1b[1E'#

Type:    NavigationSequence

Moves the cursor to the next line.

libjam.writer.view_up = '\x1b[1S'#

Type:    NavigationSequence

Scrolls the view up.

libjam.writer.view_down = '\x1b[1T'#

Type:    NavigationSequence

Scrolls the view down.

class libjam.writer.Style(start, end)#

A CSI command that selects the grahpic rendition (SGR).

Example usage:

bold = Style(1, 22)
print(bold('This text is bold!'))

underline = Style(4, 24)
print(underline('This text is underlined!'))

bold_and_underlined = bold + underline
print(bold_and_underlined('This text is bold and underlined!'))
libjam.writer.reset = '\x1b[0m'#

Type:    Style

Resets any previously applied styles.

libjam.writer.bold = '\x1b[1m'#

Type:    Style

Boldens the text.

libjam.writer.dim = '\x1b[2m'#

Type:    Style

Dims the text.

libjam.writer.italic = '\x1b[3m'#

Type:    Style

Italicises the text.

libjam.writer.underline = '\x1b[4m'#

Type:    Style

Underlines the text.

Type:    Style

Makes the text look like its flashing.

libjam.writer.invert = '\x1b[7m'#

Type:    Style

Swaps the foreground and background colours.

libjam.writer.hide = '\x1b[8m'#

Type:    Style

Makes the text invisible.

libjam.writer.strike = '\x1b[9m'#

Type:    Style

Adds a strikethrough to the text.

libjam.writer.default = '\x1b[39m'#

Type:    Style

Sets the text colour to default.

libjam.writer.black = '\x1b[30m'#

Type:    Style

Sets the text colour to black.

libjam.writer.red = '\x1b[31m'#

Type:    Style

Sets the text colour to red.

libjam.writer.green = '\x1b[32m'#

Type:    Style

Sets the text colour to green.

libjam.writer.yellow = '\x1b[33m'#

Type:    Style

Sets the text colour to yellow.

libjam.writer.blue = '\x1b[34m'#

Type:    Style

Sets the text colour to blue.

libjam.writer.purple = '\x1b[35m'#

Type:    Style

Sets the text colour to purple.

libjam.writer.cyan = '\x1b[36m'#

Type:    Style

Sets the text colour to cyan.

libjam.writer.white = '\x1b[37m'#

Type:    Style

Sets the text colour to white.

libjam.writer.on_default = '\x1b[49m'#

Type:    Style

Sets the background colour to default.

libjam.writer.on_black = '\x1b[40m'#

Type:    Style

Sets the background colour to black.

libjam.writer.on_red = '\x1b[41m'#

Type:    Style

Sets the background colour to red.

libjam.writer.on_green = '\x1b[42m'#

Type:    Style

Sets the background colour to green.

libjam.writer.on_yellow = '\x1b[43m'#

Type:    Style

Sets the background colour to yellow.

libjam.writer.on_blue = '\x1b[44m'#

Type:    Style

Sets the background colour to blue.

libjam.writer.on_purple = '\x1b[45m'#

Type:    Style

Sets the background colour to purple.

libjam.writer.on_cyan = '\x1b[46m'#

Type:    Style

Sets the background colour to cyan.

libjam.writer.on_white = '\x1b[47m'#

Type:    Style

Sets the background colour to white.

libjam.writer.bright_black = '\x1b[90m'#

Type:    Style

Sets the text colour to grey.

libjam.writer.bright_red = '\x1b[91m'#

Type:    Style

Sets the text colour to bright red.

libjam.writer.bright_green = '\x1b[92m'#

Type:    Style

Sets the text colour to bright green.

libjam.writer.bright_yellow = '\x1b[93m'#

Type:    Style

Sets the text colour to bright yellow.

libjam.writer.bright_blue = '\x1b[94m'#

Type:    Style

Sets the text colour to bright blue.

libjam.writer.bright_purple = '\x1b[95m'#

Type:    Style

Sets the text colour to bright purple.

libjam.writer.bright_cyan = '\x1b[96m'#

Type:    Style

Sets the text colour to bright cyan.

libjam.writer.bright_white = '\x1b[97m'#

Type:    Style

Sets the text colour to a lighter shade of white.

libjam.writer.on_bright_black = '\x1b[100m'#

Type:    Style

Sets the background colour to grey.

libjam.writer.on_bright_red = '\x1b[101m'#

Type:    Style

Sets the background colour to bright red.

libjam.writer.on_bright_green = '\x1b[102m'#

Type:    Style

Sets the background colour to bright green.

libjam.writer.on_bright_yellow = '\x1b[103m'#

Type:    Style

Sets the background colour to bright yellow.

libjam.writer.on_bright_blue = '\x1b[104m'#

Type:    Style

Sets the background colour to bright blue.

libjam.writer.on_bright_purple = '\x1b[105m'#

Type:    Style

Sets the background colour to bright purple.

libjam.writer.on_bright_cyan = '\x1b[106m'#

Type:    Style

Sets the background colour to bright cyan.

libjam.writer.on_bright_white = '\x1b[107m'#

Type:    Style

Sets the background colour to a lighter shade of white.

libjam.writer.rgb(r: int, g: int, b: int) Style#

Creates a colour Style for given rgb values.

libjam.writer.on_rgb(r: int, g: int, b: int) Style#

Creates a background colour Style for given rgb values.