xnano.region
xnano.region
¶
xnano.region
A rectangle of cells: standalone, or a live view of a canvas or a placement.
Classes:
-
Region–A rectangle of cells that you can read and write, one cell or dot at a time.
Functions:
-
write–Build a standalone region from text (does not draw).
-
box–Build an empty standalone rectangle of cells with a border (does not draw).
Region
¶
Region(buffer: Buffer, *, owner: Canvas[object, object] | Placement[object, object] | None = None, inset: tuple[int, int, int, int] = (0, 0, 0, 0), resolution: Resolution = 'cells', is_standalone: bool = False, look: Appearance | None = None, is_text: bool = False)
Bases: _Looked
A rectangle of cells that you can read and write, one cell or dot at a time.
A region is either standalone, made by write or box,
which owns its cells and can be written into a canvas, or a live view of the cells of
a canvas or a placement, from canvas.region and placement.region. Coordinates are
region coordinates, (0, 0) being the top-left cell. (x, y) in region and
region.local(point) use screen cells, the frame of context.mouse.position.
The default resolution is one cell. region.half_blocks addresses two pixels per cell
(one above the other) and region.braille eight dots per cell (two by four, one color
for the whole cell). Reading gives a Pixel; assigning accepts a
pixel, a color (the background of a cell, the color of a pixel or dot), a str (text)
or, over a slice, anything write accepts.
A standalone region owns color, border, padding and margin and draws
them in its own cells, so its coordinates include them: (0, 0) of a bordered region
is the corner of the border. On a live view they belong to the canvas or the placement,
and the coordinates address the area inside them. A region written into a canvas is a
snapshot: changing the region afterwards changes nothing on the canvas. A region made by
write draws its text again whenever its look changes, so cells you edited are
replaced; edit the cells of a box instead.
Attributes:
-
width(int) –The width in cells, or in pixels or dots at the other resolutions.
-
height(int) –The height in cells, or in pixels or dots at the other resolutions.
-
x(int) –The screen column of the left edge.
-
y(int) –The screen row of the top edge.
-
resolution(Resolution) –'cells','half_blocks'or'braille'. -
braille(Region) –The same cells as braille dots.
-
half_blocks(Region) –The same cells as half-block pixels.
-
color(Colors) –The colors of a standalone region.
-
border(BorderAppearance) –The border of a standalone region.
-
padding(PaddingAppearance) –The padding of a standalone region.
-
margin(MarginAppearance) –The margin of a standalone region.
Examples:
import xnano
card = xnano.write('hello', border='rounded')
assert card.render().splitlines()[0] == '╭─────╮'
with xnano.new(width=8, height=2) as canvas:
region = canvas.region
region[0:4, 0:1] = 'abcd'
region[5, 0] = xnano.Pixel('diamond', foreground='red')
region[0, 1] = (0, 0, 255)
assert region[5, 0].character == '◆'
assert region[1, 0].character == 'b'
assert (1, 1) in region
dots = region.braille
assert (dots.width, dots.height) == (16, 8)
Wrap a buffer. Get a region from write, box or canvas.region instead.
Parameters:
-
buffer(Buffer) –The core buffer whose cells the region addresses.
-
owner(Canvas[object, object] | Placement[object, object] | None, default:None) –The canvas or placement a live view belongs to.
-
inset(tuple[int, int, int, int], default:(0, 0, 0, 0)) –Cells left out on the left, top, right and bottom of the buffer.
-
resolution(Resolution, default:'cells') –'cells','half_blocks'or'braille'. -
is_standalone(bool, default:False) –Whether the region owns its buffer and its look.
-
look(Appearance | None, default:None) –The look of a standalone region.
-
is_text(bool, default:False) –Whether a standalone region shows the text of its look.
Methods:
-
__contains__–Whether a screen cell is inside the region.
-
local–Convert a screen cell into a cell of this region.
-
resize–Change the size of the region, in cells.
-
__getitem__–Read a cell (a
Pixel) or take a part of the region. -
__setitem__–Write a pixel, a color, text or content into a cell or a part of the region.
-
shade–Color every cell, pixel or dot from a function of its position, in one write.
-
draw_color_bytes–Draw a row-major RGBA image with its top-left pixel at
(x, y). -
draw_image–Draw an image with its top-left pixel at
(x, y). -
cell_for_pixel–Return the cell that holds a pixel or dot.
-
pixel_for_cell–Return the pixel or dot at an anchor of a cell.
-
copy–Return a standalone region with a copy of these cells and, for one, its look.
-
render–Draw the region to a string.
-
__str__–Return the characters of the region, so
print(region)shows it. -
__xnano_render__–Return the buffer that shows the region: its own, or a copy for a view.
-
__setattr__–Set an attribute, or raise
AttributeErrorfor an unknown public name.
Source code in xnano/region.py
border
property
writable
¶
border: BorderAppearance
The border of a standalone region; see BorderAppearance.
padding
property
writable
¶
padding: PaddingAppearance
The padding of a standalone region; see Edges.
resolution
property
¶
'cells', 'half_blocks' (1 by 2 per cell) or 'braille' (2 by 4).
x
property
¶
x: int
The screen column of the region's left edge; 0 for a region that is not on screen.
braille
property
¶
braille: Region
The same cells as braille dots: two columns and four rows per cell.
A dot is drawn in the color of its cell, so the eight dots of a cell share one color.
Examples:
half_blocks
property
¶
half_blocks: Region
The same cells as pixels, two to a cell: one above the other.
__contains__
¶
Whether a screen cell is inside the region.
Parameters:
Source code in xnano/region.py
local
¶
Convert a screen cell into a cell of this region.
Parameters:
-
point(Point) –A cell in screen coordinates, such as
context.mouse.position.
Source code in xnano/region.py
resize
¶
Change the size of the region, in cells.
On a view of a placement this resizes the placement. A standalone region made by
box keeps its cells where they fit. A region made by
write is sized by its text, border, padding and margin, so it
cannot be resized.
Parameters:
Raises:
-
TerminalError–The region is the view of a canvas, whose size follows its screen, a slice of another region, or it was made by
write, whose size follows its text. -
ValueError–A size is below 1 or above 65535.
Source code in xnano/region.py
__getitem__
¶
Read a cell (a Pixel) or take a part of the region.
Parameters:
Raises:
-
IndexError–A coordinate is outside the region; the message names the valid ranges.
-
TypeError–The key is a screen
Pointor not a pair. -
ValueError–A slice has a step, or a slice is taken from a pixel resolution.
Source code in xnano/region.py
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 | |
__setitem__
¶
__setitem__(key: tuple[int | slice, int | slice], value: Pixel | Color | tuple[int, int, int] | str | object) -> None
Write a pixel, a color, text or content into a cell or a part of the region.
- A
Pixelwrites its character and colors; over a slice it fills it. - A
Coloror an(red, green, blue)tuple sets the background of a cell (the color of a pixel or dot at the other resolutions). Astris never a color: make one withColor('#336699'). - A
stris text, written from the cell and clipped at the slice. - Any other content that
writeaccepts covers a slice.
Parameters:
-
key(tuple[int | slice, int | slice]) –(x, y)in region coordinates, or two slices without a step. -
value(Pixel | Color | tuple[int, int, int] | str | object) –What to write.
Raises:
-
IndexError–A coordinate is outside the region.
-
TypeError–A single cell was given content that needs a slice, a screen
Pointwas used as the key, or text was written at a pixel resolution. -
ValueError–A slice has a step.
Source code in xnano/region.py
shade
¶
shade(function: Callable[[int, int], ColorValue | None]) -> None
Color every cell, pixel or dot from a function of its position, in one write.
At the default resolution the function is called for each cell and the result is
the cell's background; region.half_blocks.shade and region.braille.shade call
it for each pixel or dot.
Parameters:
-
function(Callable[[int, int], ColorValue | None]) –Called with
(x, y)in region coordinates; return the color, orNoneto leave the cell alone.
Examples:
import xnano
with xnano.new(width=4, height=2) as canvas:
canvas.region.shade(lambda x, y: (x * 60, y * 60, 0) if x < 3 else None)
assert canvas.region[2, 1].background.to_rgb() == (120, 60, 0)
canvas.region.half_blocks.shade(lambda x, y: (0, y * 30, 0))
assert canvas.region.half_blocks[0, 3].foreground is not None
Source code in xnano/region.py
draw_color_bytes
¶
draw_color_bytes(data: bytes, *, width: int, height: int, x: int = 0, y: int = 0, mode: RasterMode = 'over') -> None
Draw a row-major RGBA image with its top-left pixel at (x, y).
At the default resolution the image is drawn with half-block pixels.
Parameters:
-
data(bytes) –width * height * 4bytes of red, green, blue and alpha. -
width(int) –The image width in pixels.
-
height(int) –The image height in pixels.
-
x(int, default:0) –The pixel column of the image's left edge.
-
y(int, default:0) –The pixel row of the image's top edge.
-
mode(RasterMode, default:'over') –'over'blends by alpha;'replace'overwrites.
Source code in xnano/region.py
draw_image
¶
draw_image(image: Image | TerminalImage, *, x: int = 0, y: int = 0, mode: RasterMode = 'over', frame_index: int = 0) -> None
Draw an image with its top-left pixel at (x, y).
Pixels outside the region are skipped, so drawing a large image at a negative x
and y scrolls it. At the default resolution the image is drawn with half-block
pixels.
Parameters:
-
image(Image | TerminalImage) –An
Imageor a picture fromxnano.image. It needs the optionalxnano-imagepackage. -
x(int, default:0) –The pixel column of the image's left edge. May be negative.
-
y(int, default:0) –The pixel row of the image's top edge. May be negative.
-
mode(RasterMode, default:'over') –'over'blends by alpha;'replace'overwrites. -
frame_index(int, default:0) –Which frame of an animated image to draw.
Source code in xnano/region.py
cell_for_pixel
¶
Return the cell that holds a pixel or dot.
Parameters:
Source code in xnano/region.py
pixel_for_cell
¶
Return the pixel or dot at an anchor of a cell.
The mouse reports cells; this picks one pixel of the cell the pointer is over.
Parameters:
-
x(int) –The cell column.
-
y(int) –The cell row.
-
anchor(PixelAnchor, default:'center') –Which pixel of the cell to return.
Source code in xnano/region.py
copy
¶
copy() -> Region
Return a standalone region with a copy of these cells and, for one, its look.
Source code in xnano/region.py
render
¶
Draw the region to a string.
Parameters:
-
format(Literal['text', 'ansi'], default:'text') –'text'for the characters only,'ansi'with colors and styles.
Examples:
Source code in xnano/region.py
__xnano_render__
¶
Return the buffer that shows the region: its own, or a copy for a view.
Source code in xnano/region.py
__setattr__
¶
Set an attribute, or raise AttributeError for an unknown public name.
Source code in xnano/_attributes.py
write
¶
write(text: str, /, **appearance: Unpack[AppearanceArguments]) -> Region
Build a standalone region from text (does not draw).
The region is sized to the text and is a value that you write into a canvas. It owns its
cells. Write it into a canvas with canvas.write(region), print it
with render, or change its look afterwards with
region.border and its siblings.
Parameters:
-
text(str) –The text, line by line.
-
appearance(Unpack[AppearanceArguments], default:{}) –The look of the text: colors, text modifiers,
border,title,paddingandmargin; seeAppearanceArguments.
Raises:
-
TypeError–textis not a string.
Examples:
import xnano
card = xnano.write('hello', border='rounded', padding={'horizontal': 1})
assert card.render().splitlines()[1] == '│ hello │'
card.border = False
assert card.render().strip() == 'hello'
Source code in xnano/region.py
box
¶
box(width: int, height: int, /, **appearance: Unpack[AppearanceArguments]) -> Region
Build an empty standalone rectangle of cells with a border (does not draw).
The region is a value that you write into a canvas.
Parameters:
-
width(int) –The width in cells, border included.
-
height(int) –The height in cells, border included.
-
appearance(Unpack[AppearanceArguments], default:{}) –The look of the box.
borderis'plain'unless you pass another kind orFalse.
Raises:
-
ValueError–A size is below 1 or above 65535.
Examples:
import xnano
frame = xnano.box(5, 3)
assert frame.render().splitlines() == ['┌───┐', '│ │', '└───┘']