From 5c7f5856efabfeb49ac46659ef3e183283ee2ce3 Mon Sep 17 00:00:00 2001 From: Michael Ochmann Date: Mon, 6 Mar 2017 22:30:05 +0100 Subject: [PATCH] fixed some casts --- src/Buffer.hpp | 12 ++++++------ src/Editor.cpp | 24 ++++++++++++------------ src/Editor.hpp | 21 +++++++++++++++++---- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/Buffer.hpp b/src/Buffer.hpp index 439db8c..4c62957 100644 --- a/src/Buffer.hpp +++ b/src/Buffer.hpp @@ -20,7 +20,7 @@ namespace groove { 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->removeTabs(this->lines.at(line)); } @@ -29,23 +29,23 @@ namespace groove { this->removeTabs(line); this->lines.emplace_back(line); } - void insert(int after) { + void insert(unsigned long after) { this->lines.insert(this->lines.begin() + after + 1, ""); } - void remove(int line) { + void remove(unsigned long 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); } - void deleteChar(int line, int car) { + void deleteChar(unsigned long line, int 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); } diff --git a/src/Editor.cpp b/src/Editor.cpp index 3df1e78..07bbfeb 100644 --- a/src/Editor.cpp +++ b/src/Editor.cpp @@ -56,20 +56,20 @@ namespace groove { void Editor::render() { int linenumber = 0; - int space = Editor::Digits(this->buffer->linebuffer().size()) + 2; + this->vspace = Editor::Digits(this->buffer->linebuffer().size()) + 2; clear(); for (int i = 0; i < LINES - 1 + this->offset; i++) { - int ln = static_cast(std::sqrt(std::pow(static_cast(this->y - linenumber), 2))); + unsigned long ln = static_cast(std::sqrt(std::pow(static_cast(this->y - linenumber), 2))); ln = ln == 0 ? linenumber : ln; if(i >= this->buffer->linebuffer().size()) { move(i - this->offset, 0); clrtoeol(); } else { - std::string line = /*std::to_string(i) + " | " +*/ this->buffer->at(i); + std::string line = this->buffer->at(i); Highlighter highlighter(line); std::unordered_map> hilist = highlighter.get(); - int x = space; + int x = this->vspace; int found = -1; int len = -1; std::string label = std::string(x - Editor::Digits(ln) - 1, ' '); @@ -80,14 +80,14 @@ namespace groove { if (linenumber != this->y) attroff(A_REVERSE); for (auto& car : line) { - if (hilist.find(x - space) != hilist.end()) { - found = x - space; - len = hilist.at(x - space).first; + if (hilist.find(x - this->vspace) != hilist.end()) { + found = x - this->vspace; + len = hilist.at(x - this->vspace).first; } 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)); - 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)); found = -1; len = -1; @@ -103,7 +103,7 @@ namespace groove { linenumber++; } this->status(); - move(this->y - this->offset, this->x + space); + move(static_cast(this->y - this->offset), static_cast(this->x + this->vspace)); } void Editor::status() { @@ -119,8 +119,8 @@ namespace groove { attroff(COLOR_PAIR(ncurses::Colors::STATUSBAR)); } - int Editor::Digits(int number) { - return number > 0 ? (int) log10 ((double) number) + 1 : 1; + unsigned long Editor::Digits(unsigned long number) { + return number > 0 ? static_cast(log10 ((double) number) + 1) : 1; } } \ No newline at end of file diff --git a/src/Editor.hpp b/src/Editor.hpp index 606025a..30e68d5 100644 --- a/src/Editor.hpp +++ b/src/Editor.hpp @@ -25,8 +25,9 @@ namespace groove { friend class modes::Quit; friend class modes::Save; private: - int x, y; + unsigned long x, y; int offset; + unsigned long vspace; char lastChar = 0; std::unique_ptr buffer; Mode mode_; @@ -48,10 +49,22 @@ namespace groove { 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 <= COLS && this->x + 1 <= this->buffer->linebuffer().at(this->y).length()) this->x++; + 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) @@ -60,7 +73,7 @@ namespace groove { return; } if (this->x >= this->buffer->linebuffer().at(this->y).length()) { - int length = static_cast(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->scrollUp(); @@ -72,7 +85,7 @@ namespace groove { return; } 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->scrollDown(); @@ -103,7 +116,7 @@ namespace groove { } void input(int c); void render(); - static int Digits(int number); + static unsigned long Digits(unsigned long number); }; } \ No newline at end of file