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();
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',
];

View File

@ -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";
}