Live screens
A form redraws when you press a key. A live screen also redraws on a timer, so it can show something that changes on its own, such as a counter fed by work running elsewhere. Follow this page to make a screen live, wake it on a stream and test it.
A live screen waits for a key for a fixed interval. Each time the wait runs out, it calls your tick callback and redraws if the frame changed. Keys reach the form as usual between ticks.
To show a counter beside the trail that goes up on every tick:
use DrevOps\PhpTui\Block\Markup;
use DrevOps\PhpTui\Screen\Axis;
use DrevOps\PhpTui\Screen\Layout\RegionName;
use DrevOps\PhpTui\Tui;
$scale = new Markup('scale', 'Waiting for the first crate...');
$tui = (new Tui($form))
->place(RegionName::Header, $scale, tail: TRUE)
->flow(RegionName::Header, Axis::Columns)
->live(function (int $count) use ($scale): void {
$scale->body(sprintf('Crates weighed: %d', $count));
}, 500);
$answers = $tui->run();
Runnable script: playground/21-live-screen.php.
To give every key on a dashboard with no fields to your own commands, select the view key preset.
The tick
->live(Closure $tick, int $interval = 250) takes the callback and the interval in milliseconds. An interval below 1 throws a FormException when live() is called.
- The tick receives the number of ticks so far, counting from 1. It also receives the room the panel has, and it returns nothing.
- The tick changes blocks you hold a reference to: a markup you placed in a screen region, or one a panel builder returned. A live table and a live listing take new rows, and a live log takes new lines.
- The screen redraws after the tick only when the frame changed, so an idle screen doesn't flicker.
- The tick runs between key presses, so a key press is never half handled when the tick runs.
- A key press or stream output ends the wait early but keeps the interval running, so the tick still runs once per interval.
Headless collection has no screen, so it never calls the tick.
Size what you show
The tick's second argument is a DrevOps\PhpTui\Screen\Size. Read it to fit your content to the screen, so your code never reads the terminal:
$size->rowsis the number of rows the panel may fill. It counts the region the form is drawn in, less the trail, the keys and the buttons around the panel. Rows your blocks leave empty still count.$size->columnsis the width every block lays out to.
Each tick measures the screen again, so the size follows a resized terminal. A tick that declares only int $count still runs, because PHP drops the extra argument.
To keep a log as long as the panel is tall:
use DrevOps\PhpTui\Screen\Size;
$tui->live(function (int $count, Size $size) use ($log): void {
$log->limit(max(1, $size->rows));
});
The first tick runs 1 interval after the screen opens, or sooner on a resize. Content sized only in the tick shows at a guessed size until then. To size it from the first frame, pass a callback to ->fit(Closure $fit). The screen calls it with the same Size before the first frame, and again each time it finds the terminal at a new size:
use DrevOps\PhpTui\Screen\Size;
$tui->fit(function (Size $size) use ($log): void {
$log->limit(max(1, $size->rows));
});
A screen without live() or a watched stream never waits on a timer. It calls fit() before the first frame, and again only when it comes back from the shell to a resized terminal.
To keep a selected row in sight in a long list, use a live listing in a region that scrolls.
Terminal resizes
When the terminal changes size, the next frame is drawn at the new size. The frame's height follows the terminal, and the theme is built again for the new width.
When the screen redraws depends on the pcntl extension:
- With
pcntl, the screen listens forSIGWINCH. A resize during the wait for a key cuts the wait short, so the screen redraws immediately and the tick runs early. A resize during a redraw or a tick is laid out after the next wait. - Without
pcntl, the screen re-reads the terminal size each time it waits for a key.
On Windows, the wait for a key can't time out. A live screen there redraws on each key press, and the tick never runs.
Wake on a stream
A screen that follows a child process can show each write from the child the moment it arrives. Pass the child's output pipe to ->watch(), and the wait for a key also ends when that pipe has output.
To show the last report a child process writes:
$bench = new Markup('bench', 'Waiting for the sorter...');
$sorter = proc_open([PHP_BINARY, 'sort-crates.php'], [1 => ['pipe', 'w']], $pipes);
stream_set_blocking($pipes[1], FALSE);
$tui = (new Tui($form))
->place(RegionName::Header, $bench, tail: TRUE)
->watch($pipes[1], function ($pipe) use ($bench): void {
$bench->body(trim((string) fread($pipe, 8192)));
});
Runnable script: playground/21-live-streams.php.
->watch($stream, Closure $callback) takes an open stream resource and the callback. Anything else throws a FormException. Each watched stream works like this:
- The callback receives the stream and reads what arrived. Make the stream non-blocking with
stream_set_blocking(), so a read returns straight away when the child has written less than it asks for. - The callback runs before the next frame, and the screen redraws when the frame changed.
- Watching the same stream again replaces its callback.
->unwatch($stream)stops watching the stream. Called from a callback or a tick, it applies from the next wait.- A stream that's closed, or at its end after its callback, stops being watched. A finished child process stops waking the screen.
- A screen waits on its watched streams even without
live().
On Windows, a stream never wakes the screen, because PHP can't wait on a pipe there.
Step away and come back
Press Ctrl-Z to hand the terminal back to your shell without ending the process. The screen leaves the alternate screen and restores the terminal modes, so the shell prompt works as usual. Then type a shell job command:
- Type
fgto bring the screen back. It redraws from its current state. - Type
bgto keep the work going while you use the prompt. The tick and the callbacks of watched streams keep running, and the screen draws nothing. Typefglater to bring the screen back with everything they changed.
When the screen comes back, it sets up the terminal again with the theme's background wash. A terminal resized while you were away redraws at its new size.
Ctrl-Z works from anywhere in the form, including from inside an open field. Keys typed after it in the same read are dropped. A form that isn't live detaches the same way, with no tick to run while it's away.
Detaching needs the pcntl and posix extensions and a terminal on standard input. Where either is missing, such as on Windows, Ctrl-Z does nothing.
Runnable script: playground/21-live-screen-detach.php.
Test a live screen
ScreenTester has a matching live() method. Mix 3 markers from DrevOps\PhpTui\Testing into the scripted keystrokes:
| Marker | Effect |
|---|---|
new Tick() | The wait for a key times out, so the tick runs |
new Resize($columns, $rows) | The terminal reports the new size from then on, and the wait is cut short as by SIGWINCH |
new Background() | Placed right after Key::named(KeyName::Suspend), the session carries on detached as after bg |
A detached session runs the tick at each Tick and applies each Resize. It comes back at the first scripted item that's neither, as after fg. Without Background, the suspend key brings the session straight back.
ScreenTester draws only the panel you give it, so put the counter in that panel. To check that the counter changes with no key pressed:
use DrevOps\PhpTui\Block\Markup;
use DrevOps\PhpTui\Block\Panel;
use DrevOps\PhpTui\Screen\Layout\PanelLayout;
use DrevOps\PhpTui\Screen\Layout\RegionName;
use DrevOps\PhpTui\Testing\Resize;
use DrevOps\PhpTui\Testing\ScreenTester;
use DrevOps\PhpTui\Testing\Tick;
$scale = new Markup('scale', 'Crates weighed: 0');
$panel = (new Panel('bench', 'Packing bench'))->layout(new PanelLayout());
$panel->in(RegionName::Content)->add($scale);
$tester = (new ScreenTester($panel))
->live(fn(int $count) => $scale->body('Crates weighed: ' . $count));
$tester->run(new Tick(), new Tick(), new Resize(40, 12));
$this->assertStringContainsString('Crates weighed: 2', $tester->frame(2));
A tester without live() skips the Tick and Resize markers.
ScreenTester also has matching watch() and unwatch() methods. Before each scripted item, the scripted terminal checks the watched streams for output. To check that a write wakes the screen, watch 1 end of a socket pair and write to the other:
[$pipe, $child] = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
fwrite($child, 'Pears sorted');
$tester = (new ScreenTester($panel))
->watch($pipe, fn($stream) => $scale->body((string) fread($stream, 64)));
$tester->run(new Tick());
$this->assertStringContainsString('Pears sorted', $tester->frame(1));