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.
Where it draws
app's viewport says where on the terminal the app draws.
FullScreen(), the default, fills the alternate screen,
as vim or htop do, and leaves the main screen as it found it.
Inline() draws in a region below the prompt, as tall as the root's content,
which leaves its last frame in the terminal when the app stops (see Inline Apps).
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 |
| Let garbage collection run in the middle of a frame | collect_garbage_between_frames |
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 |
| Print to streams other than standard output and standard error | stdout, stderr |
Printing |
Leave sys.stdout and sys.stderr alone |
capture_output |
Stray prints |
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,
clipboard_handler: Callable[[str], object]
| None = None,
collect_garbage_between_frames: bool = True,
viewport: FullScreen | Inline = FullScreen(),
stdout: TextIO = stdout,
stderr: TextIO = stderr,
capture_output: bool = True,
) -> 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.
To draw on the terminal while standard output is a pipe, as a picker run in |
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:
|
clipboard_handler
|
What a |
collect_garbage_between_frames
|
If
TYPE:
|
viewport
|
Where the application draws on its terminal:
TYPE:
|
stdout
|
Where a |
stderr
|
Where a |
capture_output
|
If
TYPE:
|
counterweight.FullScreen
dataclass
FullScreen(mouse: bool = True)
Draw on the terminal's alternate screen, which the app fills, as a full-screen program like vim does,
and hand the main screen back unchanged when the app stops.
mouse
class-attribute
instance-attribute
mouse: bool = True
Whether to track the mouse, so the app receives mouse events. Tracking takes the mouse from the terminal, so turn it off to leave the terminal's own selection and scrolling to the user.
counterweight.Inline
dataclass
Draw in a region below the prompt, as tall as the root's content, at most the terminal's height, with what the app prints above it, in the terminal's scrollback.
The region's top row is the row the cursor is on when the app starts, from its first column,
so it draws over the end of a line printed without a newline.
The root is a flex item of a column as wide as the terminal, at most as tall as it,
so a root taller than the terminal is cut off at the bottom, unless it can shrink, as on the web:
keep content that grows in a scroll_area that sticks to the end,
and give the root min_height(0), so the scroll area shrinks to fit and shows its newest lines.
keep_last_frame
class-attribute
instance-attribute
keep_last_frame: bool = True
Whether to leave the region's last frame on the screen when the app stops, with the prompt below it, or to erase it, so the prompt comes back where the region started, as a picker whose result belongs on the next line does.
mouse
class-attribute
instance-attribute
mouse: bool = False
Whether to track the mouse, so the app receives mouse events over its region.
Tracking takes the wheel from the terminal, which is how a user scrolls back through what the app printed,
so it's off by default.
The terminal reports the mouse on the screen, not the region, so an app that tracks it asks the terminal where
the cursor is (CSI 6 n) when it starts and after each resize, and works out where the region is in between.
Mouse events above the region are over the terminal's scrollback, so the app drops them.