Add compact mode, toggle with 'c'

This commit is contained in:
Chris 2024-10-02 02:14:36 +02:00
parent bf6756773e
commit be63775334
3 changed files with 28 additions and 10 deletions

View File

@ -216,6 +216,12 @@ class Editor
$this->redrawEditor();
break;
case "c":
Settings::$compactGroups = !Settings::$compactGroups;
$this->list->parseTree();
$this->redrawEditor();
break;
case "\x0e": // ctrl-n
$this->document->load((object)[]);
$this->list->parseTree();
@ -359,6 +365,8 @@ class Editor
e Edit selected value
E Edit selected key
D Delete selected key
c Toggle compact list view
q Toggle quoted keys in list view
^W Write to file
^R Read from file
^N New document with empty object

View File

@ -50,11 +50,13 @@ class TreeList implements Countable
foreach ($node->items as $item) {
$this->parseNode($item, [ ...$path, $key ], $index++);
}
if (!Settings::$compactGroups)
$this->list[$entryKey.'$'] = new Entry(depth: $level, key: $key, node: $node, closer: true);
} elseif ($node instanceof ObjectNode) {
foreach ($node->properties as $nodekey=>$item) {
$this->parseNode($item, [ ...$path, $key ], $nodekey);
}
if (!Settings::$compactGroups)
$this->list[$entryKey.'$'] = new Entry(depth: $level, key: $key, node: $node, closer: true);
}
@ -150,11 +152,13 @@ class TreeList implements Countable
echo "\e[90m".str_repeat("\u{258f} ",$entry->depth)."\e[37m";
if ($entry->closer) {
if (!Settings::$compactGroups) {
if ($entry->node instanceof ArrayNode) {
echo "]";
} elseif ($entry->node instanceof ObjectNode) {
echo "}";
}
}
return;
}
@ -167,9 +171,9 @@ class TreeList implements Countable
));
}
if ($entry->node instanceof ArrayNode) {
echo "[";
echo "[" . (Settings::$compactGroups ? "…]":"");
} elseif ($entry->node instanceof ObjectNode) {
echo "{";
echo "{" . (Settings::$compactGroups ? "…}":"");
} elseif ($entry->node instanceof ValueNode) {
$value = $entry->node->value;
echo match (gettype($value)) {

View File

@ -6,18 +6,22 @@ class Settings
{
public static bool $editorQuotedKeys = false;
public static bool $compactGroups = false;
public static function load(string $filename): void
{
if (file_exists($filename) && is_readable($filename)) {
$data = json_decode(file_get_contents($filename));
self::$editorQuotedKeys = $data->editorQuotedKeys ?? false;
self::$compactGroups = $data->compactGroups ?? false;
}
}
public static function save(string $filename): void
{
$data = json_encode([
'editorQuotedKeys' => self::$editorQuotedKeys
'editorQuotedKeys' => self::$editorQuotedKeys,
'compactGroups' => self::$compactGroups,
], JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT)."\n";
@file_put_contents($filename, $data);
}
@ -27,6 +31,8 @@ class Settings
switch ($key) {
case 'editorQuotedKeys':
self::$editorQuotedKeys = (bool)$value; break;
case 'compactGroups':
self::$compactGroups = (bool)$value; break;
}
}
}