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.

83 lines
1.6 KiB

#pragma once
#include <memory>
#include <Buffer.hpp>
#include <ncurses/ncurses.hpp>
namespace groove {
enum class Mode {
INSERT,
EDIT,
SAVE,
QUIT
};
class Editor {
private:
int x, y;
int offset;
char lastChar = 0;
std::unique_ptr<Buffer> buffer;
Mode mode_;
std::string clipboard = "";
std::string filename;
bool load();
bool save();
void status();
void scrollUp() {
if (this->y < this->offset && this->offset > 0)
this->offset--;
}
void scrollDown() {
if (this->y >= LINES - 1)
this->offset++;
}
void left() {
if (this->x - 1 >= 0)
this->x--;
}
void right() {
if (this->x + 1 <= COLS && this->x + 1 <= this->buffer->linebuffer().at(this->y).length())
this->x++;
}
void up() {
if (this->y - 1 >= 0)
this->y--;
else {
return;
}
if (this->x >= this->buffer->linebuffer().at(this->y).length()) {
int 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()) {
int length = this->buffer->linebuffer().at(this->y).length();
this->x = length > 0 ? length - 1 : 0;
}
this->scrollDown();
}
public:
Editor() : x(0), y(0), buffer(std::make_unique<Buffer>()), mode_(Mode::EDIT), filename("unbenannt"),
offset(0) {
this->buffer->insert("");
}
Editor(std::string file);
Mode mode() {
return this->mode_;
}
void input(int c);
void render();
static int Digits(int number);
};
}