fixed some casts

development
Michael Ochmann 8 years ago
parent ba56dfc489
commit 5c7f5856ef
  1. 12
      src/Buffer.hpp
  2. 24
      src/Editor.cpp
  3. 21
      src/Editor.hpp

@ -20,7 +20,7 @@ namespace groove {
return this->lines; return this->lines;
} }
void insert(std::string str, int line) { void insert(std::string str, unsigned long line) {
this->lines.insert(this->lines.begin() + line, str); this->lines.insert(this->lines.begin() + line, str);
this->removeTabs(this->lines.at(line)); this->removeTabs(this->lines.at(line));
} }
@ -29,23 +29,23 @@ namespace groove {
this->removeTabs(line); this->removeTabs(line);
this->lines.emplace_back(line); this->lines.emplace_back(line);
} }
void insert(int after) { void insert(unsigned long after) {
this->lines.insert(this->lines.begin() + after + 1, ""); this->lines.insert(this->lines.begin() + after + 1, "");
} }
void remove(int line) { void remove(unsigned long line) {
this->lines.erase(this->lines.begin() + line); this->lines.erase(this->lines.begin() + line);
} }
void remove(int line, int car) { void remove(unsigned long line, int car) {
this->lines.at(line).erase(this->lines.at(line).begin() + car); this->lines.at(line).erase(this->lines.at(line).begin() + car);
} }
void deleteChar(int line, int car) { void deleteChar(unsigned long line, int car) {
this->lines.at(line).erase(this->lines.at(line).begin() + car); this->lines.at(line).erase(this->lines.at(line).begin() + car);
} }
std::string& at(int line) { std::string& at(unsigned long line) {
return this->lines.at(line); return this->lines.at(line);
} }

@ -56,20 +56,20 @@ namespace groove {
void Editor::render() { void Editor::render() {
int linenumber = 0; int linenumber = 0;
int space = Editor::Digits(this->buffer->linebuffer().size()) + 2; this->vspace = Editor::Digits(this->buffer->linebuffer().size()) + 2;
clear(); clear();
for (int i = 0; i < LINES - 1 + this->offset; i++) { for (int i = 0; i < LINES - 1 + this->offset; i++) {
int ln = static_cast<int>(std::sqrt(std::pow(static_cast<double>(this->y - linenumber), 2))); unsigned long ln = static_cast<unsigned long>(std::sqrt(std::pow(static_cast<double>(this->y - linenumber), 2)));
ln = ln == 0 ? linenumber : ln; ln = ln == 0 ? linenumber : ln;
if(i >= this->buffer->linebuffer().size()) { if(i >= this->buffer->linebuffer().size()) {
move(i - this->offset, 0); move(i - this->offset, 0);
clrtoeol(); clrtoeol();
} }
else { else {
std::string line = /*std::to_string(i) + " | " +*/ this->buffer->at(i); std::string line = this->buffer->at(i);
Highlighter highlighter(line); Highlighter highlighter(line);
std::unordered_map<int, std::pair<int, ncurses::Colors>> hilist = highlighter.get(); std::unordered_map<int, std::pair<int, ncurses::Colors>> hilist = highlighter.get();
int x = space; int x = this->vspace;
int found = -1; int found = -1;
int len = -1; int len = -1;
std::string label = std::string(x - Editor::Digits(ln) - 1, ' '); std::string label = std::string(x - Editor::Digits(ln) - 1, ' ');
@ -80,14 +80,14 @@ namespace groove {
if (linenumber != this->y) if (linenumber != this->y)
attroff(A_REVERSE); attroff(A_REVERSE);
for (auto& car : line) { for (auto& car : line) {
if (hilist.find(x - space) != hilist.end()) { if (hilist.find(x - this->vspace) != hilist.end()) {
found = x - space; found = x - this->vspace;
len = hilist.at(x - space).first; len = hilist.at(x - this->vspace).first;
} }
if (found >= 0 && len >= 0) { if (found >= 0 && len >= 0) {
if (x - space >= found && x - space <= found + len - 1) if (x - this->vspace >= found && x - this->vspace <= found + len - 1)
attron(COLOR_PAIR(hilist.at(found).second)); attron(COLOR_PAIR(hilist.at(found).second));
else if (x - space > found && x - space >= found + len) { else if (x - this->vspace > found && x - this->vspace >= found + len) {
attron(COLOR_PAIR(ncurses::Colors::MAIN)); attron(COLOR_PAIR(ncurses::Colors::MAIN));
found = -1; found = -1;
len = -1; len = -1;
@ -103,7 +103,7 @@ namespace groove {
linenumber++; linenumber++;
} }
this->status(); this->status();
move(this->y - this->offset, this->x + space); move(static_cast<int>(this->y - this->offset), static_cast<int>(this->x + this->vspace));
} }
void Editor::status() { void Editor::status() {
@ -119,8 +119,8 @@ namespace groove {
attroff(COLOR_PAIR(ncurses::Colors::STATUSBAR)); attroff(COLOR_PAIR(ncurses::Colors::STATUSBAR));
} }
int Editor::Digits(int number) { unsigned long Editor::Digits(unsigned long number) {
return number > 0 ? (int) log10 ((double) number) + 1 : 1; return number > 0 ? static_cast<unsigned long>(log10 ((double) number) + 1) : 1;
} }
} }

@ -25,8 +25,9 @@ namespace groove {
friend class modes::Quit; friend class modes::Quit;
friend class modes::Save; friend class modes::Save;
private: private:
int x, y; unsigned long x, y;
int offset; int offset;
unsigned long vspace;
char lastChar = 0; char lastChar = 0;
std::unique_ptr<Buffer> buffer; std::unique_ptr<Buffer> buffer;
Mode mode_; Mode mode_;
@ -48,10 +49,22 @@ namespace groove {
void left() { void left() {
if (this->x - 1 >= 0) if (this->x - 1 >= 0)
this->x--; this->x--;
else {
if (this->y - 1 >= 0) {
this->y--;
this->x = this->buffer->at(this->y).length();
}
}
} }
void right() { void right() {
if (this->x + 1 <= COLS && this->x + 1 <= this->buffer->linebuffer().at(this->y).length()) if (this->x + 1 <= COLS && this->x + 1 <= this->buffer->linebuffer().at(this->y).length())
this->x++; this->x++;
else {
if (this->y + 1 <= this->buffer->size()) {
this->y++;
this->x = this->buffer->at(this->y).size();
}
}
} }
void up() { void up() {
if (this->y - 1 >= 0) if (this->y - 1 >= 0)
@ -60,7 +73,7 @@ namespace groove {
return; return;
} }
if (this->x >= this->buffer->linebuffer().at(this->y).length()) { if (this->x >= this->buffer->linebuffer().at(this->y).length()) {
int length = static_cast<int>(this->buffer->linebuffer().at(this->y).length()); unsigned long length = this->buffer->linebuffer().at(this->y).length();
this->x = length > 0 ? length - 1 : 0; this->x = length > 0 ? length - 1 : 0;
} }
this->scrollUp(); this->scrollUp();
@ -72,7 +85,7 @@ namespace groove {
return; return;
} }
if (this->x >= this->buffer->linebuffer().at(this->y).length()) { if (this->x >= this->buffer->linebuffer().at(this->y).length()) {
int length = this->buffer->linebuffer().at(this->y).length(); unsigned long length = this->buffer->linebuffer().at(this->y).length();
this->x = length > 0 ? length - 1 : 0; this->x = length > 0 ? length - 1 : 0;
} }
this->scrollDown(); this->scrollDown();
@ -103,7 +116,7 @@ namespace groove {
} }
void input(int c); void input(int c);
void render(); void render();
static int Digits(int number); static unsigned long Digits(unsigned long number);
}; };
} }
Loading…
Cancel
Save