a simple ncurses-based texteditor for the terminal
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
3.1 KiB

#pragma once
#include <memory>
#include <unordered_map>
#include <Buffer.hpp>
#include <ncurses/ncurses.hpp>
#include <modes/Insert.hpp>
#include <modes/Edit.hpp>
#include <modes/Quit.hpp>
#include <modes/Save.hpp>
#include <modes/Search.hpp>
#include <History.hpp>
#include <config/ConfigParser.hpp>
#include <modes/Exit.hpp>
namespace groove {
enum class Mode {
INSERT,
EDIT,
SAVE,
QUIT,
EXIT,
SEARCH
};
enum LineMode {
NUMBERS,
RELATIVE,
NONE,
COUNT
};
class Editor {
friend class modes::Mode;
friend class modes::Insert;
friend class modes::Edit;
friend class modes::Quit;
friend class modes::Save;
friend class modes::Exit;
friend class modes::Search;
friend class History;
private:
long x, y;
int offset;
int voffset;
long vspace;
bool inComment;
char lastChar = 0;
WINDOW* overlay;
History history;
LineMode lineMode;
std::unique_ptr<Buffer> buffer;
Mode mode_;
std::string clipboard = "";
std::string filename;
std::unordered_map<Mode, std::unique_ptr<modes::Mode>> modes;
config::ConfigParser config;
bool load();
bool save();
void status();
void scrollUp() {
if (this->y < this->offset && this->offset > 0)
this->offset--;
}
void scrollDown() {
if (this->y - this->offset >= LINES - 1 && this->y < this->buffer->size())
this->offset++;
}
void left() {
if (this->x - 1 >= 0)
this->x--;
else {
if (this->y - 1 >= 0) {
this->y--;
this->x = this->buffer->at(this->y).length();
}
}
}
void right() {
if (this->x + 1 <= this->buffer->linebuffer().at(this->y).length()) {
if (this->x + 1 <= COLS)
this->x++;
else
this->voffset++;
}
else {
if (this->y + 1 < this->buffer->size()) {
this->y++;
this->x = this->buffer->at(this->y).size();
}
}
}
void up() {
if (this->y - 1 >= 0)
this->y--;
else {
return;
}
if (this->x >= this->buffer->linebuffer().at(this->y).length()) {
long length = this->buffer->linebuffer().at(this->y).length();
this->x = length > 0 ? length - 1 : 0;
}
this->scrollUp();
}
void down() {
if (this->y + 1 < this->buffer->linebuffer().size() )
this->y++;
else {
return;
}
if (this->x >= this->buffer->linebuffer().at(this->y).length()) {
long length = this->buffer->linebuffer().at(this->y).length();
this->x = length > 0 ? length - 1 : 0;
}
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;
}
}
void renderOverlay() {
wbkgd(this->overlay, COLOR_PAIR(ncurses::Colors::STATUSBAR));
mvwaddstr(this->overlay, 3, 3, "Hallo win");
refresh();
wrefresh(this->overlay);
}
public:
Editor(std::string file = "ubenannt");
Mode mode() {
return this->mode_;
}
~Editor() {
delete this->overlay;
}
void input(int c);
void render();
static long Digits(long number);
};
}