diff --git a/src/Editor/Editor.php b/src/Editor/Editor.php index 97232a1..cf0f9cb 100644 --- a/src/Editor/Editor.php +++ b/src/Editor/Editor.php @@ -403,7 +403,7 @@ class Editor /** * Handler for the write file command (ctrl-W) * - * @return void + * @return bool */ private function doWriteFile(): bool { @@ -501,7 +501,7 @@ class Editor }) ->setItems($items) ->setTitle($wd) - ->display(0, 0, 70, 20, $this->filename); + ->display(0, 0, 70, 0, $this->filename); if ($sel === null) { $this->redrawEditor(); return; @@ -536,8 +536,8 @@ class Editor $width = min(90, $w - 10); $height = min(40, $h - 6); - $left = round(($w / 2) - ($width / 2)); - $top = round(($h / 2) - ($height / 2)); + $left = (int)round(($w / 2) - ($width / 2)); + $top = (int)round(($h / 2) - ($height / 2)); $msg = new MessageBox($this->term, self::$helpText, "Help"); $this->redrawInfoBar([ '↑/↓' => 'Scroll', '^C' => 'Close' ]); @@ -562,9 +562,9 @@ class Editor EOT."\n/ Version: ".APP_VERSION."\n/ Built at: ".APP_BUILDDATE; $width = 60; - $height = 22; - $left = round(($w / 2) - ($width / 2)); - $top = round(($h / 2) - ($height / 2)); + $height = 0; // 22; + $left = 0; // (int)round(($w / 2) - ($width / 2)); + $top = 0; // (int)round(($h / 2) - ($height / 2)); $msg = new MessageBox($this->term, $text, "About JSONEdit ".APP_VERSION); $this->redrawInfoBar([ '↑/↓' => 'Scroll', '^C' => 'Close' ]); @@ -654,7 +654,7 @@ class Editor $deleteKey = basename($path); $collNode = $this->list->getNodeForPath($parentPath); if ($collNode instanceof ArrayNode) { - $collNode->removeIndex($deleteKey); + $collNode->removeIndex(intval($deleteKey)); $this->list->parseTree(); $this->redrawEditor(); $this->modified = true; diff --git a/src/Editor/Menu.php b/src/Editor/Menu.php index efdfdf0..b403da6 100644 --- a/src/Editor/Menu.php +++ b/src/Editor/Menu.php @@ -83,12 +83,12 @@ class Menu * @param string|int|null $value * @return mixed */ - public function display(int $left, int $top, int $width, int $height = 0, string|int|null $value): mixed + public function display(int $left, int $top, int $width, int $height = 0, string|int|null $value = null): mixed { //$this->index = 0; $keys = array_keys($this->items); - $this->index = array_search($value, $keys) ?? 0; + $this->index = array_search($value, $keys) ?: 0; [$w,$h] = $this->terminal->getSize(); if ($height == 0) { @@ -187,7 +187,7 @@ class Menu $thumbTop = round(($visibleItems - 1) / count($this->items) * $scrollTop); $thumbBottom = round(($visibleItems) / count($this->items) * $scrollBottom); - $tleft = round(($width / 2) - ((mb_strlen($this->title) + 2) / 2)); + $tleft = (int)round(($width / 2) - ((mb_strlen($this->title) + 2) / 2)); // draw head echo "\e[40;37m"; diff --git a/src/Editor/MessageBox.php b/src/Editor/MessageBox.php index b4b1b77..39a4433 100644 --- a/src/Editor/MessageBox.php +++ b/src/Editor/MessageBox.php @@ -53,13 +53,15 @@ class MessageBox [$w,$h] = $this->terminal->getSize(); if ($height == 0) { - $height = min($h - 5, count($wrapped) + 4); + $height = min($h - $top - 2, count($wrapped) + 4); } + // again? oh well.. + $height = min($height, $h - $top - 2); if ($left == 0) { $left = round(($w - $width) / 2); } if ($top == 0) { - $top = round(($h - $height) / 2); + $top = (int)round(($h - $height) / 2); } $maxScroll = (count($wrapped) > $height) ? count($wrapped) - $height + 4 : 0; @@ -127,6 +129,7 @@ class MessageBox * @return void */ private function redraw(int $left, int $top, int $width, int $height, array $wrapped, string $title, int $maxScroll) { + $visibleItems = $height - 4; // calculate scrollbar thumb positions @@ -150,6 +153,8 @@ class MessageBox ->writeAt($left, $top + $height - 1, self::U_CORNER_BOTTOMLEFT.str_repeat(self::U_EDGE_HORIZONTAL,$width - 2).self::U_CORNER_BOTTOMRIGHT) ; for ($n = 0; $n < $visibleItems; $n++) { + $pre = ''; + $post = ''; if (isset($wrapped[$n+$this->scroll])) { $line = $wrapped[$n+$this->scroll]??null; if (str_starts_with($line, "# ")) { diff --git a/src/List/TreeList.php b/src/List/TreeList.php index 581cb97..65c45da 100644 --- a/src/List/TreeList.php +++ b/src/List/TreeList.php @@ -152,10 +152,7 @@ class TreeList implements Countable, IteratorAggregate return; } $entry = $this->list[$key]; - if (!$entry) { - echo "\e[0m\e[K\e[31mE_NO_ENTRY_IN_LIST\e[0m"; - return; - } + echo "\e[{$screenRow};1H\e[0m"; echo ($selected?"\e[44;97m":"\e[0;37m")."\e[K"; echo "\e[90m".str_repeat( @@ -204,26 +201,27 @@ class TreeList implements Countable, IteratorAggregate : "\e[36m{$entry->key}:\e[37m " )); } - if ($entry->node instanceof ArrayNode) { + $node = $entry->node; + if ($node instanceof ArrayNode) { echo "[" . (Settings::$compactGroups ? "…]":""); - if ($entry->node->isCollapsed()) { + if ($node->isCollapsed()) { if (!Settings::$collapseBefore) echo " \e[90m\u{25ba}"; - echo " \e[90;2m[".count($entry->node->items)."]\e[22m"; + echo " \e[90;2m[".count($node->items)."]\e[22m"; if (!Settings::$compactGroups) echo "\e[37m ]"; } else { if (!Settings::$collapseBefore) echo " \e[90m\u{25bc}"; } - } elseif ($entry->node instanceof ObjectNode) { + } elseif ($node instanceof ObjectNode) { echo "{" . (Settings::$compactGroups ? "…}":""); - if ($entry->node->isCollapsed()) { + if ($node->isCollapsed()) { if (!Settings::$collapseBefore) echo " \e[90m\u{25ba}"; - echo " \e[90;2;3m".join(", ",array_keys($entry->node->properties))."\e[22;23m"; + echo " \e[90;2;3m".join(", ",array_keys($node->properties))."\e[22;23m"; if (!Settings::$compactGroups) echo "\e[37m }"; } else { if (!Settings::$collapseBefore) echo " \e[90m\u{25bc}"; } - } elseif ($entry->node instanceof ValueNode) { - $value = $entry->node->value; + } elseif ($node instanceof ValueNode) { + $value = $node->value; echo match (gettype($value)) { 'string' => "\e[33m", 'integer' => "\e[94m", @@ -232,7 +230,7 @@ class TreeList implements Countable, IteratorAggregate 'NULL' => "\e[31m", default => "", }; - echo json_encode($entry->node->value, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE); + echo json_encode($node->value, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE); } } diff --git a/src/Tree/ArrayNode.php b/src/Tree/ArrayNode.php index 717a3e7..88c042d 100644 --- a/src/Tree/ArrayNode.php +++ b/src/Tree/ArrayNode.php @@ -21,10 +21,9 @@ class ArrayNode extends Node implements CollapsibleNode $this->items = array_values($this->items); } - public function __clone() + public function __clone(): void { - $items = array_map(fn($v) => clone $v, $this->items); - return new ArrayNode($items); + $this->items = array_map(fn($v) => clone $v, $this->items); } public function jsonSerialize(): mixed diff --git a/src/Tree/ObjectNode.php b/src/Tree/ObjectNode.php index 0c51807..3b36e12 100644 --- a/src/Tree/ObjectNode.php +++ b/src/Tree/ObjectNode.php @@ -37,13 +37,12 @@ class ObjectNode extends Node implements CollapsibleNode unset($this->properties[$key]); } - public function __clone() + public function __clone(): void { - $properties = array_combine( + $this->properties = array_combine( array_keys($this->properties), array_map(fn($v) => clone $v, $this->properties) ); - return new ObjectNode($properties); } public function jsonSerialize(): mixed