From 338449824f3c4e5594a02407992d2c53343125fc Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Tue, 1 Oct 2024 22:44:11 +0200 Subject: [PATCH] Improve prompts, treat invalid json as string on insert/edit --- src/Editor/Editor.php | 29 +++++++++++++++++++++-------- src/Editor/Menu.php | 2 +- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/Editor/Editor.php b/src/Editor/Editor.php index 78ddff9..d3e50a7 100644 --- a/src/Editor/Editor.php +++ b/src/Editor/Editor.php @@ -82,7 +82,7 @@ class Editor $this->redrawEditor(); break; case 'k{DOWN}': - $this->currentRow = min(count($this->list), $this->currentRow + 1); + $this->currentRow = min(count($this->list) - 1, $this->currentRow + 1); $this->redrawEditor(); break; case 'k{PGUP}': @@ -102,7 +102,7 @@ class Editor $parent = $this->list->getEntryForIndex($parentIndex); if ($parent->node instanceof ObjectNode) { - $newVal = $this->ask("\e[33;1mnew key:\e[0m ", $entry->key); + $newVal = $this->ask("\e[0;33mnew key:\e[0m ", $entry->key); if ($newVal !== null) { $entry->key = $newVal; $this->redrawEditor(); @@ -121,6 +121,10 @@ class Editor $newVal = $this->ask("\e[33;1mnew value:\e[0m ", $val); if ($newVal !== null) { $val = json_decode($newVal); + // If the string decodes to null, but isn't 'null', treat it as a string + if ($val === null && $newVal !== 'null') { + $val = $newVal; + } $node->value = $val; $this->redrawEditor(); } else { @@ -287,7 +291,7 @@ class Editor $coll = $this->list->findNearestCollection($this->currentRow); $node = $this->list->getNodeForIndex($coll); if ($node instanceof ObjectNode) { - $key = $this->ask("\e[97mkey:\e[0m "); + $key = $this->ask("\e[0;33mkey:\e[0m "); if ($key === null) { $this->redrawInfoBar(); return; @@ -295,9 +299,15 @@ class Editor } if ($value === null) - $value = $this->ask("\e[97mvalue:\e[0m "); + $value = $this->ask("\e[0;32mvalue:\e[0m "); if ($value !== null) { - $value = json_decode($value); + $newvalue = json_decode($value); + // If the string decodes to null, but isn't 'null', treat it as a string + if ($newvalue === null && $value !== 'null') { + $newvalue = $value; + } + $value = $newvalue; + $valueNode = match (true) { is_array($value) => new ArrayNode([]), is_object($value) => new ObjectNode([]), @@ -411,10 +421,13 @@ class Editor $keys = [ 'e' => 'Edit', 'E' => 'Edit key', - 'i' => 'Insert', + 'I' => 'Insert', + 'i' => 'Insert value', 'D' => 'Delete', - 'C' => 'Copy', - 'P' => 'Paste', + //'C' => 'Copy', + //'P' => 'Paste', + '^R' => 'Read', + '^W' => 'Write', '^C' => 'Exit', ]; diff --git a/src/Editor/Menu.php b/src/Editor/Menu.php index dc1eafa..615ea2e 100644 --- a/src/Editor/Menu.php +++ b/src/Editor/Menu.php @@ -86,7 +86,7 @@ class Menu $item = $item . str_repeat(" ", $width - 2 - mb_strlen($item)) . "\e[40;37m"; $item = (($n == $this->index)?"\e[37;44m":"\e[40;37m") . $item; $this->terminal - ->writeAt($left, $top + 3 + $n, self::U_EDGE_VERTICAL.$item.self::U_EDGE_VERTICAL_SCROLL); + ->writeAt($left, $top + 3 + $n, self::U_EDGE_VERTICAL.$item.self::U_EDGE_VERTICAL_THUMB); } echo "\e[0m"; }