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

View File

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

View File

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