diff --git a/src/Editor/Editor.php b/src/Editor/Editor.php index db56156..e65bcc8 100644 --- a/src/Editor/Editor.php +++ b/src/Editor/Editor.php @@ -56,8 +56,6 @@ class Editor */ public function loadFile(string $filename): void { - $this->filename = $filename; - $this->shortfilename = basename($filename); $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); switch ($ext) { case 'json': @@ -70,7 +68,7 @@ class Editor default: throw new \RuntimeException("Unable to read file of type {$ext}"); } - $this->setWindowTitle($this->shortfilename." - JSONEdit"); + $this->setOpenedFilename($filename); $this->document->load($doc); $this->list->parseTree(); } @@ -317,44 +315,7 @@ class Editor break; case "\x13": // ctrl-s - $sel = null; - $mark = "\u{f657} "; - $clear = "\u{f630} "; - - do { - $this->redrawEditor(); - $items = [ - 'collapseBefore' => (Settings::$collapseBefore?$mark:$clear)."Collapse before key", - 'compactGroups' => (Settings::$compactGroups?$mark:$clear)."Compact groups", - 'quotedKeys' => (Settings::$editorQuotedKeys?$mark:$clear)."Quoted keys", - 'tailLine' => (Settings::$tailLine?$mark:$clear)."Show tail line", - 'indentationGuides' => (Settings::$indentationGuides?$mark:$clear)."Show indentation guides", - 'done' => " Done" - ]; - $sel = (new Menu($this->term, $items, "Settings")) - ->display(0, 0, 40, 0, $sel); - switch ($sel) { - case 'collapseBefore': - Settings::$collapseBefore = !Settings::$collapseBefore; - break; - case 'compactGroups': - Settings::$compactGroups = !Settings::$compactGroups; - $this->list->parseTree(); - break; - case 'quotedKeys': - Settings::$editorQuotedKeys = !Settings::$editorQuotedKeys; - break; - case 'tailLine': - Settings::$tailLine = !Settings::$tailLine; - break; - case 'indentationGuides': - Settings::$indentationGuides = !Settings::$indentationGuides; - break; - case null: - break(2); - } - } while ($sel != 'done'); - $this->redrawEditor(); + $this->doSettingsMenu(); break; case "\x0F": // ctrl-o @@ -480,13 +441,10 @@ class Editor return false; } - $this->filename = $saveTo; - $this->shortfilename = basename($saveTo); + $this->setOpenedFilename($saveTo); $this->modified = false; $this->redrawEditor(); - $this->setWindowTitle($this->shortfilename." - JSONEdit"); - $this->showMessage("\e[97;42mWrote to {$saveTo}"); return true; @@ -566,8 +524,7 @@ class Editor } } } - $this->filename = $sel; - $this->shortfilename = basename($sel); + $this->setOpenedFilename($sel); $this->redrawEditor(); $this->showMessage("\e[42;97mOpened {$this->shortfilename}"); } @@ -825,6 +782,48 @@ class Editor } } + public function doSettingsMenu(): void + { + $sel = null; + $mark = "\u{f657} "; + $clear = "\u{f630} "; + + do { + $this->redrawEditor(); + $this->redrawInfoBar([ "^C" => "Close", "↑↓" => "Navigate", "Enter" => "Toggle/Select" ]); + $items = [ + 'collapseBefore' => (Settings::$collapseBefore?$mark:$clear)."Collapse before key", + 'compactGroups' => (Settings::$compactGroups?$mark:$clear)."Compact groups", + 'quotedKeys' => (Settings::$editorQuotedKeys?$mark:$clear)."Quoted keys", + 'tailLine' => (Settings::$tailLine?$mark:$clear)."Show tail line", + 'indentationGuides' => (Settings::$indentationGuides?$mark:$clear)."Show indentation guides", + //'done' => " Done" + ]; + $sel = (new Menu($this->term, $items, "Settings")) + ->display(0, 0, 40, 0, $sel); + switch ($sel) { + case 'collapseBefore': + Settings::$collapseBefore = !Settings::$collapseBefore; + break; + case 'compactGroups': + Settings::$compactGroups = !Settings::$compactGroups; + $this->list->parseTree(); + break; + case 'quotedKeys': + Settings::$editorQuotedKeys = !Settings::$editorQuotedKeys; + break; + case 'tailLine': + Settings::$tailLine = !Settings::$tailLine; + break; + case 'indentationGuides': + Settings::$indentationGuides = !Settings::$indentationGuides; + break; + } + } while ($sel); + $this->redrawEditor(); + + } + /** * Ask for input * @@ -1018,6 +1017,13 @@ class Editor } + private function setOpenedFilename(string $filename): void + { + $this->filename = $filename; + $this->shortfilename = basename($filename); + $this->setWindowTitle($this->shortfilename." - JSONEdit"); + } + private function setWindowTitle(string $title): void { echo "\e]2;{$title}\x07";