diff --git a/src/Editor.cpp b/src/Editor.cpp index c0ce8b2..3df1e78 100644 --- a/src/Editor.cpp +++ b/src/Editor.cpp @@ -10,6 +10,9 @@ namespace groove { buffer(std::make_unique()), mode_(Mode::EDIT), filename(filename), offset(0) { this->modes.emplace(Mode::INSERT, std::make_unique(*this)); + 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)); if (!this->load()) { std::cerr << "Could not open file: '" << this->filename << "', creating it on save.\n"; this->buffer->insert(""); @@ -47,76 +50,8 @@ namespace groove { return false; } - void groove::Editor::input(int car) { - if (this->mode_ != Mode::SAVE) { - switch (car) { - case KEY_LEFT: - this->left(); - return; - case KEY_RIGHT: - this->right(); - return; - case KEY_UP: - this->up(); - return; - case KEY_DOWN: - this->down(); - return; - } - } - - switch (this->mode_) { - case Mode::EDIT: - switch (car) { - case 'q': - this->mode_ = Mode::QUIT; - break; - case 'i': - this->mode_ = Mode::INSERT; - break; - case 'a': - this->x = static_cast(this->buffer->at(this->y).length()); - this->mode_ = Mode::INSERT; - break; - case 's': - this->mode_ = Mode::SAVE; - break; - case 'c': - this->clipboard = this->buffer->at(this->y); - break; - case 'x': - this->clipboard = this->buffer->at(this->y); - this->buffer->remove(this->y); - break; - case 'p': - this->buffer->insert(this->clipboard, this->y); - this->y++; - this->x = this->buffer->at(this->y).size(); - break; - case 'd': - this->buffer->remove(this->y); - break; - } - break; - case Mode::INSERT: - this->modes.at(Mode::INSERT)->input(car); - break; - case Mode::QUIT: - break; - case Mode::SAVE: - switch (car) { - case 'n': - case 27: - this->mode_ = Mode::EDIT; - break; - case 'y': - if (!this->save()) - std::cerr << "Could not write to file: '" << this->filename << "'\n"; - this->mode_ = Mode::EDIT; - break; - } - break; - } + void groove::Editor::input(int c) { + this->modes.at(this->mode_)->input(c); } void Editor::render() { @@ -173,33 +108,14 @@ namespace groove { void Editor::status() { std::string status; - switch(this->mode_) - { - case Mode::EDIT: - status = "[E]"; - break; - case Mode::INSERT: - status = "[I]"; - break; - case Mode::QUIT: - status = "[QUIT]"; - break; - } - status += "\t\t" + - std::to_string(this->y) + ", " + - std::to_string(this->x) + - "\t\t\tbuffer: " + std::to_string(this->buffer->linebuffer().size()) + - "\toffset: " + std::to_string(this->offset); - if (this->mode_ == Mode::SAVE) - status = "Save File '" + this->filename + "'? [Y/N]: "; - - status += std::string(COLS - status.length(), ' '); + std::string position = std::to_string(this->y) + ", " + std::to_string(this->x) + " "; + status = this->modes.at(this->mode_)->status(); + status += std::string(COLS - status.length() - position.length(), ' '); + status += position; attron(COLOR_PAIR(ncurses::Colors::STATUSBAR)); mvprintw(LINES-1, 0, status.c_str()); - - //clrtoeol(); attroff(COLOR_PAIR(ncurses::Colors::STATUSBAR)); } diff --git a/src/Editor.hpp b/src/Editor.hpp index fbe1c95..301277c 100644 --- a/src/Editor.hpp +++ b/src/Editor.hpp @@ -4,6 +4,9 @@ #include #include #include +#include +#include +#include #include namespace groove { @@ -18,6 +21,9 @@ namespace groove { class Editor { friend class modes::Mode; friend class modes::Insert; + friend class modes::Edit; + friend class modes::Quit; + friend class modes::Save; private: int x, y; int offset; @@ -71,6 +77,25 @@ namespace groove { } this->scrollDown(); } + + bool movement(int c) { + switch (c) { + case KEY_LEFT: + this->left(); + return true; + case KEY_RIGHT: + this->right(); + return true; + case KEY_UP: + this->up(); + return true; + case KEY_DOWN: + this->down(); + return true; + default: + return false; + } + } public: Editor(std::string file = "ubenannt"); Mode mode() { diff --git a/src/modes/Edit.cpp b/src/modes/Edit.cpp new file mode 100644 index 0000000..6191a33 --- /dev/null +++ b/src/modes/Edit.cpp @@ -0,0 +1,42 @@ +#include +#include + +namespace groove { + namespace modes { + + void Edit::input(int c) { + if (this->editor.movement(c)) + return; + switch (c) { + case 'q': + this->editor.mode_ = groove::Mode::QUIT; + break; + case 'i': + this->editor.mode_ = groove::Mode::INSERT; + break; + case 'a': + this->editor.x = static_cast(this->editor.buffer->at(this->editor.y).length()); + this->editor.mode_ = groove::Mode::INSERT; + break; + case 's': + this->editor.mode_ = groove::Mode::SAVE; + break; + case 'c': + this->editor.clipboard = this->editor.buffer->at(this->editor.y); + break; + case 'x': + this->editor.clipboard = this->editor.buffer->at(this->editor.y); + this->editor.buffer->remove(this->editor.y); + break; + case 'p': + this->editor.buffer->insert(this->editor.clipboard, this->editor.y); + this->editor.y++; + this->editor.x = this->editor.buffer->at(this->editor.y).size(); + break; + case 'd': + this->editor.buffer->remove(this->editor.y); + break; + } + } + } +} \ No newline at end of file diff --git a/src/modes/Edit.hpp b/src/modes/Edit.hpp new file mode 100644 index 0000000..a1d1649 --- /dev/null +++ b/src/modes/Edit.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include + +namespace groove { + namespace modes { + + class Edit : public Mode { + public: + using Mode::Mode; + std::string status() { + return "[E]" + this->status_; + } + void input(int c); + }; + + } +} \ No newline at end of file diff --git a/src/modes/Insert.cpp b/src/modes/Insert.cpp index a278330..07252d3 100644 --- a/src/modes/Insert.cpp +++ b/src/modes/Insert.cpp @@ -6,11 +6,11 @@ namespace groove { namespace modes { void Insert::input(int c) { + if (this->editor.movement(c)) + return; switch (c) { case 27: this->editor.mode_ = groove::Mode::EDIT; - this->editor.x = 0; - this->editor.y = 0; break; case KEY_ENTER: case 10: diff --git a/src/modes/Insert.hpp b/src/modes/Insert.hpp index 5ac43f8..a0999d3 100644 --- a/src/modes/Insert.hpp +++ b/src/modes/Insert.hpp @@ -9,7 +9,7 @@ namespace groove { public: using Mode::Mode; std::string status() { - return this->status_; + return "[I]" + this->status_; } void input(int c); }; diff --git a/src/modes/Quit.cpp b/src/modes/Quit.cpp new file mode 100644 index 0000000..e7babc7 --- /dev/null +++ b/src/modes/Quit.cpp @@ -0,0 +1,12 @@ +#include + + +namespace groove { + namespace modes { + + void Quit::input(int c) { + + } + + } +} \ No newline at end of file diff --git a/src/modes/Quit.hpp b/src/modes/Quit.hpp new file mode 100644 index 0000000..30f13a0 --- /dev/null +++ b/src/modes/Quit.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include + +namespace groove { + namespace modes { + + class Quit : public Mode { + public: + using Mode::Mode; + std::string status() { + return this->status_; + } + void input(int c); + }; + + } +} \ No newline at end of file diff --git a/src/modes/Save.cpp b/src/modes/Save.cpp new file mode 100644 index 0000000..b5d24bd --- /dev/null +++ b/src/modes/Save.cpp @@ -0,0 +1,28 @@ +#include +#include +#include + +namespace groove { + namespace modes { + + void Save::input(int c) { + switch (c) { + case 'n': + case 27: + this->editor.mode_ = groove::Mode::EDIT; + break; + case 'y': + if (!this->editor.save()) + std::cerr << "Could not write to file: '" << this->editor.filename << "'\n"; + this->editor.mode_ = groove::Mode::EDIT; + break; + } + } + + std::string Save::status() { + this->status_ = "Save file '" + this->editor.filename + "'? [Y/N]:"; + return this->status_; + } + + } +} \ No newline at end of file diff --git a/src/modes/Save.hpp b/src/modes/Save.hpp new file mode 100644 index 0000000..8016750 --- /dev/null +++ b/src/modes/Save.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include + +namespace groove { + namespace modes { + + class Save : public Mode { + public: + using Mode::Mode; + std::string status(); + void input(int c); + }; + + } +} \ No newline at end of file