|
|
@ -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) |
|
|
|
|
|
|
|
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)); |
|
|
|
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)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|