Running an App
app runs an application.
Give it the root component, already called with its arguments,
and run it on an event loop with asyncio.run:
import asyncio
from counterweight import app
from counterweight.components import component
from counterweight.elements import Text
@component
def root(name: str) -> Text:
return Text(content=f"Hello, {name}!")
if __name__ == "__main__":
asyncio.run(app(root("world")))
It switches the terminal to its alternate screen, reads input raw and turns on mouse reporting,
then renders the root and handles input until something returns Quit,
which by default Ctrl+C does.
It restores the terminal before it returns,
and before an error raised in a component, a handler or an effect propagates out of it.
What else it takes
The rest of app's parameters each belong to a part of the framework, with its own page:
| To | Pass | See |
|---|---|---|
| Title the terminal window | title |
SetTitle |
| Change what Ctrl+C and Ctrl+Z do, or give other keys defaults | unclaimed_keys |
Keys the terminal would act on |
| Read the time in another time zone, or freeze it | clock |
use_clock |
| Drive it without a person, or without a terminal | autopilot, headless, dimensions |
Autopilot |
| Record it as an asciicast | recording |
Recording |
| Catch components that go stale | check_purity |
Performance |
| See other libraries' debug logs in the devlog | log_levels |
The devlog |
| Match a font that isn't twice as tall as it is wide | cell_aspect_ratio |
Gradients |
| Draw on a terminal other than standard input and output | input_stream, output_stream |
Running programs |
Which version is running
counterweight.__version__ is the version installed,
which counterweight version prints too.
API
counterweight.app
async
app(
root: Component,
output_stream: TextIO = stdout,
input_stream: TextIO = stdin,
headless: bool = False,
dimensions: tuple[int, int] | None = None,
autopilot: Iterable[AutopilotStep] = (),
check_purity: bool = False,
clock: Clock = SystemClock(),
log_levels: Mapping[str, int] = frozendict(),
title: str | None = None,
unclaimed_keys: Mapping[
str, AnyControl
] = DEFAULT_UNCLAIMED_KEYS,
cell_aspect_ratio: float = DEFAULT_CELL_ASPECT_RATIO,
recording: Recording | None = None,
) -> None
| PARAMETER | DESCRIPTION |
|---|---|
root
|
The root component of the application, already called
with its arguments, such as
TYPE:
|
output_stream
|
The stream to which the application will write its output. |
input_stream
|
The stream from which the application will read its input. |
headless
|
If
TYPE:
|
dimensions
|
The dimensions of the terminal window, in characters.
If |
autopilot
|
An iterable of events and controls to be processed by the application,
and of
TYPE:
|
check_purity
|
If
TYPE:
|
clock
|
Where
TYPE:
|
log_levels
|
Levels by logger name, with
TYPE:
|
title
|
The title to give the terminal window when the application starts,
as
TYPE:
|
unclaimed_keys
|
What a key press does when no handler claims it, by its key,
as a browser takes a key's default action unless a listener prevents it.
The application reads the terminal raw, so every key reaches it as a key press,
including Ctrl+C and Ctrl+Z, which a terminal otherwise turns into signals,
and the defaults,
TYPE:
|
cell_aspect_ratio
|
How many times taller than it is wide a cell of the terminal's font is, which gradients compute their angles and circles with, so that a 45 degree gradient or a circle looks like one. Most terminal fonts are about 2.
TYPE:
|
recording
|
Where and how to record the application as an asciicast,
which asciinema-player plays back, whether it's headless or not.
If
TYPE:
|