added linenumbers

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

@ -2,6 +2,7 @@
#include <Highlighter.hpp>
#include <fstream>
#include <iostream>
#include <math.h>
namespace groove {
@ -193,8 +194,12 @@ namespace groove {
}
void Editor::render() {
int linenumber = 0;
int space = Editor::Digits(this->buffer->linebuffer().size()) + 2;
clear();
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()) {
move(i - this->offset, 0);
clrtoeol();
@ -203,33 +208,41 @@ namespace groove {
std::string line = /*std::to_string(i) + " | " +*/ this->buffer->at(i);
Highlighter highlighter(line);
std::unordered_map<int, std::pair<int, ncurses::Colors>> hilist = highlighter.get();
int x = 0;
int x = space;
int found = -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) {
if (hilist.find(x) != hilist.end()) {
found = x;
len = hilist.at(x).first;
if (hilist.find(x - space) != hilist.end()) {
found = x - space;
len = hilist.at(x - space).first;
}
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));
else if (x > found && x >= found + len) {
else if (x - space > found && x - space >= found + len) {
attron(COLOR_PAIR(ncurses::Colors::MAIN));
found = -1;
len = -1;
}
}
mvaddch(i - this->offset, x, car);
mvaddch(i - this->offset, x , car);
attron(COLOR_PAIR(ncurses::Colors::MAIN));
x++;
}
//mvprintw(i - this->offset, 0, line.c_str());
}
clrtoeol();
linenumber++;
}
this->status();
move(this->y - this->offset, this->x);
move(this->y - this->offset, this->x + space);
}
void Editor::status() {
@ -264,4 +277,8 @@ namespace groove {
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 render();
static int Digits(int number);
};
}
Loading…
Cancel
Save