From dfdd66b021c23bbcb5a33bedd66e58c3a22529e1 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Fri, 4 Oct 2024 10:51:59 +0200 Subject: [PATCH] Handle terminal being resized --- src/Editor/Editor.php | 5 +++++ src/Terminal/Terminal.php | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Editor/Editor.php b/src/Editor/Editor.php index 1198f4a..de1de20 100644 --- a/src/Editor/Editor.php +++ b/src/Editor/Editor.php @@ -104,6 +104,11 @@ class Editor while ($this->running) { [$w,$h] = $this->term->getSize(); + if ($this->term->resized) { + $this->redrawEditor(); + $this->term->resized = false; + } + usleep(10000); $read = $this->term->readKey(); switch ($read) { case 'k{UP}': diff --git a/src/Terminal/Terminal.php b/src/Terminal/Terminal.php index 825e8c0..0a89ddf 100644 --- a/src/Terminal/Terminal.php +++ b/src/Terminal/Terminal.php @@ -14,6 +14,8 @@ class Terminal private bool $active = false; + public bool $resized = false; + public function __construct() { if (self::$init++ == 0) { @@ -43,7 +45,11 @@ class Terminal //ob_flush(); $this->measureTerminal(); - pcntl_signal(SIGWINCH, $this->measureTerminal(...)); + pcntl_signal(SIGWINCH, function () { + $this->resized = true; + $this->measureTerminal(); + }); + pcntl_async_signals(true); $this->active = true; }