Live log
A live log holds lines that your code writes over time and keeps the newest ones. Use it on a live screen to show a running record, such as each crate weighed at a bench. The log collects no answer.
The log holds at most its line limit and drops the oldest lines past it. It draws every line it holds. To keep the newest lines in sight, put the log in a region declared with ->follows().
Add a log that follows its tail
A following region needs a layout of your own, because a panel's default layout has 1 region and the fields go in it. Follow these steps to put a log under the fields:
-
Write a layout with a region for the log, named by an enum of your own.
->fixed(5)gives the log 5 rows, and->follows()keeps the window on its last line:use DrevOps\PhpTui\Screen\Axis;use DrevOps\PhpTui\Screen\Layout\AbstractLayout;use DrevOps\PhpTui\Screen\Layout\RegionName;use DrevOps\PhpTui\Screen\Layout\RegionNameInterface;enum BenchRegion: string implements RegionNameInterface {case Log = 'log';}final class BenchLayout extends AbstractLayout {public function __construct() {parent::__construct(Axis::Rows);$this->region(RegionName::Content)->content();$this->region(BenchRegion::Log)->fixed(5)->follows();}} -
Build the log, arrange the panel by the layout, and add the log to the
BenchRegion::Logregion:use DrevOps\PhpTui\Block\Log;$log = new Log('scale', 20);$form = Form::create('Weighing station')->panel('Bench', function (PanelBuilder $p) use ($log): void {$p->layout(BenchLayout::class);$p->confirm('Organic only?')->default(TRUE);$p->in(BenchRegion::Log)->add($log);}); -
Write a line on each tick:
$tui = (new Tui($form))->live(function (int $count) use ($log): void {$log->write(sprintf('Crate %d weighed', $count));});Confirm that the newest line shows at the bottom of the region. When older lines sit above the window, the region's first row carries the overflow mark.
Runnable script: playground/22-live-table-log.php.
Options
new Log(string $id, int $limit = 100) builds a log. $p->log($id, $limit) builds one in the panel's current region and returns it.
| Method | Effect |
|---|---|
->write(string ...) | Appends lines. Each argument can hold several lines, split at any line ending. Control bytes are removed. |
->limit(int $limit) | Sets the line limit. Lowering it drops the oldest lines past the new limit. |
->clear() | Removes every line. |
->lines() | Returns the lines, oldest first. |
->lineLimit() | Returns the line limit. |
A limit below 1 throws a FormException, in the constructor and in limit().
Each line draws as 1 row, styled as a markup line. A line wider than its region is clipped.
How a following region scrolls
->follows() declares a scrolling region whose window stays on the end of its contents. Because the region scrolls, it marks the rows above the window.
scrollTo() moves the window off the end and unpins it. After that, the region scrolls like any other, so a focused row in it moves the window to keep that row in sight. Call follows() again to pin the window back. Keep the log in a region of its own. A pinned region stays on the end even when the cursor is on a row in it, so that row can scroll out of sight.
Headless behavior
A live log 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 |