functui.classes

class functui.classes.Box(width: int, height: int, position: Coordinate = (0, 0))[source]

An immutable rectangle defined by width, height and position.

width

The rectangle’s width.

Type:

int

height

The rectangle’s height.

Type:

int

position

The rectangle’s position.

Type:

functui.classes.Coordinate

classmethod from_rect(rect: Rect, position: Coordinate)[source]

Create a box with a rect as constructor.

Parameters:
  • rect – The rectangle whose width and height should be applied.

  • position – Box’s position.

Returns:

A new box with updated width and height, unchanged position.

height: int

Alias for field number 1

intersect(other: Self) Self[source]

Returns the intersection region between this box and another.

If the boxes do not overlap, return a new box with width and height set to 0 and same position.

Parameters:

other – The box to intersect with.

Returns:

A new box representing the overlap region.

is_overlaping(other: Self) bool[source]

Wrather this box overlaps another box.

Parameters:

other – The box to text overlap with.

Returns:

Weather there is an overlap or not.

is_point_inside(point: Coordinate)[source]

Determines whether a point lies within the box’s boundaries.

The check uses half-open bounds: inclusive of the top/left edges, exclusive of the bottom/right edges.

Parameters:

point – The point to test.

Returns:

True if the point lies inside the box, else False.

offset_by(coordinate: Coordinate) Self[source]

Returns a new box moved by the given coordinate offset.

Parameters:

coordinate – The (dx, dy) offset to apply.

Returns:

A new box shifted by the provided coordinate.

position: Coordinate

Alias for field number 2

property rect: Rect

Returns a Rect representing only this box’s size.

Returns:

A rectangle with the same width and height as the box.

resize(top: int = 0, bottom: int = 0, left: int = 0, right: int = 0) Self[source]

Returns a new box resized by expanding or shrinking each side.

Positive values expand outward; negative values shrink inward.

Parameters:
  • top – Amount to expand (or shrink) upward. Defaults to 0.

  • bottom – Amount to expand downward. Defaults to 0.

  • left – Amount to expand leftward. Defaults to 0.

  • right – Amount to expand rightward. Defaults to 0.

Returns:

A new box with adjusted width, height, and shifted position.

union(other: Self) Self[source]

Returns the smallest box that fully contains both this box and another.

This method is commutative.

Parameters:

other – The box to union with.

Returns:

A new box representing the bounding rectangle of both boxes.

using_rect(rect: Rect)[source]

Returns a copy of this box but with dimensions replaced by a Rect.

Parameters:

rect – The rectangle whose width and height should be applied.

Returns:

A new box with updated width and height, unchanged position.

width: int

Alias for field number 0

class functui.classes.CharType(*values)[source]
class functui.classes.Color24(r: int, g: int, b: int)[source]

Represent a 24 bit color.

r

Red value, an integer from 0 to 255 inclusive.

Type:

int

g

Green value, an integer from 0 to 255 inclusive.

Type:

int

b

Blue value, an integer from 0 to 255 inclusive.

Type:

int

b: int

Alias for field number 2

g: int

Alias for field number 1

property hex: int

Convert to an integer represeting this colors hexadecimal value.

r: int

Alias for field number 0

class functui.classes.Color4(*values)[source]

ANSI SGR codes for 4 bit colors

BLACK
RED
GREEN
YELLOW
BLUE
MAGENTA
CYAN
WHITE
RESET

Use terminal’s default foreground or background color.

BRIGHT_BLACK
BRIGHT_RED
BRIGHT_GREEN
BRIGHT_YELLOW
BRIGGT_BLUE
BRIGHT_MAGENTA
BRIGHT_CYAN
BRIGHT_WHITE
class functui.classes.ComputedStyle(fg: Color = Color4.RESET, bg: Color = Color4.RESET, attrs: StyleAttr = <StyleAttr: 0>)[source]

An immutable dataclass for style attributes that can be rendered.

fg

Foreground

Type:

functui.classes.Color

bg

Background

Type:

functui.classes.Color

char_style

Styling flags.

attrs: StyleAttr

Alias for field number 2

bg: Color

Alias for field number 1

fg: Color

Alias for field number 0

class functui.classes.Coordinate(x: int, y: int)[source]

An immutable coordinate in 2d space

x
Type:

int

y
Type:

int

x: int

Alias for field number 0

y: int

Alias for field number 1

class functui.classes.DrawBox(fill, box)[source]
box: Box

Alias for field number 1

fill: Pixel

Alias for field number 0

class functui.classes.DrawPixel(pixel, at)[source]
at: Coordinate

Alias for field number 1

pixel: Pixel

Alias for field number 0

class functui.classes.DrawStringLine(string, at)[source]
at: Coordinate

Alias for field number 1

string: tuple[Pixel]

All pixels are assumed to contain the same style.

class functui.classes.Frame(view_box, screen_rect, default_style, measure_text)[source]
default_style: ComputedStyle

Alias for field number 2

measure_text: MeasureTextFunc

Alias for field number 3

screen_rect: Rect

Alias for field number 1

view_box: Box

Alias for field number 0

class functui.classes.InputEvent(key_event, mouse_position_event)[source]
key_event: str | None

Represents both key and mouse button events. Is set to None if no key or mouse button event was emitted.

mouse_position_event: Coordinate | None

New mouse position. Is set to None if mouse position was not changed.

class functui.classes.Layout(func: Callable, min_size: MinSize, render: partial[Result])[source]

An immutable layout that can be rendered as a string

func

The function that returned this layout. Used to give this layout a name.

Type:

Callable

min_size

Function that returns

Type:

functui.classes.MinSize

func: Callable

Alias for field number 0

min_size: MinSize

Alias for field number 1

render: partial[Result]

Alias for field number 2

class functui.classes.MeasureTextFunc(*args, **kwargs)[source]

A function that measures how long a string is when it is printed.

Parameters:

string (str)

Returns:

Printed length of the string.

Return type:

int

class functui.classes.MinSize(*args, **kwargs)[source]

A function that returns a Layout’s minimum size.

Parameters:
  • measure_text (MeasureTextFunc)

  • rect (Rect) – Available space for the layout. Useful for implementing text wrapping, where the layouts height depends on available width.

Returns:

A Layout’s minium size.

Return type:

Rect

class functui.classes.Pixel(char, char_type, style)[source]
char: str

Alias for field number 0

char_type: CharType

Alias for field number 1

style: ComputedStyle

Alias for field number 2

class functui.classes.Rect(width: int, height: int)[source]

A simple immutable rectangle defined by width and height.

width
Type:

int

height
Type:

int

clamp(other: Self) Self[source]

Returns a rectangle clamped so its dimensions do not exceed another rectangle.

Effectively selects the minimum width and height between the two rectangles. This method is commutative.

Parameters:

other – The rectangle whose dimensions act as an upper bound.

Returns:

A new rectangle whose dimensions are the min of the two.

Return type:

Rect

clamp_height(height: int) Self[source]

Returns a rectangle with its height limited to a maximum value.

Parameters:

height – The maximum allowed height.

Returns:

A new rectangle with height clamped to at most the given value.

Return type:

Rect

clamp_width(width: int) Self[source]

Returns a rectangle with its width limited to a maximum value.

Parameters:

width – The maximum allowed width.

Returns:

A new rectangle with width clamped to at most the given value.

Return type:

Rect

height: int

Alias for field number 1

resize(width: int = 0, height: int = 0) Self[source]

Returns a new Rect resized by expanding or shrinking.

Positive values expand; negative values shrink.

Parameters:
  • width – Amount to expand (or shrink) on the x axis.

  • height – Amount to expand (or shrink) on the y axis.

Returns: A new rectangle with altered size.

union(other: Self) Self[source]

Returns a rectangle representing the maximum dimensions of this and another.

Effectively selects the largest width and height between the two rectangles. This method is commutative.

Parameters:

other – The rectangle to compare against.

Returns:

A new rectangle whose dimensions are the max of the two.

Return type:

Rect

width: int

Alias for field number 0

class functui.classes.Result(_draw_commands: list[functui.classes.DrawPixel | functui.classes.DrawBox | functui.classes.DrawStringLine] = <factory>, _data: dict[type[functui.classes.ResultData], functui.classes.ResultData] = <factory>)[source]
class functui.classes.ResultCreatedWith(measure_text_func: MeasureTextFunc, screen_size: Rect)[source]

this is added to a result by the get_result function so that this data can later be used by any rendering function

class functui.classes.ResultData[source]
class functui.classes.Screen(width: int, height: int)[source]

Represents the text grid of a screen.

set(pos: Coordinate, data: Pixel) None[source]

may error if out of range!!!

class functui.classes.StyleAttr(*values)[source]

Flags representing different syles.

BOLD
REVERSE
ITALIC
UNDERLINE
STRIKE_THROUGH

Is not suported by the curses renderer.

DIM

Will be interpreted as thinner font weight by the html renderer.

class functui.classes.StyleRule(fg: Color | None = None, bg: Color | None = None, add_attrs: StyleAttr = <StyleAttr: 0>, remove_attrs: StyleAttr = <StyleAttr: 0>)[source]

An immutable dataclass for style attributes.

fg

Foreground color.

Type:

functui.classes.Color | None

bg

Background color.

Type:

functui.classes.Color | None

add_attrs

Add styling flags.

Type:

functui.classes.StyleAttr

remove_attrs

Remove styling flags.

Type:

functui.classes.StyleAttr

add_attrs: StyleAttr

Alias for field number 2

bg: Color | None

Alias for field number 1

fg: Color | None

Alias for field number 0

remove_attrs: StyleAttr

Alias for field number 3

class functui.classes.WrapperNode(*args, **kwargs)[source]

A function that creates a layout based on a child layout.

Parameters:

child_layout (Layout)

Returns:

New layout based on child.

Return type:

Layout

functui.classes.hex(value: int, /)[source]

Create a new Color24 from a hexodecimal integer.

functui.classes.hsl(h: float, s: float, l: float, /)[source]

Create a new Color24 from hsl parameters.

functui.classes.intersperse(iterable: Iterable, sep: T) Iterable[source]

Yield elements with sep inserted between them.

Example

>>> from functui.classes import intersperse
>>> list(intersperse([1, 2, 3], 0))
[1, 0, 2, 0, 3]
functui.classes.layout_to_result(layout: Layout, dimensions: Rect, measure_text: MeasureTextFunc = <function <lambda>>) Result[source]

Converts a layout to a result that can be converted to desired output type.

See also

To see what to do with the result, read I/O Overview.

functui.classes.rgb(r: int, g: int, b: int, /)[source]

Create a new Color24 from rgb parameters.