diff --git a/src/Editor.cpp b/src/Editor.cpp index fe9c50c..03864a7 100644 --- a/src/Editor.cpp +++ b/src/Editor.cpp @@ -14,6 +14,7 @@ namespace groove { this->modes.emplace(Mode::EDIT, std::make_unique(*this)); this->modes.emplace(Mode::QUIT, std::make_unique(*this)); this->modes.emplace(Mode::SAVE, std::make_unique(*this)); + this->modes.emplace(Mode::EXIT, std::make_unique(*this)); this->lineMode = this->config.get("linenumbers") == "relative" ? LineMode::RELATIVE : this->config.get("linenumbers") == "none" ? LineMode::NONE : LineMode::NUMBERS; @@ -40,8 +41,11 @@ namespace groove { std::getline(file, line); this->buffer->insert(line); } + this->buffer->changed = false; + return true; } + this->buffer->changed = false; return false; } diff --git a/src/Editor.hpp b/src/Editor.hpp index 22fcb13..9460cef 100644 --- a/src/Editor.hpp +++ b/src/Editor.hpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace groove { @@ -17,7 +18,8 @@ namespace groove { INSERT, EDIT, SAVE, - QUIT + QUIT, + EXIT }; enum LineMode { @@ -33,6 +35,7 @@ namespace groove { friend class modes::Edit; friend class modes::Quit; friend class modes::Save; + friend class modes::Exit; friend class History; private: long x, y; diff --git a/src/modes/Edit.cpp b/src/modes/Edit.cpp index e7b37ec..86047ee 100644 --- a/src/modes/Edit.cpp +++ b/src/modes/Edit.cpp @@ -10,7 +10,8 @@ namespace groove { return; switch (c) { case 'q': - this->editor.mode_ = groove::Mode::QUIT; + this->editor.mode_ = groove::Mode::EXIT; + this->editor.input(' '); break; case 'i': this->editor.mode_ = groove::Mode::INSERT; diff --git a/src/modes/Exit.cpp b/src/modes/Exit.cpp new file mode 100644 index 0000000..8bb3669 --- /dev/null +++ b/src/modes/Exit.cpp @@ -0,0 +1,34 @@ +#include +#include + + +namespace groove { + namespace modes { + + void Exit::input(int c) { + if (!this->editor.buffer->changed) { + this->editor.mode_ = groove::Mode::QUIT; + + return; + } + + this->status_ = "Save file '" + this->editor.filename + "'? [Y/N]:"; + switch (c) { + case 27: + this->status_ = ""; + this->editor.mode_ = groove::Mode::EDIT; + this->editor.input(' '); + break; + case 'n': + this->editor.mode_ = groove::Mode::QUIT; + break; + case 'y': + if (!this->editor.save()) + std::cerr << "Could not write to file: '" << this->editor.filename << "'\n"; + this->editor.mode_ = groove::Mode::QUIT; + break; + } + } + + } +} \ No newline at end of file diff --git a/src/modes/Exit.hpp b/src/modes/Exit.hpp new file mode 100644 index 0000000..db17a15 --- /dev/null +++ b/src/modes/Exit.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include + +namespace groove { + namespace modes { + + class Exit : public Mode { + public: + using Mode::Mode; + std::string status() { + return this->status_; + } + void input(int c); + }; + + } +} \ No newline at end of file