finished implementing modes

development
Michael Ochmann 8 years ago
parent 9e27520ab1
commit baeb89fdb9
  1. 102
      src/Editor.cpp
  2. 25
      src/Editor.hpp
  3. 42
      src/modes/Edit.cpp
  4. 18
      src/modes/Edit.hpp
  5. 4
      src/modes/Insert.cpp
  6. 2
      src/modes/Insert.hpp
  7. 12
      src/modes/Quit.cpp
  8. 18
      src/modes/Quit.hpp
  9. 28
      src/modes/Save.cpp
  10. 16
      src/modes/Save.hpp

@ -10,6 +10,9 @@ namespace groove {
buffer(std::make_unique<Buffer>()), mode_(Mode::EDIT), buffer(std::make_unique<Buffer>()), mode_(Mode::EDIT),
filename(filename), offset(0) { filename(filename), offset(0) {
this->modes.emplace(Mode::INSERT, std::make_unique<modes::Insert>(*this)); this->modes.emplace(Mode::INSERT, std::make_unique<modes::Insert>(*this));
this->modes.emplace(Mode::EDIT, std::make_unique<modes::Edit>(*this));
this->modes.emplace(Mode::QUIT, std::make_unique<modes::Quit>(*this));
this->modes.emplace(Mode::SAVE, std::make_unique<modes::Save>(*this));
if (!this->load()) { if (!this->load()) {
std::cerr << "Could not open file: '" << this->filename << "', creating it on save.\n"; std::cerr << "Could not open file: '" << this->filename << "', creating it on save.\n";
this->buffer->insert(""); this->buffer->insert("");
@ -47,76 +50,8 @@ namespace groove {
return false; return false;
} }
void groove::Editor::input(int car) { void groove::Editor::input(int c) {
if (this->mode_ != Mode::SAVE) { this->modes.at(this->mode_)->input(c);
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<int>(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 Editor::render() { void Editor::render() {
@ -173,33 +108,14 @@ namespace groove {
void Editor::status() { void Editor::status() {
std::string 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) std::string position = std::to_string(this->y) + ", " + std::to_string(this->x) + " ";
status = "Save File '" + this->filename + "'? [Y/N]: "; status = this->modes.at(this->mode_)->status();
status += std::string(COLS - status.length() - position.length(), ' ');
status += std::string(COLS - status.length(), ' '); status += position;
attron(COLOR_PAIR(ncurses::Colors::STATUSBAR)); attron(COLOR_PAIR(ncurses::Colors::STATUSBAR));
mvprintw(LINES-1, 0, status.c_str()); mvprintw(LINES-1, 0, status.c_str());
//clrtoeol();
attroff(COLOR_PAIR(ncurses::Colors::STATUSBAR)); attroff(COLOR_PAIR(ncurses::Colors::STATUSBAR));
} }

@ -4,6 +4,9 @@
#include <Buffer.hpp> #include <Buffer.hpp>
#include <ncurses/ncurses.hpp> #include <ncurses/ncurses.hpp>
#include <modes/Insert.hpp> #include <modes/Insert.hpp>
#include <modes/Edit.hpp>
#include <modes/Quit.hpp>
#include <modes/Save.hpp>
#include <unordered_map> #include <unordered_map>
namespace groove { namespace groove {
@ -18,6 +21,9 @@ namespace groove {
class Editor { class Editor {
friend class modes::Mode; friend class modes::Mode;
friend class modes::Insert; friend class modes::Insert;
friend class modes::Edit;
friend class modes::Quit;
friend class modes::Save;
private: private:
int x, y; int x, y;
int offset; int offset;
@ -71,6 +77,25 @@ namespace groove {
} }
this->scrollDown(); 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: public:
Editor(std::string file = "ubenannt"); Editor(std::string file = "ubenannt");
Mode mode() { Mode mode() {

@ -0,0 +1,42 @@
#include <modes/Edit.hpp>
#include <Editor.hpp>
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<int>(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;
}
}
}
}

@ -0,0 +1,18 @@
#pragma once
#include <modes/Mode.hpp>
namespace groove {
namespace modes {
class Edit : public Mode {
public:
using Mode::Mode;
std::string status() {
return "[E]" + this->status_;
}
void input(int c);
};
}
}

@ -6,11 +6,11 @@ namespace groove {
namespace modes { namespace modes {
void Insert::input(int c) { void Insert::input(int c) {
if (this->editor.movement(c))
return;
switch (c) { switch (c) {
case 27: case 27:
this->editor.mode_ = groove::Mode::EDIT; this->editor.mode_ = groove::Mode::EDIT;
this->editor.x = 0;
this->editor.y = 0;
break; break;
case KEY_ENTER: case KEY_ENTER:
case 10: case 10:

@ -9,7 +9,7 @@ namespace groove {
public: public:
using Mode::Mode; using Mode::Mode;
std::string status() { std::string status() {
return this->status_; return "[I]" + this->status_;
} }
void input(int c); void input(int c);
}; };

@ -0,0 +1,12 @@
#include <modes/Quit.hpp>
namespace groove {
namespace modes {
void Quit::input(int c) {
}
}
}

@ -0,0 +1,18 @@
#pragma once
#include <modes/Mode.hpp>
namespace groove {
namespace modes {
class Quit : public Mode {
public:
using Mode::Mode;
std::string status() {
return this->status_;
}
void input(int c);
};
}
}

@ -0,0 +1,28 @@
#include <modes/Save.hpp>
#include <Editor.hpp>
#include <iostream>
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_;
}
}
}

@ -0,0 +1,16 @@
#pragma once
#include <modes/Mode.hpp>
namespace groove {
namespace modes {
class Save : public Mode {
public:
using Mode::Mode;
std::string status();
void input(int c);
};
}
}
Loading…
Cancel
Save