sled

Једноставни уређивач текста
git clone https://git.sr.ht/~strahinja/sled
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување 5a103eaadcbd5615148b49c5819a4310fc0c8fb9
родитељ 0fd235e85fd42c445c2272162e34063618843a11
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Sat, 29 Jun 2024 19:06:26 +0200

draw.c: Add support for line numbers

Diffstat:
Mdraw.c | 60++++++++++++++++++++++++++++++++++++++++++++----------------
измењених датотека: 1, додавања: 44(+), брисања: 16(-)

diff --git a/draw.c b/draw.c @@ -317,10 +317,15 @@ draw_screen(const struct DrawState* state) /* Selection end row/col */ int ser = 0; int sec = 0; + int maxx; if (!state) return 1; + maxx = state->maxx; + if (line_numbers) + maxx -= line_numbers_width; + if (state->rows_count == 0) return 0; @@ -354,16 +359,40 @@ draw_screen(const struct DrawState* state) } } + if (line_numbers) + { + char numbuf[LINE_DEFAULT]; + char* numbuf_end = NULL; + char* pnumbuf = NULL; + snprintf(numbuf, LINE_DEFAULT, "%d", i + 1); + numbuf_end = strchr(numbuf, 0); + if (numbuf_end) + { + pnumbuf = numbuf_end - numbuf + > line_numbers_width + ? numbuf_end - line_numbers_width + : numbuf; + draw_string(0, cy, LINENO_FG, LINENO_BG, + pnumbuf, numbuf_end - numbuf, + line_numbers_width, 0, 0, RIGHT, 0, 1, + NULL, 0, 0, 0, 0, 0, 0); + } + } if (current->text) - u32_draw_string(0, cy, TEXT_FG, TEXT_BG, current->text, - state->maxx, state->maxx, 0, 0, CENTER, - state->first_shown.col, 0, state->search, - SEARCH_FG, SEARCH_BG, ssi, sei, SEL_FG, SEL_BG); + u32_draw_string(line_numbers ? line_numbers_width + 2 + : 0, + cy, TEXT_FG, TEXT_BG, current->text, maxx, maxx, + 0, 0, CENTER, state->first_shown.col, 0, + state->search, SEARCH_FG, SEARCH_BG, ssi, sei, + SEL_FG, SEL_BG); i++; cy++; } - tb_set_cursor(state->display_column - state->first_shown.col, + tb_set_cursor(line_numbers + ? line_numbers_width + 2 + state->display_column + - state->first_shown.col + : state->display_column - state->first_shown.col, state->coord.row - state->first_shown.row); return 0; @@ -372,38 +401,37 @@ draw_screen(const struct DrawState* state) int draw_status(const struct DrawState* state) { + const struct StatusSegment* pseg; char* buf = NULL; ssize_t seg_size; int current_start = 0; - const struct StatusSegment* pseg; + int maxx = state->maxx; if (!state) return 1; - seg_size = state->maxx / LEN(status_segments); + seg_size = maxx / LEN(status_segments); pseg = status_segments; if (IN(state->mode, M_PROMPT) && *state->prompt) { if (draw_string(0, state->maxy - 1, PROMPT_FG, PROMPT_BG, - state->prompt, strlen(state->prompt) + 2, - state->maxx, 1, 1, CENTER, 0, 1, NULL, 0, 0, 0, 0, - 0, 0)) + state->prompt, strlen(state->prompt) + 2, maxx, 1, + 1, CENTER, 0, 1, NULL, 0, 0, 0, 0, 0, 0)) return 1; } else if (state->action && *state->action) { if (draw_string(0, state->maxy - 1, ACTION_FG, ACTION_BG, - state->action, strlen(state->action) + 2, - state->maxx, 1, 1, CENTER, 0, 1, NULL, 0, 0, 0, 0, - 0, 0)) + state->action, strlen(state->action) + 2, maxx, 1, + 1, CENTER, 0, 1, NULL, 0, 0, 0, 0, 0, 0)) return 1; } else if (state->error && *state->error) { if (draw_string(0, state->maxy - 1, ERROR_FG, ERROR_BG, - state->error, strlen(state->error) + 2, state->maxx, - 1, 1, CENTER, 0, 1, NULL, 0, 0, 0, 0, 0, 0)) + state->error, strlen(state->error) + 2, maxx, 1, 1, + CENTER, 0, 1, NULL, 0, 0, 0, 0, 0, 0)) return 1; } else @@ -424,7 +452,7 @@ draw_status(const struct DrawState* state) memccpy(buf, pseg->format, 0, limits[L_LINE_MAX]); if (pseg == status_segments + LEN(status_segments) - 1) - seg_size = state->maxx - 1 - current_start; + seg_size = maxx - 1 - current_start; if (draw_string(current_start, state->maxy - 1, pseg->fg, pseg->bg, buf, MIN(seg_size, (ssize_t)(strlen(buf) + 2)),