Implement folding (use '+' key), clean up code
This commit is contained in:
		@@ -6,6 +6,7 @@ use NoccyLabs\JsonEdit\List\TreeList;
 | 
			
		||||
use NoccyLabs\JsonEdit\Settings;
 | 
			
		||||
use NoccyLabs\JsonEdit\Terminal\Terminal;
 | 
			
		||||
use NoccyLabs\JsonEdit\Tree\ArrayNode;
 | 
			
		||||
use NoccyLabs\JsonEdit\Tree\CollapsibleNode;
 | 
			
		||||
use NoccyLabs\JsonEdit\Tree\ObjectNode;
 | 
			
		||||
use NoccyLabs\JsonEdit\Tree\Tree;
 | 
			
		||||
use NoccyLabs\JsonEdit\Tree\ValueNode;
 | 
			
		||||
@@ -79,7 +80,7 @@ class Editor
 | 
			
		||||
    /**
 | 
			
		||||
     * Load a document from array/object
 | 
			
		||||
     *
 | 
			
		||||
     * @param mixed $document
 | 
			
		||||
<     * @param mixed $document
 | 
			
		||||
     * @return void
 | 
			
		||||
     */
 | 
			
		||||
    public function loadDocument(mixed $document): void
 | 
			
		||||
@@ -147,8 +148,7 @@ class Editor
 | 
			
		||||
                        $this->redrawEditor();
 | 
			
		||||
                        $this->modified = true;
 | 
			
		||||
                    } else {
 | 
			
		||||
                        $this->term->setCursor(1, $h);
 | 
			
		||||
                        echo "\e[97;41mCan only delete from object, array\e[K\e[0m";
 | 
			
		||||
                        $this->showMessage("\e[97;41mCan only delete from object, array");
 | 
			
		||||
                    }
 | 
			
		||||
                    break;
 | 
			
		||||
 | 
			
		||||
@@ -176,7 +176,7 @@ class Editor
 | 
			
		||||
                        'array' => "Insert Array[]",
 | 
			
		||||
                    ], 'Insert');
 | 
			
		||||
                    $this->redrawInfoBar([ '^C' => 'Cancel', '↑/↓' => 'Select option', 'Enter' => 'Accept' ]);
 | 
			
		||||
                    $sel = $menu->display(5, 2, 30, 0, "value");
 | 
			
		||||
                    $sel = $menu->display(0, 0, 30, 0, "value");
 | 
			
		||||
                    $this->redrawEditor();
 | 
			
		||||
                    switch ($sel) {
 | 
			
		||||
                        case 'value':
 | 
			
		||||
@@ -211,6 +211,15 @@ class Editor
 | 
			
		||||
                    $this->running = false;
 | 
			
		||||
                    break;
 | 
			
		||||
 | 
			
		||||
                case "+":
 | 
			
		||||
                    $node = $this->list->getNodeForIndex($this->currentRow);
 | 
			
		||||
                    if ($node instanceof CollapsibleNode) {
 | 
			
		||||
                        $node->collapse(!$node->isCollapsed());
 | 
			
		||||
                        $this->list->parseTree();
 | 
			
		||||
                        $this->redrawEditor();
 | 
			
		||||
                    }
 | 
			
		||||
                    break;
 | 
			
		||||
 | 
			
		||||
                case "q":
 | 
			
		||||
                    Settings::$editorQuotedKeys = !Settings::$editorQuotedKeys;
 | 
			
		||||
                    $this->redrawEditor();
 | 
			
		||||
@@ -269,13 +278,12 @@ class Editor
 | 
			
		||||
 | 
			
		||||
        $readFrom = $this->ask("\e[33mRead from:\e[0m ", "");
 | 
			
		||||
 | 
			
		||||
        $this->term->setCursor(1, $h);
 | 
			
		||||
        if (!file_exists($readFrom)) {
 | 
			
		||||
            echo "\e[97;41mFile does not exist\e[K\e[0m";
 | 
			
		||||
            $this->showMessage("\e[97;41mFile does not exist");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        if (!is_readable($readFrom)) {
 | 
			
		||||
            echo "\e[97;41mFile not readable\e[K\e[0m";
 | 
			
		||||
            $this->showMessage("\e[97;41mFile not readable");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        $ext = strtolower(pathinfo($readFrom, PATHINFO_EXTENSION));
 | 
			
		||||
@@ -288,7 +296,7 @@ class Editor
 | 
			
		||||
                $doc = Yaml::parseFile($readFrom);
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                echo "\e[97;41mUnable to read format: {$ext}\e[K\e[0m";
 | 
			
		||||
                $this->showMessage("\e[97;41mUnable to read format: {$ext}");
 | 
			
		||||
                return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -300,8 +308,7 @@ class Editor
 | 
			
		||||
        $this->list->parseTree();
 | 
			
		||||
        $this->redrawEditor();
 | 
			
		||||
 | 
			
		||||
        $this->term->setCursor(1, $h);
 | 
			
		||||
        echo "\e[97;42mLoaded {$readFrom}\e[K\e[0m";
 | 
			
		||||
        $this->showMessage("\e[97;42mLoaded {$readFrom}");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -329,8 +336,7 @@ 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->showMessage("\e[97;41mUnable to write format: {$ext}");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->filename = $saveTo;
 | 
			
		||||
@@ -338,8 +344,7 @@ class Editor
 | 
			
		||||
        $this->modified = false;
 | 
			
		||||
        $this->redrawEditor();
 | 
			
		||||
 | 
			
		||||
        $this->term->setCursor(1, $h);
 | 
			
		||||
        echo "\e[97;42mWrote to {$saveTo}\e[K\e[0m";
 | 
			
		||||
        $this->showMessage("\e[97;42mWrote to {$saveTo}");
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
@@ -375,20 +380,20 @@ class Editor
 | 
			
		||||
        ## Doing stuff
 | 
			
		||||
 | 
			
		||||
        ### Adding keys or values
 | 
			
		||||
 | 
			
		||||
        To add a key or a value, navigate to a value in an array or object, or to a specific array or object, and press "i". You will be prompted for the value, and for objects the key.
 | 
			
		||||
 | 
			
		||||
        You can also press "I" to add arrays and objects. Just select what you want to add in the menu and press enter.
 | 
			
		||||
 | 
			
		||||
        ### Editing keys
 | 
			
		||||
 | 
			
		||||
        You can edit keys on objects. For this, press "E".
 | 
			
		||||
 | 
			
		||||
        ### Editing values
 | 
			
		||||
 | 
			
		||||
        To edit a value, press "e". The value is verbatim JSON, so strings should be quoted and all that. Anything that is unparsable JSON will be used as is, resulting in a string.
 | 
			
		||||
 | 
			
		||||
        ### Loading and Saving files
 | 
			
		||||
        To load a file, press ^R and enter the filename to read. To write to a file, press ^W and enter the filename to write to.
 | 
			
		||||
 | 
			
		||||
        ### YAML or JSON?
 | 
			
		||||
        There is no need to select YAML or JSON mode. All operations work the same, and the format is determined on load or save.
 | 
			
		||||
 | 
			
		||||
        # Disclaimer
 | 
			
		||||
 | 
			
		||||
@@ -472,8 +477,9 @@ class Editor
 | 
			
		||||
                $this->redrawEditor();
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            $this->term->setCursor(1, $h);
 | 
			
		||||
            echo "\e[97;41mCan only edit keys on objects\e[K\e[0m";
 | 
			
		||||
            // $this->term->setCursor(1, $h);
 | 
			
		||||
            // echo "\e[97;41mCan only edit keys on objects\e[K\e[0m";
 | 
			
		||||
            $this->showMessage("\e[97;41mCan only edit keys on objects");
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
@@ -505,8 +511,7 @@ class Editor
 | 
			
		||||
                $this->redrawInfoBar();
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            $this->term->setCursor(1, $h);
 | 
			
		||||
            echo "\e[97;41mCan not edit array/object\e[K\e[0m";
 | 
			
		||||
            $this->showMessage("\e[97;41mCan not edit array/object");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -702,4 +707,12 @@ class Editor
 | 
			
		||||
            //echo " \e[1m{$key}\e[2m \e[3m{$info}\e[0m \e[90m\u{2502}\e[0m";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function showMessage(string $message): void
 | 
			
		||||
    {
 | 
			
		||||
        [$w,$h] = $this->term->getSize();
 | 
			
		||||
        $this->term->setCursor(1, $h);
 | 
			
		||||
        echo $message."\e[K\e[0m";
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user