Skip to content

Commit

Permalink
Update aspectus
Browse files Browse the repository at this point in the history
(v0.1.6)

Signed-off-by: thgs <[email protected]>
  • Loading branch information
thgs committed Dec 22, 2023
1 parent 76676fd commit f2bf173
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A slider component",
"type": "library",
"require": {
"aspectus/aspectus": "^0.1.5"
"aspectus/aspectus": "^0.1.6"
},
"license": "MIT",
"autoload": {
Expand Down
51 changes: 22 additions & 29 deletions examples/simple.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Aspectus\Aspectus;
use Aspectus\Component;
use Aspectus\Components\Basic\DefaultMainComponent;
use Aspectus\Components\Input\Slider;
use Aspectus\Components\Input\View\SliderView;
use Aspectus\Message;
Expand All @@ -14,13 +14,14 @@

$xterm = new Xterm(new TerminalDevice());

$mainComponent = new class($xterm) implements Component
$mainComponent = new class($xterm) extends DefaultMainComponent
{
private Slider $slider;

public function __construct(private Xterm $xterm)
public function __construct(protected Xterm $xterm)
{
$this->slider = new Slider($this->xterm, new SliderView(10, 5, 50, showValue: true));
parent::__construct($this->xterm);
}

public function view(): string
Expand All @@ -37,34 +38,26 @@ public function view(): string

public function update(?Message $message): ?Message
{
switch ($message?->type) {
case Message::INIT:
$this->xterm
->saveCursorAndEnterAlternateScreenBuffer()
->hideCursor()
->setPrivateModeTrackMouseAll()
->flush();
break;
case Message::TERMINATE:
$this->xterm
->restoreCursorAndEnterNormalScreenBuffer()
->showCursor()
->unsetPrivateModeTrackMouseAll()
->flush();
break;
case Message::KEY_PRESS:
if (strtolower($message['key']) === 'q') {
return Message::quit();
}
return $this->slider->update($message);
case Message::MOUSE_INPUT:
return $this->slider->update($message);
return match ($message?->type) {
Message::KEY_PRESS => match ($message['key']) {
'q' => Message::quit(),
default => $this->slider->update($message),
},
Message::MOUSE_INPUT => $this->slider->update($message),
default => parent::update($message)
};
}

default:
return null;
}
protected function onInit(Aspectus $aspectus): ?Message
{
$this->xterm->setPrivateModeTrackMouseAll();
return parent::onInit($aspectus);
}

return null;
protected function onTerminate(Aspectus $aspectus): ?Message
{
$this->xterm->unsetPrivateModeTrackMouseAll();
return parent::onTerminate($aspectus);
}
};

Expand Down

0 comments on commit f2bf173

Please sign in to comment.