Skip to main content

Board

Board: a packing bench with a spinner beside Pear until it is packedBoard: a packing bench with a spinner beside Pear until it is packed

A board lines up plain text, styled text and marks in columns, with no borders. Use it to draw a dashboard-style screen, such as the crates at a packing bench. The board collects no answer.

You add entries to a board in the order they draw. Your code chooses the text and the role each run plays, and the theme chooses the colors and glyphs.

To show a packing bench that a live screen redraws on every tick:

use DrevOps\PhpTui\Block\Board;
use DrevOps\PhpTui\Block\Spinner;
use DrevOps\PhpTui\Block\Styled;
use DrevOps\PhpTui\Theme\Tone;

$board = new Board('bench');

$fill = function (int $count) use ($board): void {
$board->clear()->frame($count)
->spread([Styled::strong('PACKING BENCH')], [Styled::muted('tick ' . $count)])
->rule()
->pair([Styled::strong('CRATE'), Styled::strong('STATUS')], [Styled::strong('LAST STEP')])
->pair(['Apple', 'packed'], ['Sealed'])
->pair(['Pear', [new Spinner(), 'weighing']], [Styled::strong('Weighing', Tone::Accent)]);
};

$fill(0);

$form = Form::create('Packing station')
->panel('Bench', function (PanelBuilder $p) use ($board): void {
$p->add($board);
});

$tui = (new Tui($form))->live($fill);

Runnable script: playground/23-board.php.

Entries​

new Board(string $id) builds an empty board. $p->board($id) builds one in the panel's current region and returns it. Each method in the following table adds 1 entry and returns the board:

MethodDraws
->line(...$parts)The parts 1 after another, with nothing between them.
->row(...$cells)The cells in columns, aligned with every other row and pair on the board. A cell is 1 part or a list of parts drawn a space apart.
->pair(array $left, array $right)The left cells as a row, then a divider and the right parts a space apart. Every pair's divider sits in the same column.
->spread(array $left, array $right)The left parts at the left edge and the right parts against the right edge. The left parts are cut to leave 2 spaces before the right ones.
->rule()A line across the frame.

3 more methods manage the board as a whole:

MethodEffect
->clear()Removes every entry, so a tick can draw the board again from the top.
->frame(int $frame)Sets the frame every spinner on the board draws at. A frame past the theme's last wraps.
->items()Returns each entry as its BoardItem case and its runs of parts.

Columns sit 2 spaces apart (Board::GAP), and each row's trailing spaces are trimmed. Every line is cut to the content width.

Parts​

A part is 1 of 4 things:

PartDraws
A stringThe text in the theme's plain text color.
StyledThe text in a tone, bold when it's strong.
A MarkInterface valueA glyph of your own in its tone, or its ASCII stand-in when the theme draws ASCII.
new Spinner()The theme's spinner glyph at the board's frame.

new Styled(string $text, ?Tone $tone = NULL, bool $isStrong = FALSE) builds styled text. Styled::strong($text, $tone) builds strong text in an optional tone, and Styled::muted($text) builds text in the Muted tone.

The Tone enum names the role a run plays, and the theme picks its color:

ToneUse it forDefaultTheme draws it like
Accentsomething in progress, picked or pointed atthe cursor and the caret
Successsomething that finished as intendeda field's value
Warningsomething that needs attention but didn't failthe overflow marker
Errorsomething that faileda field's error
Mutedsomething quieter than the text around ita rule

The board sanitizes every string it draws, styled text and glyphs included. It folds line breaks and tabs to spaces, so text you read from outside can't move the cursor or break a column.

Marks of your own​

A mark is a glyph your board draws beside its text, such as a status check. Implement MarkInterface on an enum of your own: glyph() returns the glyph with its ASCII stand-in, and tone() returns the tone it draws in.

use DrevOps\PhpTui\Block\MarkInterface;
use DrevOps\PhpTui\Theme\Override\Glyph;
use DrevOps\PhpTui\Theme\Tone;

enum CrateMark: string implements MarkInterface {
case Packed = 'packed';
case Bruised = 'bruised';

public function glyph(): Glyph {
return $this === self::Packed ? new Glyph('✓', '+') : new Glyph('✗', 'x');
}

public function tone(): ?Tone {
return $this === self::Packed ? Tone::Success : Tone::Error;
}
}

$board->row([CrateMark::Packed, 'Apple'], 'packed');

A mark whose ASCII stand-in is empty draws nothing in ASCII, and the board drops the space that would have followed it. An empty plain string keeps its space, so you can use 1 to indent a cell.

How it draws​

The board measures each column on the drawn text without its escape codes, so colors never shift a column. It draws every part through the theme's BoardElementsInterface. The theme's board elements cover the text, the marks, the spinner, the pair divider and the rule.

The board draws every entry it holds. A region that wasn't declared to scroll clips the lines past its end.

Headless behavior​

A board is display-only: it carries no answer, and headless collection never draws it.

Display modes​

In all 4 display modes - Unicode or ASCII, color on or off:

ANSINo ANSI
UnicodeBoard: Unicode + ANSIBoard: Unicode + ANSIBoard: Unicode + No ANSIBoard: Unicode + No ANSI
ASCIIBoard: ASCII + ANSIBoard: ASCII + ANSIBoard: ASCII + No ANSIBoard: ASCII + No ANSI