Ask to save changes on new/open/read

This commit is contained in:
Chris 2024-10-17 14:47:13 +02:00
parent 82323ade56
commit e924c6f2cf

View File

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