added linenumbers

development
Michael Ochmann 8 years ago
parent b13b665913
commit 00f707f99f
  1. 31
      src/Editor.cpp
  2. 1
      src/Editor.hpp

@ -2,6 +2,7 @@
#include <Highlighter.hpp> #include <Highlighter.hpp>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <math.h>
namespace groove { namespace groove {
@ -193,8 +194,12 @@ namespace groove {
} }
void Editor::render() { void Editor::render() {
int linenumber = 0;
int space = 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 = std::sqrt(std::pow(static_cast<double>(this->y - linenumber), 2));
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();
@ -203,18 +208,25 @@ namespace groove {
std::string line = /*std::to_string(i) + " | " +*/ this->buffer->at(i); std::string line = /*std::to_string(i) + " | " +*/ 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 = 0; int x = space;
int found = -1; int found = -1;
int len = -1; int len = -1;
std::string label = std::string(x - Editor::Digits(ln) - 1, ' ');
label += std::to_string(ln) + ' ';
if (linenumber != this->y)
attron(A_REVERSE);
mvprintw(i - this->offset, 0, label.c_str());
if (linenumber != this->y)
attroff(A_REVERSE);
for (auto& car : line) { for (auto& car : line) {
if (hilist.find(x) != hilist.end()) { if (hilist.find(x - space) != hilist.end()) {
found = x; found = x - space;
len = hilist.at(x).first; len = hilist.at(x - space).first;
} }
if (found >= 0 && len >= 0) { if (found >= 0 && len >= 0) {
if (x >= found && x <= found + len - 1) if (x - space >= found && x - space <= found + len - 1)
attron(COLOR_PAIR(hilist.at(found).second)); attron(COLOR_PAIR(hilist.at(found).second));
else if (x > found && x >= found + len) { else if (x - space > found && x - space >= found + len) {
attron(COLOR_PAIR(ncurses::Colors::MAIN)); attron(COLOR_PAIR(ncurses::Colors::MAIN));
found = -1; found = -1;
len = -1; len = -1;
@ -227,9 +239,10 @@ namespace groove {
//mvprintw(i - this->offset, 0, line.c_str()); //mvprintw(i - this->offset, 0, line.c_str());
} }
clrtoeol(); clrtoeol();
linenumber++;
} }
this->status(); this->status();
move(this->y - this->offset, this->x); move(this->y - this->offset, this->x + space);
} }
void Editor::status() { void Editor::status() {
@ -264,4 +277,8 @@ namespace groove {
attroff(COLOR_PAIR(ncurses::Colors::STATUSBAR)); attroff(COLOR_PAIR(ncurses::Colors::STATUSBAR));
} }
int Editor::Digits(int number) {
return number > 0 ? (int) log10 ((double) number) + 1 : 1;
}
} }

@ -77,6 +77,7 @@ namespace groove {
} }
void input(int c); void input(int c);
void render(); void render();
static int Digits(int number);
}; };
} }
Loading…
Cancel
Save