UI Components
Tip
Follow the Beautiful UIs guide for examples.!
Spinners¶
Spin
¶
The spinning animation you'd like to use. The spinners are sourced from the NPM cli-spinners package.
You can see all the spinners in action by running uv run -m examples.spinner
. The full list can be found in the code here.
Spinner
¶
A spinner indicating that something is happening behind the scenes. It can be used as a context manager or like a decorator. The context manager usage is like so:
import asyncio
from clypi import Spinner
async def main():
async with Spinner("Doing something", capture=True) as s:
await asyncio.sleep(1)
s.title = "Slept for a bit"
print("I slept for a bit, will sleep a bit more")
await asyncio.sleep(1)
asyncio.run(main())
Spinner.__init__()
¶
def __init__(
self,
title: str,
animation: Spin | list[str] = Spin.DOTS,
prefix: str = " ",
suffix: str = "…",
speed: float = 1,
capture: bool = False,
)
title
: the initial text to display as the spinner spinsanimation
: a providedSpin
animation or a list of frames to displayprefix
: text or padding displayed before the iconsuffix
: text or padding displayed after the iconspeed
: a multiplier to speed or slow down the frame rate of the animationcapture
: if enabled, the Spinner will capture all stdout and stderr and display it nicely
done
¶
Mark the spinner as done early and optionally display a message.
fail
¶
Mark the spinner as failed early and optionally display an error message.
log
¶
Display extra log messages to the user as the spinner spins and your work progresses.
pipe
¶
async def pipe(
self,
pipe: asyncio.StreamReader | None,
color: ColorType = "blue",
prefix: str = "",
)
Examples:
async def main():
async with Spinner("Doing something") as s:
proc = await asyncio.create_subprocess_shell(
"for i in $(seq 1 10); do date && sleep 0.4; done;",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await asyncio.gather(
s.pipe(proc.stdout, color="blue", prefix="(stdout)"),
s.pipe(proc.stderr, color="red", prefix="(stdout)"),
)
spinner
(decorator)¶
This is just a utility decorator that let's you wrap functions so that a spinner
displays while they run. spinner
accepts the same arguments as the context manager Spinner
.
import asyncio
from clypi import spinner
@spinner("Doing work", capture=True)
async def do_some_work():
await asyncio.sleep(2)
asyncio.run(do_some_work())
Boxed¶
Boxes
¶
The border style you'd like to use. To see all the box styles in action run uv run -m examples.boxed
.
The full list can be found in the code here.
boxed
¶
def boxed(
lines: T,
width: int | None = None,
style: Boxes = Boxes.HEAVY,
alignment: AlignType = "left",
title: str | None = None,
color: ColorType = "bright_white",
) -> T:
Parameters:
lines
: the type of lines will determine it's output type. It can be one ofstr
,list[str]
orIterable[str]
width
: the desired width of the boxstyle
: the desired style (seeBoxes
)alignment
: the style of alignment (seealign
)title
: optionally define a title for the box, it's length must be < widthcolor
: a color for the box border and title (seecolors
)
Stack¶
def stack(*blocks: list[str], padding: int = 1) -> str:
def stack(*blocks: list[str], padding: int = 1, lines: bool) -> list[str]:
Horizontally aligns blocks of text to display a nice layout where each block is displayed side by side.
Parameters:
blocks
: a series of blocks of lines of strings to display side by sidepadding
: the space between each blocklines
: if the output should be returned as lines or as a string
Separator¶
separator
¶
def separator(
separator: str = "━",
width: t.Literal["max"] | int = "max",
title: str | None = None,
color: ColorType | None = None,
) -> str:
Parameters:
separator
: the character used to build the separator linewidth
: ifmax
it will use the max size of the terminal. Otherwise you can provide a fixed width.title
: optionally provide a title to display in the middle of the separatorcolor
: the color for the characters
Indented¶
indented
¶
Indents a set of lines with the given prefix
Align¶
align
¶
Aligns text according to alignment
and width
. In contrast with the built-in
methods rjust
, ljust
, and center
, clypi.align(...)
aligns text according
to it's true visible width (the built-in methods count color codes as width chars).
Parameters:
s
: the string being alignedalignment
: one ofleft
,right
, orcenter
width
: the wished final visible width of the string
Examples: