Improve prompts, treat invalid json as string on insert/edit

This commit is contained in:
Chris 2024-10-01 22:44:11 +02:00
parent 88abeaa896
commit 338449824f
2 changed files with 22 additions and 9 deletions

View File

@ -82,7 +82,7 @@ class Editor
$this->redrawEditor(); $this->redrawEditor();
break; break;
case 'k{DOWN}': case 'k{DOWN}':
$this->currentRow = min(count($this->list), $this->currentRow + 1); $this->currentRow = min(count($this->list) - 1, $this->currentRow + 1);
$this->redrawEditor(); $this->redrawEditor();
break; break;
case 'k{PGUP}': case 'k{PGUP}':
@ -102,7 +102,7 @@ class Editor
$parent = $this->list->getEntryForIndex($parentIndex); $parent = $this->list->getEntryForIndex($parentIndex);
if ($parent->node instanceof ObjectNode) { 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) { if ($newVal !== null) {
$entry->key = $newVal; $entry->key = $newVal;
$this->redrawEditor(); $this->redrawEditor();
@ -121,6 +121,10 @@ class Editor
$newVal = $this->ask("\e[33;1mnew value:\e[0m ", $val); $newVal = $this->ask("\e[33;1mnew value:\e[0m ", $val);
if ($newVal !== null) { if ($newVal !== null) {
$val = json_decode($newVal); $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; $node->value = $val;
$this->redrawEditor(); $this->redrawEditor();
} else { } else {
@ -287,7 +291,7 @@ class Editor
$coll = $this->list->findNearestCollection($this->currentRow); $coll = $this->list->findNearestCollection($this->currentRow);
$node = $this->list->getNodeForIndex($coll); $node = $this->list->getNodeForIndex($coll);
if ($node instanceof ObjectNode) { if ($node instanceof ObjectNode) {
$key = $this->ask("\e[97mkey:\e[0m "); $key = $this->ask("\e[0;33mkey:\e[0m ");
if ($key === null) { if ($key === null) {
$this->redrawInfoBar(); $this->redrawInfoBar();
return; return;
@ -295,9 +299,15 @@ class Editor
} }
if ($value === null) if ($value === null)
$value = $this->ask("\e[97mvalue:\e[0m "); $value = $this->ask("\e[0;32mvalue:\e[0m ");
if ($value !== null) { 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) { $valueNode = match (true) {
is_array($value) => new ArrayNode([]), is_array($value) => new ArrayNode([]),
is_object($value) => new ObjectNode([]), is_object($value) => new ObjectNode([]),
@ -411,10 +421,13 @@ class Editor
$keys = [ $keys = [
'e' => 'Edit', 'e' => 'Edit',
'E' => 'Edit key', 'E' => 'Edit key',
'i' => 'Insert', 'I' => 'Insert',
'i' => 'Insert value',
'D' => 'Delete', 'D' => 'Delete',
'C' => 'Copy', //'C' => 'Copy',
'P' => 'Paste', //'P' => 'Paste',
'^R' => 'Read',
'^W' => 'Write',
'^C' => 'Exit', '^C' => 'Exit',
]; ];

View File

@ -86,7 +86,7 @@ class Menu
$item = $item . str_repeat(" ", $width - 2 - mb_strlen($item)) . "\e[40;37m"; $item = $item . str_repeat(" ", $width - 2 - mb_strlen($item)) . "\e[40;37m";
$item = (($n == $this->index)?"\e[37;44m":"\e[40;37m") . $item; $item = (($n == $this->index)?"\e[37;44m":"\e[40;37m") . $item;
$this->terminal $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"; echo "\e[0m";
} }