writer#
Provides functionality for making fancy terminal output.
- libjam.writer.print(text: str, flush: bool = False)#
Prints
textto stdout, without a newline.
- libjam.writer.println(text: str, flush: bool = False)#
Prints
textto stdout, with a newline.
- libjam.writer.eprint(text: str, flush: bool = False)#
Prints
textto stderr, without a newline.
- libjam.writer.eprintln(text: str, flush: bool = False)#
Prints
textto 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_columnsis 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:
CSICommandHides the cursor.
- libjam.writer.show_cursor = '\x1b[?25h'#
Type:
CSICommandReveals the cursor.
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.
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:
ClearSequenceClears the whole line.
- libjam.writer.clear_line_from_cursor = '\x1b[0K'#
Type:
ClearSequenceClears the line starting from the cursor.
- libjam.writer.clear_line_before_cursor = '\x1b[1K'#
Type:
ClearSequenceClears the line up to the cursor.
- libjam.writer.clear_page = '\x1b[2J'#
Type:
ClearSequenceClears the whole page.
- libjam.writer.clear_page_from_cursor = '\x1b[0J'#
Type:
ClearSequenceClears the page starting from the cursor.
- libjam.writer.clear_page_before_cursor = '\x1b[1J'#
Type:
ClearSequenceClears the page up to the cursor.
- libjam.writer.clear_history = '\x1b[3J'#
Type:
ClearSequenceClears the scrollback buffer.
- class libjam.writer.StatusBar(status: str)#
A context manager that prints the
statusto stderr on entry and clears it on exit.If the
statusmessage 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
statusmessage 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.
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[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A[1A'You can simply call
up(50)to get this:'[50A'
- libjam.writer.up = '\x1b[1A'#
Type:
NavigationSequenceMoves the cursor up.
- libjam.writer.down = '\x1b[1B'#
Type:
NavigationSequenceMoves the cursor down.
- libjam.writer.left = '\x1b[1D'#
Type:
NavigationSequenceMoves the cursor left.
- libjam.writer.right = '\x1b[1C'#
Type:
NavigationSequenceMoves the cursor right.
- libjam.writer.prev_line = '\x1b[1F'#
Type:
NavigationSequenceMoves the cursor to the previous line.
- libjam.writer.next_line = '\x1b[1E'#
Type:
NavigationSequenceMoves the cursor to the next line.
- libjam.writer.view_up = '\x1b[1S'#
Type:
NavigationSequenceScrolls the view up.
- libjam.writer.view_down = '\x1b[1T'#
Type:
NavigationSequenceScrolls 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.bright_white = '\x1b[97m'#
Type:
StyleSets the text colour to a lighter shade of white.
- libjam.writer.on_bright_green = '\x1b[102m'#
Type:
StyleSets the background colour to bright green.
- libjam.writer.on_bright_yellow = '\x1b[103m'#
Type:
StyleSets the background colour to bright yellow.
- libjam.writer.on_bright_purple = '\x1b[105m'#
Type:
StyleSets the background colour to bright purple.