Add line numbers

This commit is contained in:
2025-12-13 16:47:07 +00:00
parent d8c281d7d7
commit a59111162c
5 changed files with 85 additions and 43 deletions

View File

@@ -5,6 +5,7 @@ extern "C" {
#include "../include/main.h"
#include "../include/ts.h"
#include "../include/utils.h"
#include <cmath>
Editor *new_editor(const char *filename, Coord position, Coord size) {
Editor *editor = new Editor();
@@ -69,6 +70,10 @@ void update_render_fold_marker(uint32_t row, uint32_t cols) {
void render_editor(Editor *editor) {
uint32_t sel_start = 0, sel_end = 0;
uint32_t numlen =
2 + static_cast<int>(std::log10(editor->root->line_count + 1));
uint32_t render_width = editor->size.col - numlen;
uint32_t render_x = editor->position.col + numlen;
if (editor->selection_active) {
uint32_t sel1 = line_to_byte(editor->root, editor->cursor.row, nullptr) +
editor->cursor.col;
@@ -90,7 +95,7 @@ void render_editor(Editor *editor) {
while (rendered_rows < editor->size.row) {
if (editor->folded[line_index]) {
if (editor->folded[line_index] == 2) {
update_render_fold_marker(rendered_rows, editor->size.col);
update_render_fold_marker(rendered_rows, render_width);
rendered_rows++;
}
do {
@@ -118,14 +123,28 @@ void render_editor(Editor *editor) {
if (rendered_rows == 0)
current_byte_offset += editor->scroll.col;
while (current_byte_offset < line_len && rendered_rows < editor->size.row) {
uint32_t color = editor->cursor.row == line_index ? 0x222222 : 0;
if (current_byte_offset == 0 || rendered_rows == 0) {
char buf[16];
int len = snprintf(buf, sizeof(buf), "%*u", numlen - 1, line_index + 1);
uint32_t num_color =
editor->cursor.row == line_index ? 0xFFFFFF : 0x555555;
for (int i = 0; i < len; i++)
update(editor->position.row + rendered_rows, editor->position.col + i,
std::string(1, buf[i]).c_str(), num_color, 0, 0);
} else {
for (uint32_t i = 0; i < numlen; i++)
update(editor->position.row + rendered_rows, editor->position.col + i,
" ", 0, 0, 0);
}
uint32_t col = 0;
uint32_t local_render_offset = 0;
uint32_t line_left = line_len - current_byte_offset;
while (line_left > 0 && col < editor->size.col) {
while (line_left > 0 && col < render_width) {
if (line_index == editor->cursor.row &&
editor->cursor.col == (current_byte_offset + local_render_offset)) {
cursor.row = editor->position.row + rendered_rows;
cursor.col = editor->position.col + col;
cursor.col = render_x + col;
}
uint32_t absolute_byte_pos =
global_byte_offset + current_byte_offset + local_render_offset;
@@ -141,33 +160,32 @@ void render_editor(Editor *editor) {
std::string cluster(line + current_byte_offset + local_render_offset,
cluster_len);
int width = display_width(cluster.c_str(), cluster_len);
if (col + width > editor->size.col)
if (col + width > render_width)
break;
update(editor->position.row + rendered_rows, editor->position.col + col,
cluster.c_str(), fg, bg, fl);
update(editor->position.row + rendered_rows, render_x + col,
cluster.c_str(), fg, bg | color, fl);
local_render_offset += cluster_len;
line_left -= cluster_len;
col += width;
while (width-- > 1)
update(editor->position.row + rendered_rows,
editor->position.col + col - width, "\x1b", fg, bg, fl);
update(editor->position.row + rendered_rows, render_x + col - width,
"\x1b", fg, bg | color, fl);
}
if (line_index == editor->cursor.row &&
editor->cursor.col == (current_byte_offset + local_render_offset)) {
cursor.row = editor->position.row + rendered_rows;
cursor.col = editor->position.col + col;
cursor.col = render_x + col;
}
if (editor->selection_active &&
global_byte_offset + line_len + 1 > sel_start &&
global_byte_offset + line_len + 1 <= sel_end &&
col < editor->size.col) {
update(editor->position.row + rendered_rows, editor->position.col + col,
" ", 0, 0x555555, 0);
global_byte_offset + line_len + 1 <= sel_end && col < render_width) {
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
0x555555 | color, 0);
col++;
}
while (col < editor->size.col) {
update(editor->position.row + rendered_rows, editor->position.col + col,
" ", 0xFFFFFF, 0, 0);
while (col < render_width) {
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
0 | color, 0);
col++;
}
rendered_rows++;
@@ -175,21 +193,29 @@ void render_editor(Editor *editor) {
}
if (line_len == 0 ||
(current_byte_offset >= line_len && rendered_rows == 0)) {
uint32_t color = editor->cursor.row == line_index ? 0x222222 : 0;
char buf[16];
int len = snprintf(buf, sizeof(buf), "%*u", numlen - 1, line_index + 1);
uint32_t num_color =
editor->cursor.row == line_index ? 0xFFFFFF : 0x555555;
for (int i = 0; i < len; i++)
update(editor->position.row + rendered_rows, editor->position.col + i,
std::string(1, buf[i]).c_str(), num_color, 0, 0);
if (editor->cursor.row == line_index) {
cursor.row = editor->position.row + rendered_rows;
cursor.col = editor->position.col;
cursor.col = render_x;
}
uint32_t col = 0;
if (editor->selection_active &&
global_byte_offset + line_len + 1 > sel_start &&
global_byte_offset + line_len + 1 <= sel_end) {
update(editor->position.row + rendered_rows, editor->position.col + col,
" ", 0, 0x555555, 0);
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
0x555555 | color, 0);
col++;
}
while (col < editor->size.col) {
update(editor->position.row + rendered_rows, editor->position.col + col,
" ", 0xFFFFFF, 0, 0);
while (col < render_width) {
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
0 | color, 0);
col++;
}
rendered_rows++;
@@ -213,10 +239,10 @@ void render_editor(Editor *editor) {
}
set_cursor(cursor.row, cursor.col, type, true);
}
while (rendered_rows < editor->size.row) {
for (uint32_t col = 0; col < editor->size.col; col++)
update(editor->position.row + rendered_rows, editor->position.col + col,
" ", 0xFFFFFF, 0, 0);
while (rendered_rows < render_width) {
for (uint32_t col = 0; col < render_width; col++)
update(editor->position.row + rendered_rows, render_x + col, " ",
0xFFFFFF, 0, 0);
rendered_rows++;
}
free(it);