API
Application
counterweight.app.app
async
app(
root: Callable[[], Component],
output_stream: TextIO = sys.stdout,
input_stream: TextIO = sys.stdin,
headless: bool = False,
dimensions: tuple[int, int] | None = None,
autopilot: Iterable[AnyEvent | AnyControl] = (),
) -> None
PARAMETER | DESCRIPTION |
---|---|
root |
The root component of the application. |
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. This is primarily useful for testing or generating screenshots programmatically. Note that the autopilot will not be processed until after the initial render cycle, and that using the autopilot does not automatically cause the application to quit!
TYPE:
|
Components
counterweight.components.component
component(
func: Callable[P, AnyElement]
) -> Callable[P, Component]
A decorator that marks a function as a component.
counterweight.components.Component
dataclass
Component(
func: Callable[..., AnyElement],
args: tuple[object, ...],
kwargs: dict[str, object],
key: str | int | None = None,
)
The result of calling a component function.
These should not be instantiated directly;
instead, use the @component
decorator on a function
and call it normally.
Elements
counterweight.elements.Div
children
class-attribute
instance-attribute
children: Sequence[Component | AnyElement] = Field(
default=()
)
on_key
class-attribute
instance-attribute
on_key: Callable[[KeyPressed], AnyControl | None] | None = (
None
)
on_mouse
class-attribute
instance-attribute
on_mouse: (
Callable[[MouseEvent], AnyControl | None] | None
) = None
counterweight.elements.Text
on_key
class-attribute
instance-attribute
on_key: Callable[[KeyPressed], AnyControl | None] | None = (
None
)
on_mouse
class-attribute
instance-attribute
on_mouse: (
Callable[[MouseEvent], AnyControl | None] | None
) = None
Hooks
counterweight.hooks.use_state
PARAMETER | DESCRIPTION |
---|---|
initial_value |
The initial value of the state. It can either be the initial value itself, or a zero-argument function that returns the initial value.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
T
|
The current value of the state (i.e., for the current render cycle). |
Setter[T]
|
A function that can be called to update the value of the state (e.g., in an event handler). It can either be called with the new value of the state, or a function that takes the current value of the state and returns the new value of the state. If the value is not equal to the current of the state, Counterweight will trigger a render cycle. |
counterweight.hooks.use_effect
PARAMETER | DESCRIPTION |
---|---|
setup |
The setup function that will be called when the component first mounts or if its dependencies have changed (see below).
TYPE:
|
deps |
The dependencies of the effect.
If any of the dependencies change, the previous invocation of the
TYPE:
|
counterweight.hooks.use_ref
counterweight.hooks.use_mouse
use_mouse() -> Mouse
RETURNS | DESCRIPTION |
---|---|
Mouse
|
A record describing the current state of the mouse. |
counterweight.hooks.Mouse
dataclass
Mouse(
absolute: Position, motion: Position, button: int | None
)
absolute
instance-attribute
The absolute position of the mouse on the screen (i.e., the top-left corner of the screen is Position(x=0, y=0)
).
motion
instance-attribute
The difference in the absolute
position of the mouse since the last render cycle.
button
instance-attribute
button: int | None
The button that is currently pressed, or None
if no button is pressed.
counterweight.hooks.use_rects
use_rects() -> Rects
RETURNS | DESCRIPTION |
---|---|
Rects
|
A recording describing the rectangular areas of the
|
counterweight.hooks.Rects
dataclass
counterweight.hooks.use_hovered
use_hovered() -> Hovered
RETURNS | DESCRIPTION |
---|---|
Hovered
|
A record describing which of the calling component's top-level element's
|
counterweight.hooks.Hovered
dataclass
Events
counterweight.events.MouseEvent
module-attribute
MouseEvent = Union[
MouseMoved,
MouseDown,
MouseUp,
MouseScrolledDown,
MouseScrolledUp,
]
counterweight.events.MouseMoved
dataclass
MouseMoved(absolute: Position, button: int | None)
absolute
instance-attribute
The absolute position on the screen that the mouse moved to.
button
instance-attribute
button: int | None
The button that was held during the motion, or None
if no button was pressed.
counterweight.events.MouseDown
dataclass
MouseDown(absolute: Position, button: int)
absolute
instance-attribute
The absolute position on the screen that the mouse moved to.
counterweight.events.MouseUp
dataclass
MouseUp(absolute: Position, button: int)
absolute
instance-attribute
The absolute position on the screen that the mouse moved to.
counterweight.events.MouseScrolledDown
dataclass
MouseScrolledDown(absolute: Position, direction: int = 1)
absolute
instance-attribute
The absolute position on the screen that the mouse moved to.
direction
class-attribute
instance-attribute
direction: int = 1
The direction that the mouse was scrolled as an integer offset; -1
for up, +1
for down.
counterweight.events.MouseScrolledUp
dataclass
MouseScrolledUp(absolute: Position, direction: int = -1)
absolute
instance-attribute
The absolute position on the screen that the mouse moved to.
direction
class-attribute
instance-attribute
direction: int = -1
The direction that the mouse was scrolled as an integer offset; -1
for up, +1
for down.
Controls
counterweight.controls.AnyControl
module-attribute
AnyControl = Union[
Quit, Bell, Screenshot, Suspend, ToggleBorderHealing
]
counterweight.controls.Quit
dataclass
Cause the application to quit.
The quit occurs at the beginning of the next render cycle, so all other events that are due to be processed in the current cycle will be processed before the application exits.
counterweight.controls.Bell
dataclass
Cause the terminal to emit a bell sound.
The bell occurs at the beginning of the next render cycle, so all other events that are due to be processed in the current cycle will be processed before the sound is played.
counterweight.controls.Screenshot
dataclass
Screenshot(
handler: Callable[[ElementTree], Awaitable[None] | None]
)
Take a "screenshot" of the rendered UI,
using the given handler
callback function.
The screenshot is passed to the handler
as an
ElementTree
containing an SVG representation of the UI.
The screenshot is taken at the beginning of the next render cycle, so all other events that are due to be processed in the current cycle will be processed before the screenshot is taken (but the screenshot will still be of the UI from before the next render occurs!).
to_file
classmethod
to_file(
path: Path, indent: int | None = None
) -> Screenshot
A convenience method for producing a Screenshot
that writes the resulting SVG to the given path
.
PARAMETER | DESCRIPTION |
---|---|
path |
The path to write the SVG to. Parent directories will be created if they do not exist.
TYPE:
|
indent |
The number of spaces to indent the SVG by (for readability).
If
TYPE:
|
counterweight.controls.ToggleBorderHealing
dataclass
Toggle whether border healing occurs.
Styles
counterweight.styles.Style
counterweight.styles.Span
counterweight.styles.Margin
counterweight.styles.Border
edges
class-attribute
instance-attribute
counterweight.styles.Padding
counterweight.styles.Content
counterweight.styles.Typography
counterweight.styles.Color
counterweight.styles.CellStyle
Layout
counterweight.styles.Flex
position
class-attribute
instance-attribute
align_self
class-attribute
instance-attribute
align_self: Literal[
"none", "start", "center", "end", "stretch"
] = "none"
justify_children
class-attribute
instance-attribute
justify_children: Literal[
"start",
"center",
"end",
"space-between",
"space-around",
"space-evenly",
] = "start"
align_children
class-attribute
instance-attribute
align_children: Literal[
"start", "center", "end", "stretch"
] = "start"
counterweight.styles.Relative
Relative positioning is relative to the parent element's content box. Elements occupy space and are laid out next to their siblings according to the parent's layout direction.
counterweight.styles.Absolute
Absolute positioning is relative to the parent element's content box, but the element does not occupy space in the layout.
The inset
property determines which corner of the
parent element's content box this element is positioned relative to.