Add messagebox, bugfixes

This commit is contained in:
2024-10-02 00:53:11 +02:00
parent 24f569070f
commit f23c378020
6 changed files with 239 additions and 5 deletions

View File

@ -3,6 +3,7 @@
namespace NoccyLabs\JEdit\Editor;
use NoccyLabs\JEdit\List\TreeList;
use NoccyLabs\JEdit\Settings;
use NoccyLabs\JEdit\Terminal\Terminal;
use NoccyLabs\JEdit\Tree\ArrayNode;
use NoccyLabs\JEdit\Tree\ObjectNode;
@ -27,6 +28,8 @@ class Editor
private bool $running = true;
private bool $modified = false;
/**
* Constructor
*
@ -137,10 +140,12 @@ class Editor
$collNode->removeIndex($deleteKey);
$this->list->parseTree();
$this->redrawEditor();
$this->modified = true;
} elseif ($collNode instanceof ObjectNode) {
$collNode->unset($deleteKey);
$this->list->parseTree();
$this->redrawEditor();
$this->modified = true;
} else {
$this->term->setCursor(1, $h);
echo "\e[97;41mCan only delete from object, array\e[K\e[0m";
@ -175,18 +180,58 @@ class Editor
switch ($sel) {
case 'value':
$this->doInsertValue();
$this->modified = true;
break;
case 'array':
$this->doInsertValue('[]');
$this->modified = true;
break;
case 'object':
$this->doInsertValue('{}');
$this->modified = true;
break;
}
break;
case 'h':
$text = <<<EOT
Welcome to JSONEdit! The editor you have missed all this time without even knowing it!
To get started, press I (shift-i) and add something to the document. Use the arrow keys to get around. To cancel a prompt, close a menu or close a dialog, press ctrl-C. When you are happy with your work, press ctrl-W and enter a filename to write. You can also press ctrl-R and read in a new file, overwriting your masterpiece.
↑↓ Navigate values in document
i Insert a new value
I Insert value, array or object
e Edit selected value
E Edit selected key
D Delete selected key
^W Write to file
^R Read from file
^N New document with empty object
^C Cancel/Exit
This is beta software, if not alpha. It kinda works, but there will be issues. Feel free to help out with a patch, or by filing bug reports.
Known issues include:
* Editing long lines will blow up. Don't try to edit anything longer than the terminal is wide.
* There is no fullscreen editing, so verbatim blocks in twig will probably not work well either.
* Comments are not preserved.
* Files are overwritten without confirmation.
* There is no command mode, no search.
* Some things just don't work yet.
* Unhandled keys will appear in the bottom left of the screen with a delay.
* Folding is not yet implemented.
Go to https://dev.noccylabs.info/noccy/jsonedit to find the source code, issue tracker, and learn more about the project!
EOT;
$msg = new MessageBox($this->term, $text, "Help (press ctrl-C to close)");
$msg->display(5, 3, 70, 20);
$this->redrawEditor();
break;
case 'i':
$this->doInsertValue();
$this->modified = true;
break;
case 'Q':
@ -194,6 +239,20 @@ class Editor
$this->running = false;
break;
case "q":
Settings::$editorQuotedKeys = !Settings::$editorQuotedKeys;
$this->redrawEditor();
break;
case "\x0e": // ctrl-n
$this->document->load((object)[]);
$this->list->parseTree();
$this->filename = "untitled.json";
$this->shortfilename = "untitled.json";
$this->modified = false;
$this->redrawEditor();
break;
case "\x12": // ctrl-r
$this->doReadFile();
break;
@ -211,6 +270,8 @@ class Editor
}
}
Settings::save(SETTINGS_FILE);
// for ($n = 0; $n < 20; $n++) {
// $this->currentRow = $n;
// $this->redrawEditor();
@ -278,7 +339,6 @@ class Editor
$doc = $this->document->save();
$this->term->setCursor(1, $h);
$ext = strtolower(pathinfo($saveTo, PATHINFO_EXTENSION));
switch ($ext) {
@ -291,11 +351,19 @@ class Editor
file_put_contents($saveTo, Yaml::dump($doc));
break;
default:
$this->term->setCursor(1, $h);
echo "\e[97;41mUnable to write format: {$ext}\e[K\e[0m";
}
$this->filename = $saveTo;
$this->shortfilename = basename($saveTo);
$this->modified = false;
$this->redrawEditor();
$this->term->setCursor(1, $h);
echo "\e[97;42mWrote to {$saveTo}\e[K\e[0m";
}
/**
@ -316,7 +384,9 @@ class Editor
if ($parent->node instanceof ObjectNode) {
$newVal = $this->ask("\e[0;33mnew key:\e[0m ", $entry->key);
if ($newVal !== null) {
$parent->node->rename($entry->key, $newVal);
$entry->key = $newVal;
$this->list->parseTree();
$this->redrawEditor();
}
} else {
@ -347,6 +417,7 @@ class Editor
$val = $newVal;
}
$node->value = $val;
$this->modified = true;
$this->redrawEditor();
} else {
$this->redrawInfoBar();
@ -395,6 +466,7 @@ class Editor
} elseif ($node instanceof ObjectNode) {
$node->set($key, $valueNode);
}
$this->modified = true;
$this->list->parseTree();
$this->redrawEditor();
} else {
@ -477,8 +549,9 @@ class Editor
$path = $this->list->getPathForIndex($this->currentRow);
$node = $this->list->getNodeForIndex($this->currentRow);
$modified = $this->modified ? "\e[31m*\e[37m" : "";
$this->term->setCursor(1, $h-1);
echo "\e[40;37m\e[K\e[1m{$this->shortfilename}\e[22m#\e[3m{$path}\e[37;23m";
echo "\e[40;37m\e[K{$modified}\e[1m{$this->shortfilename}\e[22m#\e[3m{$path}\e[37;23m";
//$this->term->setCursor(1, $h);
echo " = ";