From be63775334b5a04c9829c2543b5607e0ca8db681 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Wed, 2 Oct 2024 02:14:36 +0200 Subject: [PATCH] Add compact mode, toggle with 'c' --- src/Editor/Editor.php | 8 ++++++++ src/List/TreeList.php | 22 +++++++++++++--------- src/Settings.php | 8 +++++++- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/Editor/Editor.php b/src/Editor/Editor.php index a709fa7..f82e4f8 100644 --- a/src/Editor/Editor.php +++ b/src/Editor/Editor.php @@ -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 diff --git a/src/List/TreeList.php b/src/List/TreeList.php index f7200ef..604dc02 100644 --- a/src/List/TreeList.php +++ b/src/List/TreeList.php @@ -50,12 +50,14 @@ class TreeList implements Countable foreach ($node->items as $item) { $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) { foreach ($node->properties as $nodekey=>$item) { $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,11 +152,13 @@ class TreeList implements Countable echo "\e[90m".str_repeat("\u{258f} ",$entry->depth)."\e[37m"; if ($entry->closer) { - if ($entry->node instanceof ArrayNode) { - echo "]"; - } elseif ($entry->node instanceof ObjectNode) { - echo "}"; - } + 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)) { diff --git a/src/Settings.php b/src/Settings.php index 27f3d54..755b457 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -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; } } } \ No newline at end of file