Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve terminal-attributes example #9

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions examples/terminal-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use Aspectus\Terminal\Event\InputEvent;
use Aspectus\Terminal\Xterm;
use Revolt\EventLoop;
use function Amp\async;
use function Amp\delay;

require \dirname(__DIR__) . '/vendor/autoload.php';

Expand Down Expand Up @@ -30,24 +32,20 @@ function () use ($xterm) {
// boot
->setPrivateModeSaveCursorAndEnterAlternateScreenBuffer()
->hideCursor()
->moveCursorTo(10, 15)
->write("Resize your terminal")
->flush()
;

EventLoop::repeat(1, function () use ($xterm, $primary, $secondary) {
$xterm
// set colors
->bgBlue()
->brightYellow()
->eraseAll();

$update = function () use ($xterm, $primary, $secondary) {
$view = [
// Primary
'Terminal ID' => $primary->terminalId,
'Capabilities' => "\n\t" . implode(
" ",
// only first letter for when its running on xterm
array_map(fn ($capability) => substr($capability, 0, 1), $primary->capabilities)
),
" ",
// only first letter for when its running on xterm
array_map(fn ($capability) => substr($capability, 0, 1), $primary->capabilities)
),

// Secondary
'Type' => $secondary->type,
Expand Down Expand Up @@ -79,21 +77,35 @@ function () use ($xterm) {
[11, 40],
];

$xterm
->bgBlue()
->brightYellow()
->eraseAll();

$i = 0;
foreach ($view as $title => $data) {
$element = str_pad($title . ' ', 25, '.') . ' : ' . $data;
$xterm
->moveCursorTo(...$lines[$i++])
->write($element);
}
//
//
$xterm
->moveCursorTo(15, 10)
->blink()
->write("Press any key to exit or resize/move your screen")
->normal()
->flush()
;
};

async($update);
async(function () use (&$update) {
delay(1);
if ($update !== null) {
$update();
}
});
Comment on lines +102 to 108
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder why this is needed to be like this (and queue twice). I think maybe there is something wrong with the way the reporting functions work (for example https://github.com/Aspectus-PHP/terminal/blob/improve-terminal-attributes/src/Xterm.php#L249)


EventLoop::onSignal(\SIGWINCH, $update);
EventLoop::run();