Live listing
A live listing is a list of rows with 1 selected row and no borders. Its cells mix plain text, styled text, marks and spinners, lined up in columns. Use it on a live screen for a list longer than the screen, such as every crate at a bench. The listing collects no answer.
Each row has a key, and the selection is a key. Replacing the rows keeps the mark on the row with the same key, wherever that row now sits. The listing takes no focus and binds no key, so your code moves the selection. In a region that scrolls, the region keeps the selected row in sight.
To show a listing of crates that j and k move through:
use DrevOps\PhpTui\Answers\Answers;
use DrevOps\PhpTui\Block\Listing;
use DrevOps\PhpTui\Block\Styled;
use DrevOps\PhpTui\Input\Command;
$listing = (new Listing('crates', [Styled::strong('CRATE'), Styled::strong('STATUS')]))
->rows(['apple' => ['Apple', 'packed'], 'pear' => ['Pear', 'queued']])
->select('apple');
$form = Form::create('Packing bench')
->panel('Crates', function (PanelBuilder $p) use ($listing): void {
$p->add($listing);
});
$tui = (new Tui($form))->keys('default', [
new Command('down', fn(Answers $answers) => $listing->move(1), 'j'),
new Command('up', fn(Answers $answers) => $listing->move(-1), 'k'),
]);
Runnable script: playground/24-live-listing.php.
Options
new Listing(string $id, array $headers = []) builds a listing. An empty header list draws no header row. $p->listing($id, $headers) builds one in the panel's current region and returns it.
| Method | Effect |
|---|---|
->rows(array $rows) | Replaces every row. Keys are row keys and values are lists of cells. A numeric key and its string form are the same key. |
->select($key) | Selects the row with that key, an integer or a string. NULL clears the selection. A key no row has throws a FormException. |
->move(int $delta) | Moves the selection by that many rows and stops at the first and last row. With nothing selected, it selects the first row. |
->headers(array $headers) | Replaces the header cells. |
->frame(int $frame) | Sets the frame every spinner in the listing draws at. A tick passes its count to turn them. |
->selected() | Returns the selected key as a string, or NULL. |
->rowCells(), ->headerCells() | Return the rows keyed by row key, and the header cells, with every cell as a list of parts. |
A cell is 1 part or a list of parts, drawn a space apart. These are the parts a cell takes:
- A string, drawn as plain text.
- A
Styled, drawn in itsToneand weight, as on a board. - A mark: a case of your own enum that implements
MarkInterface, as on a board. - A
Spinner, drawn at the listing's frame.
The listing sanitizes text and folds its line breaks and tabs to spaces, so outside text can't break a column.
When a replacement drops the selected key, the selection moves to the row now at the same position. With fewer rows, it moves to the last row, and with no rows it clears.
Keep the selected row in sight
Put the listing in a region that scrolls. A panel's content region scrolls by default, so a listing added with $p->add() needs nothing more. The region follows the selected row each frame:
- The focused row comes first. When a field in the same region holds the cursor, the region follows the field.
- Turning the mouse wheel moves the region off the selection. The next key, a command included, brings the selection back into sight.
- A region declared with
follows()stays on its tail.
When the listing is the first block its region scrolls, the header row stays in place and only the rows move. When a block is added above the listing, the header scrolls with the rows.
Keep fixed lines around the rows
A scrolling region holds both of its ends in place. A board pinned above the listing and a note packed below it stay on screen while the rows scroll between them. Pin the blocks above with $p->pin(), and pack the ones below with $p->tail():
use DrevOps\PhpTui\Block\Board;
use DrevOps\PhpTui\Block\Markup;
$bench = (new Board('bench'))->line('Bench 4');
$note = new Markup('note', 'Crates leave at noon.');
$form = Form::create('Packing bench')
->panel('Crates', function (PanelBuilder $p) use ($bench, $listing, $note): void {
$p->pin($bench);
$p->add($listing);
$p->tail($note);
});
The listing is the first block that scrolls, so its header row stays under the board. The region keeps the selected row in sight in the rows left between the held ones, and marks the edges of those rows when more lie beyond them. When the held rows outgrow the region, the listing's rows give way first and the note next.
A listing in a region that doesn't scroll draws every row, and the region clips the rows past its end.
How it draws
The listing draws its parts through the theme's board elements, the same ones a board uses. A gutter before each row holds boardSelector(): the theme's selection mark on the selected row, and blank space of the same width on the others. The header's gutter is blank. Each line is cut to the content width.
Headless behavior
A live listing 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:
| ANSI | No ANSI | |
| Unicode | ||
| ASCII |