diff --git a/src/Editor/Editor.php b/src/Editor/Editor.php index ba351b4..c1991d2 100644 --- a/src/Editor/Editor.php +++ b/src/Editor/Editor.php @@ -286,29 +286,29 @@ class Editor case "\x0e": // ctrl-n if ($this->modified) { - $menu = new Menu($this->term, [ - 'cancel' => "Return to editor", - 'save' => "Save changes", - 'discard' => "Discard changes", - //'sep0' => "---", - //'__paste' => "Paste from clipboard", - ], 'Document is modified'); - $this->redrawInfoBar([ '^C' => 'Cancel', '↑/↓' => 'Select option', 'Enter' => 'Accept' ]); - $sel = $menu->display(0, 0, 30, 0, "value"); - $this->redrawEditor(); - switch ($sel) { - case 'cancel': - break(2); - case 'save': - if (!$this->doWriteFile()) { - break(2); - } - break; - case 'discard': - break; - } - $this->modified = false; - + if ($this->askModified() === false) break; + // $menu = new Menu($this->term, [ + // 'cancel' => "Return to editor", + // 'save' => "Save changes", + // 'discard' => "Discard changes", + // //'sep0' => "---", + // //'__paste' => "Paste from clipboard", + // ], 'Document is modified'); + // $this->redrawInfoBar([ '^C' => 'Cancel', '↑/↓' => 'Select option', 'Enter' => 'Accept' ]); + // $sel = $menu->display(0, 0, 30, 0, "value"); + // $this->redrawEditor(); + // switch ($sel) { + // case 'cancel': + // break(2); + // case 'save': + // if (!$this->doWriteFile()) { + // break(2); + // } + // break; + // case 'discard': + // break; + // } + // $this->modified = false; } $this->document->load((object)[]); @@ -377,7 +377,7 @@ class Editor { [$w,$h] = $this->term->getSize(); - $readFrom = $this->ask("\e[33mRead from:\e[0m ", ""); + $readFrom = $this->ask("\e[33mRead from:\e[0m ", $this->filename); if ($readFrom === null) { $this->redrawInfoBar(); return; @@ -404,6 +404,10 @@ class Editor return; } + if ($this->modified) { + if ($this->askModified() === false) return; + } + $this->filename = $readFrom; $this->shortfilename = basename($readFrom); @@ -470,6 +474,14 @@ class Editor private function doOpenFile() { + + if ($this->modified) { + if ($this->askModified() === false) { + $this->redrawEditor(); + return; + } + } + $includeHidden = false; $wd = dirname($this->filename); if ($wd == '.') $wd = getcwd(); @@ -543,6 +555,7 @@ class Editor } } $this->setOpenedFilename($sel); + $this->modified = false; $this->redrawEditor(); $this->showMessage("\e[42;97mOpened {$this->shortfilename}"); } @@ -783,29 +796,36 @@ class Editor public function doExit(): void { if ($this->modified) { - $menu = new Menu($this->term, [ - 'cancel' => "Return to editor", - 'save' => "Save changes", - 'discard' => "Discard changes", - ], 'Document is modified'); - $this->redrawInfoBar([ '^C' => 'Cancel', '↑/↓' => 'Select option', 'Enter' => 'Accept' ]); - $sel = $menu->display(0, 0, 30, 0, "value"); - $this->redrawEditor(); - switch ($sel) { - case 'cancel': - break; - case 'save': - if ($this->doWriteFile()) { - $this->running = false; - } - break; - case 'discard': - $this->running = false; - break; + if ($this->askModified() === false) { + $this->redrawEditor(); + return; } - } else { - $this->running = false; + } + $this->running = false; + } + + private function askModified(): bool + { + $menu = new Menu($this->term, [ + 'cancel' => "Return to editor", + 'save' => "Save changes", + 'discard' => "Discard changes", + ], 'Document is modified'); + $this->redrawInfoBar([ '^C' => 'Cancel', '↑/↓' => 'Select option', 'Enter' => 'Accept' ]); + $sel = $menu->display(0, 0, 30, 0, "value"); + $this->redrawEditor(); + switch ($sel) { + case 'cancel': + return false; + case 'save': + if ($this->doWriteFile()) { + return true; + } + return false; + case 'discard': + return true; } + return false; } public function doSettingsMenu(): void