Clean up code, add comments

This commit is contained in:
Chris 2024-10-01 22:51:58 +02:00
parent 338449824f
commit 9ca53df0ec

View File

@ -25,6 +25,13 @@ class Editor
private ?string $shortfilename = "untitled.json"; private ?string $shortfilename = "untitled.json";
private bool $running = true;
/**
* Constructor
*
* @param Terminal $term
*/
public function __construct(private Terminal $term) public function __construct(private Terminal $term)
{ {
$this->document = new Tree(); $this->document = new Tree();
@ -40,6 +47,12 @@ class Editor
$this->list = new TreeList($this->document); $this->list = new TreeList($this->document);
} }
/**
* Read a file into the Tree and TreeList
*
* @param string $filename
* @return void
*/
public function loadFile(string $filename): void public function loadFile(string $filename): void
{ {
$this->filename = $filename; $this->filename = $filename;
@ -60,14 +73,23 @@ class Editor
$this->list->parseTree(); $this->list->parseTree();
} }
/**
* Load a document from array/object
*
* @param mixed $document
* @return void
*/
public function loadDocument(mixed $document): void public function loadDocument(mixed $document): void
{ {
$this->document->load($document); $this->document->load($document);
$this->list->parseTree(); $this->list->parseTree();
} }
private bool $running = true; /**
* Run the editor
*
* @return void
*/
public function run() public function run()
{ {
$this->list->parseTree(); $this->list->parseTree();
@ -95,45 +117,11 @@ class Editor
break; break;
case 'E': case 'E':
//$coll = $this->list->findNearestCollection($this->currentRow); $this->doEditKey();
$entry = $this->list->getEntryForIndex($this->currentRow);
$path = $this->list->getPathForIndex($this->currentRow);
$parentIndex = $this->list->getIndexForPath(dirname($path));
$parent = $this->list->getEntryForIndex($parentIndex);
if ($parent->node instanceof ObjectNode) {
$newVal = $this->ask("\e[0;33mnew key:\e[0m ", $entry->key);
if ($newVal !== null) {
$entry->key = $newVal;
$this->redrawEditor();
}
} else {
$this->term->setCursor(1, $h);
echo "\e[97;41mCan only edit keys on objects\e[K\e[0m";
}
break; break;
case 'e': case 'e':
//$coll = $this->list->findNearestCollection($this->currentRow); $this->doEditValue();
$node = $this->list->getNodeForIndex($this->currentRow);
if ($node instanceof ValueNode) {
$val = json_encode($node->value, JSON_UNESCAPED_SLASHES);
$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 {
$this->redrawInfoBar();
}
} else {
$this->term->setCursor(1, $h);
echo "\e[97;41mCan not edit array/object\e[K\e[0m";
}
break; break;
case '[': case '[':
@ -207,6 +195,39 @@ class Editor
break; break;
case "\x12": // ctrl-r case "\x12": // ctrl-r
$this->doReadFile();
break;
case "\x17": // ctrl-w
$this->doWriteFile();
break;
case null:
break;
default:
$this->term->setCursor(1, $h, true);
echo sprintf("%s %02x %03d", ctype_print($read)?$read:'.', ord($read), ord($read));
sleep(1);
}
}
// for ($n = 0; $n < 20; $n++) {
// $this->currentRow = $n;
// $this->redrawEditor();
// sleep(1);
// }
}
/**
* Handler for read file command (ctrl-R)
*
* @return void
*/
private function doReadFile(): void
{
[$w,$h] = $this->term->getSize();
$readFrom = $this->ask("\e[33mRead from:\e[0m ", ""); $readFrom = $this->ask("\e[33mRead from:\e[0m ", "");
$this->term->setCursor(1, $h); $this->term->setCursor(1, $h);
@ -242,10 +263,17 @@ class Editor
$this->term->setCursor(1, $h); $this->term->setCursor(1, $h);
echo "\e[97;42mLoaded {$readFrom}\e[K\e[0m"; echo "\e[97;42mLoaded {$readFrom}\e[K\e[0m";
break; }
/**
* Handler for the write file command (ctrl-W)
*
* @return void
*/
private function doWriteFile(): void
{
[$w,$h] = $this->term->getSize();
case "\x17": // ctrl-w
$saveTo = $this->ask("\e[33mWrite to:\e[0m ", $this->filename); $saveTo = $this->ask("\e[33mWrite to:\e[0m ", $this->filename);
$doc = $this->document->save(); $doc = $this->document->save();
@ -267,25 +295,74 @@ class Editor
} }
echo "\e[97;42mWrote to {$saveTo}\e[K\e[0m"; echo "\e[97;42mWrote to {$saveTo}\e[K\e[0m";
break;
case null:
break;
default:
$this->term->setCursor(1, $h, true);
echo sprintf("%s %02x %03d", ctype_print($read)?$read:'.', ord($read), ord($read));
sleep(1);
}
}
// for ($n = 0; $n < 20; $n++) {
// $this->currentRow = $n;
// $this->redrawEditor();
// sleep(1);
// }
} }
/**
* Edit selected key
*
* @return void
*/
private function doEditKey(): void
{
[$w,$h] = $this->term->getSize();
//$coll = $this->list->findNearestCollection($this->currentRow);
$entry = $this->list->getEntryForIndex($this->currentRow);
$path = $this->list->getPathForIndex($this->currentRow);
$parentIndex = $this->list->getIndexForPath(dirname($path));
$parent = $this->list->getEntryForIndex($parentIndex);
if ($parent->node instanceof ObjectNode) {
$newVal = $this->ask("\e[0;33mnew key:\e[0m ", $entry->key);
if ($newVal !== null) {
$entry->key = $newVal;
$this->redrawEditor();
}
} else {
$this->term->setCursor(1, $h);
echo "\e[97;41mCan only edit keys on objects\e[K\e[0m";
}
}
/**
* Edit selected value
*
* @return void
*/
private function doEditValue(): void
{
[$w,$h] = $this->term->getSize();
//$coll = $this->list->findNearestCollection($this->currentRow);
$node = $this->list->getNodeForIndex($this->currentRow);
if ($node instanceof ValueNode) {
$val = json_encode($node->value, JSON_UNESCAPED_SLASHES);
$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 {
$this->redrawInfoBar();
}
} else {
$this->term->setCursor(1, $h);
echo "\e[97;41mCan not edit array/object\e[K\e[0m";
}
}
/**
* Insert a new value
*
* @param mixed $value
* @return void
*/
private function doInsertValue(mixed $value = null): void private function doInsertValue(mixed $value = null): void
{ {
$coll = $this->list->findNearestCollection($this->currentRow); $coll = $this->list->findNearestCollection($this->currentRow);
@ -325,6 +402,13 @@ class Editor
} }
} }
/**
* Ask for input
*
* @param string $prompt
* @param string $value
* @return string|null
*/
public function ask(string $prompt, $value = ''): ?string public function ask(string $prompt, $value = ''): ?string
{ {
$plainPrompt = preg_replace('<\e\[.+?m>', '', $prompt); $plainPrompt = preg_replace('<\e\[.+?m>', '', $prompt);
@ -378,6 +462,11 @@ class Editor
} }
/**
* Redraw the editor
*
* @return void
*/
public function redrawEditor() public function redrawEditor()
{ {
[$w,$h] = $this->term->getSize(); [$w,$h] = $this->term->getSize();
@ -414,6 +503,11 @@ class Editor
// echo "\e[90;3m«Empty»\e[0m"; // echo "\e[90;3m«Empty»\e[0m";
} }
/**
* Redraw the info bar
*
* @return void
*/
private function redrawInfoBar() private function redrawInfoBar()
{ {
[$w,$h] = $this->term->getSize(); [$w,$h] = $this->term->getSize();