sled

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

чување 504bf7d201667cf53f994ed32c0e7549ea4f7a44
родитељ df0ae1f93847d817fa90d45160adbef952f01d7d
Аутор: Страхиња Радић <sr@strahinja.org>
Датум:   Sat,  7 Sep 2024 11:55:04 +0200

Add struct Color and convert defines to it

Diffstat:
MTODO | 3---
MTODO.done | 3+++
Mconfig.def.h | 106+++++++++++++++++++++++++++++++++++--------------------------------------------
Mdraw.c | 84+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
Mdraw.h | 28++++++++++++++++++++++++++--
Msled.c | 2+-
измењених датотека: 6, додавања: 125(+), брисања: 101(-)

diff --git a/TODO b/TODO @@ -1,9 +1,6 @@ TODO ==== -[ ] Convert #define's in config.def.h to struct Color - [ ] Add struct Color to master branch - [~] Code optimization (struct coord...) diff --git a/TODO.done b/TODO.done @@ -1,6 +1,9 @@ Done or canceled todos ====================== +[x] Convert #define's in config.def.h to struct Color + [x] Add struct Color to master branch + [x] Check wrapping with C-J when the last line in a paragraph is longer than wrap width diff --git a/config.def.h b/config.def.h @@ -2,40 +2,33 @@ * any later version. Copyright (C) 2022-2024 Страхиња Радић. * See the file LICENSE for exact copyright and license details. */ -#define DLG_FG TB_BLACK -#define DLG_BG TB_WHITE -#define ERROR_FG TB_WHITE | TB_BOLD -#define ERROR_BG TB_RED -#define ACTION_FG TB_BLACK -#define ACTION_BG TB_WHITE -#define INPUT_FG TB_BLACK -#define INPUT_BG TB_WHITE -#define INPUT_EDIT_FG TB_WHITE -#define INPUT_EDIT_BG TB_BLACK -#define LINENO_FG TB_BLACK -#define LINENO_BG TB_CYAN -#define PROMPT_FG TB_YELLOW | TB_BOLD -#define PROMPT_BG TB_MAGENTA -#define SEARCH_FG TB_WHITE | TB_BOLD -#define SEARCH_BG TB_GREEN -#define SEL_FG TB_BLACK -#define SEL_BG TB_WHITE -#define SHADOW_FG TB_BLACK -#define SHADOW_BG TB_BLACK | TB_BOLD -#define STATUS_1_FG TB_WHITE | TB_BOLD -#define STATUS_1_BG TB_BLUE -#define STATUS_2_FG TB_YELLOW | TB_BOLD -#define STATUS_2_BG TB_BLUE -#define STATUS_3_FG TB_WHITE -#define STATUS_3_BG TB_BLUE -#define STATUS_4_FG TB_WHITE | TB_BOLD -#define STATUS_4_BG TB_BLUE -#define STATUS_5_FG TB_WHITE -#define STATUS_5_BG TB_BLUE -#define TEXT_FG TB_DEFAULT -#define TEXT_BG TB_DEFAULT -#define TITLE_FG TB_BLACK -#define TITLE_BG TB_WHITE +/* clang-format off */ +static const struct Color colors[] = { + [C_DLG] = { .fg = TB_BLACK, .bg = TB_WHITE }, + [C_ERROR] = { .fg = TB_WHITE | TB_BOLD, + .bg = TB_RED }, + [C_ACTION] = { .fg = TB_BLACK, .bg = TB_WHITE }, + [C_INPUT] = { .fg = TB_BLACK, .bg = TB_WHITE }, + [C_INPUT_EDIT] = { .fg = TB_WHITE, .bg = TB_BLACK }, + [C_LINENO] = { .fg = TB_BLACK, .bg = TB_CYAN }, + [C_PROMPT] = { .fg = TB_YELLOW | TB_BOLD, + .bg = TB_MAGENTA }, + [C_SEARCH] = { .fg = TB_WHITE | TB_BOLD, + .bg = TB_GREEN }, + [C_SEL] = { .fg = TB_BLACK, .bg = TB_WHITE }, + [C_SHADOW] = { .fg = TB_BLACK, .bg = TB_BLACK | TB_BOLD}, + [C_STATUS_1] = { .fg = TB_WHITE | TB_BOLD, + .bg = TB_BLUE }, + [C_STATUS_2] = { .fg = TB_YELLOW | TB_BOLD, + .bg = TB_BLUE }, + [C_STATUS_3] = { .fg = TB_WHITE, .bg = TB_BLUE }, + [C_STATUS_4] = { .fg = TB_WHITE | TB_BOLD, + .bg = TB_BLUE }, + [C_STATUS_5] = { .fg = TB_WHITE, .bg = TB_BLUE }, + [C_TEXT] = { .fg = TB_DEFAULT, .bg = TB_DEFAULT }, + [C_TITLE] = { .fg = TB_BLACK, .bg = TB_WHITE }, +}; +/* clang-format on */ extern const int backup_fail; extern const char* backup_suffix; @@ -240,30 +233,25 @@ static const struct Key command_keys[] = { /* clang-format off */ static const struct StatusSegment status_segments[] = { - {.format = "%s%s", - .callback = format_filename, - .fg = STATUS_1_FG, - .bg = STATUS_1_BG, - .alignment = LEFT}, - {.format = "%s", - .callback = format_mode, - .fg = STATUS_2_FG, - .bg = STATUS_2_BG, - .alignment = CENTER}, - {.format = "[%s]", - .callback = format_codep, - .fg = STATUS_3_FG, - .bg = STATUS_3_BG, - .alignment = CENTER}, - {.format = "+%d %d:%d (%d%%)", - .callback = format_position, - .fg = STATUS_4_FG, - .bg = STATUS_4_BG, - .alignment = CENTER}, - {.format = "F1=HELP", - .callback = NULL, - .fg = STATUS_5_FG, - .bg = STATUS_5_BG, - .alignment = RIGHT}, + {.format = "%s%s", + .callback = format_filename, + .color_index = C_STATUS_1, + .alignment = LEFT}, + {.format = "%s", + .callback = format_mode, + .color_index = C_STATUS_2, + .alignment = CENTER}, + {.format = "[%s]", + .callback = format_codep, + .color_index = C_STATUS_3, + .alignment = CENTER}, + {.format = "+%d %d:%d (%d%%)", + .callback = format_position, + .color_index = C_STATUS_4, + .alignment = CENTER}, + {.format = "F1=HELP", + .callback = NULL, + .color_index = C_STATUS_5, + .alignment = RIGHT}, }; /* clang-format on */ diff --git a/draw.c b/draw.c @@ -230,17 +230,20 @@ draw_box(const int startx, const int endx, const int starty, const int endy, for (x = startx + 1; x < endx - 1; x++) tb_set_cell(x, y, ' ', fg, bg); tb_set_cell(endx - 1, y, BORDER_VER, fg, bg); - tb_set_cell(endx, y, ' ', SHADOW_FG, SHADOW_BG); + tb_set_cell(endx, y, ' ', colors[C_SHADOW].fg, + colors[C_SHADOW].bg); } tb_set_cell(startx, endy - 2, BORDER_SW, fg, bg); for (x = startx + 1; x < endx - 1; x++) tb_set_cell(x, endy - 2, BORDER_HOR, fg, bg); tb_set_cell(endx - 1, endy - 2, BORDER_SE, fg, bg); - tb_set_cell(endx, endy - 2, ' ', SHADOW_FG, SHADOW_BG); + tb_set_cell(endx, endy - 2, ' ', colors[C_SHADOW].fg, + colors[C_SHADOW].bg); for (x = startx + 1; x < endx + 1; x++) - tb_set_cell(x, endy - 1, ' ', SHADOW_FG, SHADOW_BG); + tb_set_cell(x, endy - 1, ' ', colors[C_SHADOW].fg, + colors[C_SHADOW].bg); } int @@ -264,16 +267,16 @@ draw_help(const struct DrawState* state) starty = h > maxy - 1 ? 0 : (maxy - h) / 2; endy = h > maxy - 1 ? maxy - 2 : starty + h; - draw_box(startx, endx, starty, endy, DLG_FG, DLG_BG); - if (draw_string(startx, starty, TITLE_FG, TITLE_BG, help_title, - strlen(help_title) + 2, w, 1, 1, CENTER, 0, 0, NULL, 0, 0, - 0, 0, 0, 0)) + draw_box(startx, endx, starty, endy, colors[C_DLG].fg, colors[C_DLG].bg); + if (draw_string(startx, starty, colors[C_TITLE].fg, colors[C_TITLE].bg, + help_title, strlen(help_title) + 2, w, 1, 1, CENTER, 0, 0, + NULL, 0, 0, 0, 0, 0, 0)) return 1; while ((size_t)row < LEN(help)) { - if (draw_string(startx + 1, starty + 1 + row, DLG_FG, DLG_BG, - help[row], w - 2, w - 2, 1, 1, LEFT, 0, 1, NULL, 0, - 0, 0, 0, 0, 0)) + if (draw_string(startx + 1, starty + 1 + row, colors[C_DLG].fg, + colors[C_DLG].bg, help[row], w - 2, w - 2, 1, 1, + LEFT, 0, 1, NULL, 0, 0, 0, 0, 0, 0)) return 1; row++; } @@ -303,20 +306,22 @@ draw_input_dialog(const struct DrawState* state) starty = maxy - 1 - h; endy = maxy - 1; - draw_box(startx, endx, starty, endy, INPUT_FG, INPUT_BG); - draw_rect(startx + 1, endx - 1, starty + 1, endy - 2, INPUT_EDIT_FG, - INPUT_EDIT_BG); + draw_box(startx, endx, starty, endy, colors[C_INPUT].fg, + colors[C_INPUT].bg); + draw_rect(startx + 1, endx - 1, starty + 1, endy - 2, + colors[C_INPUT_EDIT].fg, colors[C_INPUT_EDIT].bg); if (state->input_title) - if (draw_string(startx, starty, TITLE_FG, TITLE_BG, - state->input_title, strlen(state->input_title) + 2, - w, 1, 1, CENTER, 0, 0, NULL, 0, 0, 0, 0, 0, 0)) + if (draw_string(startx, starty, colors[C_TITLE].fg, + colors[C_TITLE].bg, state->input_title, + strlen(state->input_title) + 2, w, 1, 1, CENTER, 0, + 0, NULL, 0, 0, 0, 0, 0, 0)) return 1; dlen = display_length(state->input, len); if (dlen > state->input_first_shown_column) - u32_draw_string(startx + 1, starty + 1, INPUT_EDIT_FG, - INPUT_EDIT_BG, state->input, w - 2, w - 2, 1, 1, LEFT, - state->input_first_shown_column, 1, NULL, 0, 0, 0, 0, 0, - 0); + u32_draw_string(startx + 1, starty + 1, colors[C_INPUT_EDIT].fg, + colors[C_INPUT_EDIT].bg, state->input, w - 2, w - 2, 1, + 1, LEFT, state->input_first_shown_column, 1, NULL, 0, 0, + 0, 0, 0, 0); tb_set_cursor(startx + 2 + state->input_display_column - state->input_first_shown_column, starty + 1); @@ -337,9 +342,10 @@ draw_line_number(const struct DrawState* state, const int i, const int cy) pnumbuf = numbuf_end - numbuf > state->line_numbers_width ? numbuf_end - state->line_numbers_width : numbuf; - draw_string(0, cy, LINENO_FG, LINENO_BG, pnumbuf, - numbuf_end - numbuf + 1, state->line_numbers_width, 0, - 0, RIGHT, 0, 1, NULL, 0, 0, 0, 0, 0, 0); + draw_string(0, cy, colors[C_LINENO].fg, colors[C_LINENO].bg, + pnumbuf, numbuf_end - numbuf + 1, + state->line_numbers_width, 0, 0, RIGHT, 0, 1, NULL, 0, + 0, 0, 0, 0, 0); } } @@ -417,9 +423,11 @@ draw_screen(const struct DrawState* state) u32_draw_string(state->line_numbers ? state->line_numbers_width + 2 : 0, - cy, TEXT_FG, TEXT_BG, current->text, maxx, maxx, - 0, 0, LEFT, fsc, 0, state->search, SEARCH_FG, - SEARCH_BG, ssi, sei, SEL_FG, SEL_BG); + cy, colors[C_TEXT].fg, colors[C_TEXT].bg, + current->text, maxx, maxx, 0, 0, LEFT, fsc, 0, + state->search, colors[C_SEARCH].fg, + colors[C_SEARCH].bg, ssi, sei, colors[C_SEL].fg, + colors[C_SEL].bg); i++; cy++; } @@ -448,23 +456,26 @@ draw_status(const struct DrawState* state) 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, maxx, 1, - 1, CENTER, 0, 1, NULL, 0, 0, 0, 0, 0, 0)) + if (draw_string(0, state->maxy - 1, colors[C_PROMPT].fg, + colors[C_PROMPT].bg, 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, maxx, 1, - 1, CENTER, 0, 1, NULL, 0, 0, 0, 0, 0, 0)) + if (draw_string(0, state->maxy - 1, colors[C_ACTION].fg, + colors[C_ACTION].bg, 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, maxx, 1, 1, - CENTER, 0, 1, NULL, 0, 0, 0, 0, 0, 0)) + if (draw_string(0, state->maxy - 1, colors[C_ERROR].fg, + colors[C_ERROR].bg, state->error, + strlen(state->error) + 2, maxx, 1, 1, CENTER, 0, 1, + NULL, 0, 0, 0, 0, 0, 0)) return 1; } else @@ -487,7 +498,8 @@ draw_status(const struct DrawState* state) if (pseg == status_segments + LEN(status_segments) - 1) seg_size = maxx - 1 - current_start; if (draw_string(current_start, state->maxy - 1, - pseg->fg, pseg->bg, buf, + colors[pseg->color_index].fg, + colors[pseg->color_index].bg, buf, MIN(seg_size, (ssize_t)(strlen(buf) + 2)), last ? seg_size + 1 : seg_size, 1, 1, pseg->alignment, 0, 1, NULL, 0, 0, 0, 0, 0, diff --git a/draw.h b/draw.h @@ -37,6 +37,31 @@ enum { typedef enum { LEFT, CENTER, RIGHT } Alignment; +typedef enum { + C_DLG, + C_ERROR, + C_ACTION, + C_INPUT, + C_INPUT_EDIT, + C_LINENO, + C_PROMPT, + C_SEARCH, + C_SEL, + C_SHADOW, + C_STATUS_1, + C_STATUS_2, + C_STATUS_3, + C_STATUS_4, + C_STATUS_5, + C_TEXT, + C_TITLE +} ColorIndex; + +struct Color { + uint16_t fg; + uint16_t bg; +}; + struct Coord { int col; int row; @@ -104,8 +129,7 @@ struct StatusSegment { const char* format; void (*callback)(char*, ssize_t, const char*, const ssize_t, const struct DrawState*); - uint16_t fg; - uint16_t bg; + ColorIndex color_index; Alignment alignment; }; diff --git a/sled.c b/sled.c @@ -3216,7 +3216,7 @@ init_termbox: while (state.running) { - tb_set_clear_attrs(TEXT_FG, TEXT_BG); + tb_set_clear_attrs(colors[C_TEXT].fg, colors[C_TEXT].bg); tb_clear(); if (state.recalculate_size)