These are helpers for application output on a terminal.
New in version 0.6.
This is a helper for dealing with output to a terminal.
Parameters: |
|
---|
After each call resulting in visible characters to be written to the stream the stream is flushed, certain methods allow to override this behaviour.
Specifies the default terminal width.
Returns a TerminalWriter for the given byte stream.
If an encoding cannot be determined from the stream it will fallback to the given encoding, if it is None sys.getdefaultencoding() will be used.
Should an error occur during encoding you can specify what should happen with the errors parameter:
The stream to which the output is written.
The prefix used by writeline().
The string used for indentation.
True if escaping should be done automatically.
Escapes all control characters in the given string.
This is useful if you are dealing with ‘untrusted’ strings you want to write to a file, stdout or stderr which may be viewed using tools such as cat which execute ANSI escape sequences.
Returns a Dimensions object.
May raise NotImplementedError depending on the stream or platform.
Returns the width of the terminal.
This falls back to the COLUMNS environment variable and if that fails to default_width unless default is not None, in which case default would be used.
Therefore the returned value might not not be at all correct.
Returns the width of the terminal remaining once the prefix and indentation has been written.
Parameters: | default_width – The width which is assumed per default for the terminal, see get_width() for more information. |
---|
Warning
Tabs are considered to have a length of 1. This problem may be solved in the future until then it is recommended to avoid tabs.
A contextmanager which allows you to set certain options for the following writes.
Parameters: |
|
---|
Note
The underlying terminal may support only certain options, especially the attributes (bold, faint, standout and blink) are not necessarily available.
The following colours are available, the exact colour varies between terminals and their configuration.
Colors ====== black yellow teal red blue white green purple
A contextmanager which can be used instead of writeline().
This is useful if you want to write a line with multiple different options.
Returns autoescape if escape is None otherwise escape.
Writes the given string to the stream.
Parameters: |
|
---|
Writes the given line to the stream respecting indentation.
Parameters: |
|
---|
Writes each line in the given iterable to the stream respecting indentation.
Parameters: |
|
---|
Writes a horizontal ruler with the width of the terminal to the stream.
Parameters: | character – Specifies the character used for the ruler. |
---|
Writes a table using a list of rows (content) and an optional head.
Parameters: | padding – Specifies the padding used for each cell to the left and right. |
---|
>>> import sys
>>> from brownie.terminal import TerminalWriter
>>> writer = TerminalWriter.from_bytestream(sys.stdout)
>>> writer.table([
... [u'foo', u'bar'],
... [u'spam', u'eggs']
... ])
foo | bar
spam | eggs
<BLANKLINE>
>>> writer.table(
... [
... [u'foo', u'bar'],
... [u'spam', u'eggs']
... ],
... [u'hello', u'world']
... )
hello | world
------+------
foo | bar
spam | eggs
<BLANKLINE>
Returns a ProgressBar object which can be used to write the current progress to the stream.
The progress bar is created from the description which is a string with the following syntax:
Widgets – the parts of the progress bar which are changed with each update – are represented in the form $[a-zA-Z]+.
Some widgets required that you provide an initial value, this can be done by adding :string where string is either [a-zA-Z]+ or a double-quoted string.
If you want to escape a $ you simply precede it with another $, so $$foo` will not be treated as a widget and in the progress bar ``$foo will be shown.
Quotes (") in strings can be escaped with a backslash (\).
The following widgets are available:
Shows the current at maximum number of steps as step of steps, this method takes an initial value determining the unit of each step e.g. if each step represents a byte and you choose bytes as a unit a reasonable prefix will be chosen.
Supported units:
This requires maxsteps to be set.
If you want to implement your own widget(s) take a look at brownie.terminal.progress.Widget, you can use them by passing them in a dictionary (mapping the name to the widget class) via the widgets argument. You might also want to take a look at the source code of the built-in widgets.
There are two things you have to look out for: ProgressBar objects are not reusable if you need another object, call this method again. If you attempt to write to the stream while using a progress bar the behaviour is undefined.
A namedtuple representing the dimensions of a terminal.
Parameters: |
|
---|
This is the ‘low-level’ progress bar implementation used by TerminalWriter.progress().
A progress bar which acts as a container for various widgets which may be part of a progress bar.
Initializing and finishing can be done by using the progress bar as a context manager instead of calling init() and finish().
Parameters: |
|
---|
Returns a ProgressBar from a string.
The string is used as a progressbar, $[a-zA-Z]+ is substituted with a widget as defined by widgets.
$ can be escaped with another $ e.g. $$foo will not be substituted.
Initial values as required for the HintWidget are given like this $hint:initial, if the initial value is supposed to contain a space you have to use a quoted string $hint:"foo bar"; quoted can be escaped using a backslash.
If you want to provide your own widgets or overwrite existing ones pass a dictionary mapping the desired names to the widget classes to this method using the widgets keyword argument. The default widgets are:
Name | Class | Requires maxsteps |
---|---|---|
text | TextWidget | No |
hint | HintWidget | No |
percentage | Percentage | Yes |
bar | BarWidget | No |
sizedbar | PercentageBarWidget | Yes |
step | StepWidget | Yes |
time | TimeWidget | No |
speed | DataTransferSpeedWidget | No |
Returns an iterable of tuples consisting of the position of the widget and the widget itself ordered by each widgets priority.
Returns the width usable by all widgets which don’t provide a size hint.
Represents a part of a progress bar.
The priority of the widget defines in which order they are updated. The default priority is 0.
This is important as the first widget being updated has the entire line available.
Should be True if this widget depends on ProgressBar.maxsteps being set to something other than None.
Called when the progress bar is initialized.
Should return the output of the widget as string.
Represents a ‘hint’, changing text passed with each update, in a progress bar.
Requires that ProgressBar.next() is called with a hint keyword argument.
This widget has a priority of 1.
Represents a string showing the progress as percentage.
A simple bar which moves with each update not corresponding with the progress being made.
The bar is enclosed in brackets, progress is visualized by tree hashes ### moving forwards or backwards with each update; the rest of the bar is filled with dots ..
A simple bar which shows the progress in terms of a bar being filled corresponding to the percentage of progress.
The bar is enclosed in brackets, progress is visualized with hashes # the remaining part uses dots ..
Shows at which step we are currently at and how many are remaining as step of steps.
Parameters: | unit – If each step represents something other than a simple task e.g. a byte when doing file transactions, you can specify a unit which is used. |
---|
Supported units: