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
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,6 +796,16 @@ class Editor
public function doExit(): void
{
if ($this->modified) {
if ($this->askModified() === false) {
$this->redrawEditor();
return;
}
}
$this->running = false;
}
private function askModified(): bool
{
$menu = new Menu($this->term, [
'cancel' => "Return to editor",
'save' => "Save changes",
@ -793,19 +816,16 @@ class Editor
$this->redrawEditor();
switch ($sel) {
case 'cancel':
break;
return false;
case 'save':
if ($this->doWriteFile()) {
$this->running = false;
return true;
}
break;
return false;
case 'discard':
$this->running = false;
break;
}
} else {
$this->running = false;
return true;
}
return false;
}
public function doSettingsMenu(): void