CE line numbers on the side

This commit is contained in:
2026-03-06 22:45:31 +01:00
parent 49059f4bfa
commit ded013d607

View File

@@ -35,6 +35,7 @@ struct editor {
struct cursor cursor;
size_t col_offset;
size_t row_offset;
size_t total_lines;
};
static struct editor editor;
@@ -77,6 +78,19 @@ static void update_vert_scroll (size_t screen_rows) {
editor.row_offset = editor.cursor.line - (screen_rows - 1) + 1;
}
static size_t count_digits (size_t n) {
if (n == 0)
return 1;
size_t count = 0;
while (n > 0) {
n /= 10;
count++;
}
return count;
}
void edit_start (const char* file_path, const char* text) {
prepare_lines (text);
struct arena temp_arena;
@@ -108,6 +122,12 @@ void edit_start (const char* file_path, const char* text) {
size_t current_idx = 0;
struct list_node_link *line_link, *tmp_line_link;
list_foreach (editor.lines, line_link, tmp_line_link) editor.total_lines++;
size_t gutter_width = count_digits (editor.total_lines);
size_t effective_cols = (cols > gutter_width + 1) ? cols - (gutter_width + 1) : cols;
list_foreach (editor.lines, line_link, tmp_line_link) {
if (current_idx < editor.row_offset) {
current_idx++;
@@ -123,6 +143,11 @@ void edit_start (const char* file_path, const char* text) {
bbptr += w;
bb_remaining -= w;
w = snprintf (bbptr, bb_remaining, ANSIQ_SETFG_CYAN "%*zu " ANSIQ_GR_RESET, gutter_width,
current_idx + 1);
bbptr += w;
bb_remaining -= w;
size_t part1_len = line->gb.gap_start;
size_t part2_len = line->gb.size - line->gb.gap_end;
@@ -132,8 +157,8 @@ void edit_start (const char* file_path, const char* text) {
size_t start = editor.col_offset;
size_t len = part1_len - start;
if (len > cols)
len = cols;
if (len > effective_cols)
len = effective_cols;
w = snprintf (bbptr, bb_remaining, "%.*s", (int)len, &line->gb.buffer[start]);
bbptr += w;
@@ -141,7 +166,7 @@ void edit_start (const char* file_path, const char* text) {
current_col += len;
}
if (current_col < cols && part2_len > 0) {
if (current_col < effective_cols && part2_len > 0) {
size_t skip = 0;
if (editor.col_offset > part1_len)
skip = editor.col_offset - part1_len;
@@ -149,8 +174,8 @@ void edit_start (const char* file_path, const char* text) {
if (skip < part2_len) {
size_t len = part2_len - skip;
if (current_col + len > cols)
len = cols - current_col;
if (current_col + len > effective_cols)
len = effective_cols - current_col;
w = snprintf (bbptr, bb_remaining, "%.*s", (int)len,
&line->gb.buffer[line->gb.gap_end + skip]);
@@ -172,13 +197,13 @@ void edit_start (const char* file_path, const char* text) {
bb_remaining -= w;
w = snprintf (bbptr, bb_remaining, "Editing %s; line %zu col %zu", file_path,
editor.cursor.line, editor.cursor.col);
editor.cursor.line + 1, editor.cursor.col + 1);
bbptr += w;
bb_remaining -= w;
w = snprintf (bbptr, bb_remaining, ANSIQ_DYN_CUR_SET ANSIQ_CUR_VISIBLE,
(int)(editor.cursor.line - editor.row_offset) + 1,
(int)(editor.cursor.col - editor.col_offset) + 1);
(int)(editor.cursor.col - editor.col_offset) + 1 + (int)gutter_width + 1);
bbptr += w;
bb_remaining -= w;
@@ -274,6 +299,7 @@ void edit_start (const char* file_path, const char* text) {
break;
}
editor.total_lines = 0;
arena_reset (&temp_arena);
}