Improve rendering logic

This commit is contained in:
Night Kaly 2024-11-12 02:39:51 +00:00
parent 71312ab340
commit cca7880c49
Signed by: night0721
SSH key fingerprint: SHA256:B/hgVwUoBpx5vdNsXl9w8XwZljA9766uk6T4ubZp5HM

132
vip.c
View file

@ -25,7 +25,6 @@ editor_t *cur_editor;
void draw_status_bar(void); void draw_status_bar(void);
void refresh_screen(void); void refresh_screen(void);
void draw_rows(void);
void move_xy(int key); void move_xy(int key);
void insert_char(int c); void insert_char(int c);
void insert_new_line(void); void insert_new_line(void);
@ -44,7 +43,6 @@ void del_row(int at);
char *export_buffer(int *buflen); char *export_buffer(int *buflen);
int is_separator(int c); int is_separator(int c);
void update_highlight(row_t *row); void update_highlight(row_t *row);
char *syntax_to_color(int hl, size_t *len);
void select_syntax(void); void select_syntax(void);
void die(const char *s); void die(const char *s);
void cleanup(void); void cleanup(void);
@ -580,31 +578,64 @@ void draw_rows(void)
char *c = &cur_editor->row[filerow].render[cur_editor->coloff]; char *c = &cur_editor->row[filerow].render[cur_editor->coloff];
unsigned char *hl = &cur_editor->row[filerow].hl[cur_editor->coloff]; unsigned char *hl = &cur_editor->row[filerow].hl[cur_editor->coloff];
char *current_color = malloc(COLOR_LEN * 2);
for (int j = 0; j < len; j++) { for (int j = 0; j < len; j++) {
if (iscntrl(c[j])) { if (iscntrl(c[j])) {
bprintf("\033[7m^%c\033[m", '@' + c[j]); bprintf("%s%c\033[m", OVERLAY_2_BG, '@' + c[j]);
if (strncmp(current_color, WHITE_BG, COLOR_LEN)) {
bprintf(current_color);
}
} else if (hl[j] == NORMAL) { } else if (hl[j] == NORMAL) {
if (strncmp(current_color, WHITE_BG, COLOR_LEN)) { bprintf("%s%c", WHITE_BG, c[j]);
memcpy(current_color, WHITE_BG, COLOR_LEN);
bprintf(WHITE_BG);
}
bprintf("%c", c[j]);
} else { } else {
size_t len; switch (hl[j]) {
char *color = syntax_to_color(hl[j], &len); case NUMBER:
if (strncmp(current_color, color, len)) { bprintf(PEACH_BG);
memcpy(current_color, color, len); break;
bprintf(color);
case STRING:
bprintf(GREEN_BG);
break;
case SYMBOL:
bprintf(SKY_BG);
break;
case COMMENT:
case MLCOMMENT:
bprintf("\033[3m");
bprintf(OVERLAY_2_BG);
bprintf("\033[23m");
break;
case KW:
bprintf(MAUVE_BG);
break;
case KW_TYPE:
bprintf(YELLOW_BG);
break;
case KW_FN:
bprintf(BLUE_BG);
break;
case KW_BRACKET:
bprintf(RED_BG);
break;
case MATCH:
bprintf(BLACK_BG);
bprintf(SKY_FG);
break;
case RESET:
bprintf(BLACK_BG);
bprintf(SKY_FG);
break;
default:
bprintf(WHITE_BG);
} }
free(color);
bprintf("%c", c[j]); bprintf("%c", c[j]);
} }
} }
free(current_color);
bprintf(WHITE_BG); bprintf(WHITE_BG);
} }
@ -791,7 +822,7 @@ void update_highlight(row_t *row)
continue; continue;
} else { } else {
if (c == '<' && (row->render[i-1] == 'e' || (row->render[i-1] == ' ' && if (c == '<' && (row->render[i-1] == 'e' || (row->render[i-1] == ' ' &&
row->render[i-2] == 'e'))) { row->render[i-2] == 'e'))) {
in_include = 1; in_include = 1;
row->hl[i] = STRING; row->hl[i] = STRING;
i++; i++;
@ -803,7 +834,7 @@ void update_highlight(row_t *row)
(c == '.' && prev_hl == NUMBER) || (c == '.' && prev_hl == NUMBER) ||
(c >= 'A' && c <= 'Z') || (c >= 'A' && c <= 'Z') ||
(c == '_' && prev_hl == NUMBER) (c == '_' && prev_hl == NUMBER)
) { ) {
row->hl[i] = NUMBER; row->hl[i] = NUMBER;
i++; i++;
prev_sep = 0; prev_sep = 0;
@ -869,65 +900,6 @@ void update_highlight(row_t *row)
update_highlight(&cur_editor->row[row->idx + 1]); update_highlight(&cur_editor->row[row->idx + 1]);
} }
char *syntax_to_color(int hl, size_t *len)
{
switch (hl) {
case NUMBER:
*len = COLOR_LEN;
return strdup(PEACH_BG);
case STRING:
*len = COLOR_LEN;
return strdup(GREEN_BG);
case SYMBOL:
*len = COLOR_LEN;
return strdup(SKY_BG);
case COMMENT:;
case MLCOMMENT:;
/*
char *color = malloc(COLOR_LEN * 2 + 1);
snprintf(color, COLOR_LEN * 2 + 1, "\033[3m%s\033[23m", OVERLAY_2_BG);
*len = COLOR_LEN * 2;
return color;*/
*len = COLOR_LEN;
return strdup(OVERLAY_2_BG);
case KW:
*len = COLOR_LEN;
return strdup(MAUVE_BG);
case KW_TYPE:
*len = COLOR_LEN;
return strdup(YELLOW_BG);
case KW_FN:
*len = COLOR_LEN;
return strdup(BLUE_BG);
case KW_BRACKET:
*len = COLOR_LEN;
return strdup(RED_BG);
case MATCH:;
char *str = malloc(COLOR_LEN * 2 + 1);
snprintf(str, COLOR_LEN * 2 + 1, "%s%s", BLACK_BG, SKY_FG);
*len = COLOR_LEN * 2;
return str;
case RESET:;
char *res = malloc(COLOR_LEN * 2 + 1);
snprintf(res, COLOR_LEN * 2 + 1, "%s%s", WHITE_BG, BLACK_FG);
*len = COLOR_LEN * 2;
return res;
default:
*len = COLOR_LEN;
return strdup(WHITE_BG);
}
}
void select_syntax(void) void select_syntax(void)
{ {
if (strlen(cur_editor->filename) == 0) return; if (strlen(cur_editor->filename) == 0) return;