Misc fixes

This commit is contained in:
Chris 2024-10-07 00:30:09 +02:00
parent 46bf135446
commit 0e1354bd47
6 changed files with 33 additions and 32 deletions

View File

@ -403,7 +403,7 @@ class Editor
/** /**
* Handler for the write file command (ctrl-W) * Handler for the write file command (ctrl-W)
* *
* @return void * @return bool
*/ */
private function doWriteFile(): bool private function doWriteFile(): bool
{ {
@ -501,7 +501,7 @@ class Editor
}) })
->setItems($items) ->setItems($items)
->setTitle($wd) ->setTitle($wd)
->display(0, 0, 70, 20, $this->filename); ->display(0, 0, 70, 0, $this->filename);
if ($sel === null) { if ($sel === null) {
$this->redrawEditor(); $this->redrawEditor();
return; return;
@ -536,8 +536,8 @@ class Editor
$width = min(90, $w - 10); $width = min(90, $w - 10);
$height = min(40, $h - 6); $height = min(40, $h - 6);
$left = round(($w / 2) - ($width / 2)); $left = (int)round(($w / 2) - ($width / 2));
$top = round(($h / 2) - ($height / 2)); $top = (int)round(($h / 2) - ($height / 2));
$msg = new MessageBox($this->term, self::$helpText, "Help"); $msg = new MessageBox($this->term, self::$helpText, "Help");
$this->redrawInfoBar([ '↑/↓' => 'Scroll', '^C' => 'Close' ]); $this->redrawInfoBar([ '↑/↓' => 'Scroll', '^C' => 'Close' ]);
@ -562,9 +562,9 @@ class Editor
EOT."\n/ Version: ".APP_VERSION."\n/ Built at: ".APP_BUILDDATE; EOT."\n/ Version: ".APP_VERSION."\n/ Built at: ".APP_BUILDDATE;
$width = 60; $width = 60;
$height = 22; $height = 0; // 22;
$left = round(($w / 2) - ($width / 2)); $left = 0; // (int)round(($w / 2) - ($width / 2));
$top = round(($h / 2) - ($height / 2)); $top = 0; // (int)round(($h / 2) - ($height / 2));
$msg = new MessageBox($this->term, $text, "About JSONEdit ".APP_VERSION); $msg = new MessageBox($this->term, $text, "About JSONEdit ".APP_VERSION);
$this->redrawInfoBar([ '↑/↓' => 'Scroll', '^C' => 'Close' ]); $this->redrawInfoBar([ '↑/↓' => 'Scroll', '^C' => 'Close' ]);
@ -654,7 +654,7 @@ class Editor
$deleteKey = basename($path); $deleteKey = basename($path);
$collNode = $this->list->getNodeForPath($parentPath); $collNode = $this->list->getNodeForPath($parentPath);
if ($collNode instanceof ArrayNode) { if ($collNode instanceof ArrayNode) {
$collNode->removeIndex($deleteKey); $collNode->removeIndex(intval($deleteKey));
$this->list->parseTree(); $this->list->parseTree();
$this->redrawEditor(); $this->redrawEditor();
$this->modified = true; $this->modified = true;

View File

@ -83,12 +83,12 @@ class Menu
* @param string|int|null $value * @param string|int|null $value
* @return mixed * @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; //$this->index = 0;
$keys = array_keys($this->items); $keys = array_keys($this->items);
$this->index = array_search($value, $keys) ?? 0; $this->index = array_search($value, $keys) ?: 0;
[$w,$h] = $this->terminal->getSize(); [$w,$h] = $this->terminal->getSize();
if ($height == 0) { if ($height == 0) {
@ -187,7 +187,7 @@ class Menu
$thumbTop = round(($visibleItems - 1) / count($this->items) * $scrollTop); $thumbTop = round(($visibleItems - 1) / count($this->items) * $scrollTop);
$thumbBottom = round(($visibleItems) / count($this->items) * $scrollBottom); $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 // draw head
echo "\e[40;37m"; echo "\e[40;37m";

View File

@ -53,13 +53,15 @@ class MessageBox
[$w,$h] = $this->terminal->getSize(); [$w,$h] = $this->terminal->getSize();
if ($height == 0) { 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) { if ($left == 0) {
$left = round(($w - $width) / 2); $left = round(($w - $width) / 2);
} }
if ($top == 0) { if ($top == 0) {
$top = round(($h - $height) / 2); $top = (int)round(($h - $height) / 2);
} }
$maxScroll = (count($wrapped) > $height) ? count($wrapped) - $height + 4 : 0; $maxScroll = (count($wrapped) > $height) ? count($wrapped) - $height + 4 : 0;
@ -127,6 +129,7 @@ class MessageBox
* @return void * @return void
*/ */
private function redraw(int $left, int $top, int $width, int $height, array $wrapped, string $title, int $maxScroll) { private function redraw(int $left, int $top, int $width, int $height, array $wrapped, string $title, int $maxScroll) {
$visibleItems = $height - 4; $visibleItems = $height - 4;
// calculate scrollbar thumb positions // 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) ->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++) { for ($n = 0; $n < $visibleItems; $n++) {
$pre = '';
$post = '';
if (isset($wrapped[$n+$this->scroll])) { if (isset($wrapped[$n+$this->scroll])) {
$line = $wrapped[$n+$this->scroll]??null; $line = $wrapped[$n+$this->scroll]??null;
if (str_starts_with($line, "# ")) { if (str_starts_with($line, "# ")) {

View File

@ -152,10 +152,7 @@ class TreeList implements Countable, IteratorAggregate
return; return;
} }
$entry = $this->list[$key]; $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 "\e[{$screenRow};1H\e[0m";
echo ($selected?"\e[44;97m":"\e[0;37m")."\e[K"; echo ($selected?"\e[44;97m":"\e[0;37m")."\e[K";
echo "\e[90m".str_repeat( echo "\e[90m".str_repeat(
@ -204,26 +201,27 @@ class TreeList implements Countable, IteratorAggregate
: "\e[36m{$entry->key}:\e[37m " : "\e[36m{$entry->key}:\e[37m "
)); ));
} }
if ($entry->node instanceof ArrayNode) { $node = $entry->node;
if ($node instanceof ArrayNode) {
echo "[" . (Settings::$compactGroups ? "…]":""); echo "[" . (Settings::$compactGroups ? "…]":"");
if ($entry->node->isCollapsed()) { if ($node->isCollapsed()) {
if (!Settings::$collapseBefore) echo " \e[90m\u{25ba}"; 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 ]"; if (!Settings::$compactGroups) echo "\e[37m ]";
} else { } else {
if (!Settings::$collapseBefore) echo " \e[90m\u{25bc}"; if (!Settings::$collapseBefore) echo " \e[90m\u{25bc}";
} }
} elseif ($entry->node instanceof ObjectNode) { } elseif ($node instanceof ObjectNode) {
echo "{" . (Settings::$compactGroups ? "…}":""); echo "{" . (Settings::$compactGroups ? "…}":"");
if ($entry->node->isCollapsed()) { if ($node->isCollapsed()) {
if (!Settings::$collapseBefore) echo " \e[90m\u{25ba}"; 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 }"; if (!Settings::$compactGroups) echo "\e[37m }";
} else { } else {
if (!Settings::$collapseBefore) echo " \e[90m\u{25bc}"; if (!Settings::$collapseBefore) echo " \e[90m\u{25bc}";
} }
} elseif ($entry->node instanceof ValueNode) { } elseif ($node instanceof ValueNode) {
$value = $entry->node->value; $value = $node->value;
echo match (gettype($value)) { echo match (gettype($value)) {
'string' => "\e[33m", 'string' => "\e[33m",
'integer' => "\e[94m", 'integer' => "\e[94m",
@ -232,7 +230,7 @@ class TreeList implements Countable, IteratorAggregate
'NULL' => "\e[31m", 'NULL' => "\e[31m",
default => "", default => "",
}; };
echo json_encode($entry->node->value, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE); echo json_encode($node->value, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
} }
} }

View File

@ -21,10 +21,9 @@ class ArrayNode extends Node implements CollapsibleNode
$this->items = array_values($this->items); $this->items = array_values($this->items);
} }
public function __clone() public function __clone(): void
{ {
$items = array_map(fn($v) => clone $v, $this->items); $this->items = array_map(fn($v) => clone $v, $this->items);
return new ArrayNode($items);
} }
public function jsonSerialize(): mixed public function jsonSerialize(): mixed

View File

@ -37,13 +37,12 @@ class ObjectNode extends Node implements CollapsibleNode
unset($this->properties[$key]); unset($this->properties[$key]);
} }
public function __clone() public function __clone(): void
{ {
$properties = array_combine( $this->properties = array_combine(
array_keys($this->properties), array_keys($this->properties),
array_map(fn($v) => clone $v, $this->properties) array_map(fn($v) => clone $v, $this->properties)
); );
return new ObjectNode($properties);
} }
public function jsonSerialize(): mixed public function jsonSerialize(): mixed