From 7aff4e898613279b26bff0755f58646c7cc44d13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Benjara?= Date: Thu, 5 Sep 2024 10:52:15 +0300 Subject: [PATCH] feat: highlight numbers by coloring each digit red --- Makefile | 1 + src/eve.h | 19 ++++++--- src/main.c | 5 ++- src/output/output_2.c | 40 +++++++++++++++++-- src/row_operations/row_operations.c | 4 +- src/row_operations/row_operations_2.c | 3 +- src/syntax_highlighting.c | 55 +++++++++++++++++++++++++++ 7 files changed, 114 insertions(+), 13 deletions(-) create mode 100644 src/syntax_highlighting.c diff --git a/Makefile b/Makefile index eedbc1a..66bf1f2 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,7 @@ SRC_FILES := main.c \ editor_operations.c \ find.c \ options.c \ + syntax_highlighting.c \ file/file_io.c \ file/save_file_operations.c \ input/input.c \ diff --git a/src/eve.h b/src/eve.h index 2c2092a..be875a4 100644 --- a/src/eve.h +++ b/src/eve.h @@ -3,7 +3,7 @@ /* eve.h */ /* */ /* Created: 2023/11/27 17:17:10 by cezelot */ -/* Updated: 2024/08/19 18:13:42 by cezelot */ +/* Updated: 2024/09/05 08:07:47 by cezelot */ /* */ /* Copyright (C) 2024 Ismael Benjara, Alberto Rodriguez */ /* */ @@ -69,11 +69,17 @@ enum e_editor_key { PAGE_DOWN }; +enum e_editor_highlight { + HL_NORMAL = 0, + HL_NUMBER +}; + typedef struct s_editor_row { - char *chars; - char *render; - int size; - int rsize; + int size; + int rsize; + char *chars; + char *render; + unsigned char *hl; } t_erow; typedef struct s_editor_config { @@ -128,6 +134,9 @@ int write_file(int fd, int len, char *buf, t_env *env); void find(t_env *env); // ---------------------------------------------------------------- options.c -- void parse_options(int ac, char **av, int *option_index); +// ---------------------------------------------------- syntax_highlighting.c -- +int syntax_to_color(int hl); +void update_syntax(t_erow *row); // ------------------------------------------------------------------ input.c -- void move_cursor(t_env *env, int key); void handle_keypress(t_env *env); diff --git a/src/main.c b/src/main.c index 5c4e147..7b52aec 100644 --- a/src/main.c +++ b/src/main.c @@ -3,7 +3,7 @@ /* eve - simple terminal-based text editor */ /* */ /* Created: 2023/11/26 12:20:29 by cezelot */ -/* Updated: 2024/08/17 18:47:42 by alberrod */ +/* Updated: 2024/09/05 08:50:15 by cezelot */ /* */ /* Copyright (C) 2024 Ismael Benjara */ /* */ @@ -49,7 +49,8 @@ close_editor(t_env *env) free(env->filename); while (i < env->numrows) { free(env->row[i].chars); - free(env->row[i++].render); + free(env->row[i].render); + free(env->row[i++].hl); } free(env->row); } diff --git a/src/output/output_2.c b/src/output/output_2.c index 10f8348..58fb6c8 100644 --- a/src/output/output_2.c +++ b/src/output/output_2.c @@ -3,7 +3,7 @@ /* output_2.c */ /* */ /* Created: 2024/05/29 10:19:15 by cezelot */ -/* Updated: 2024/05/30 18:25:02 by cezelot */ +/* Updated: 2024/09/05 10:25:30 by cezelot */ /* */ /* Copyright (C) 2024 Ismael Benjara */ /* */ @@ -37,11 +37,27 @@ set_status_message(t_env *env, const char *format, ...) env->statusmsg_time = time(NULL); } +static void +print_color_escape_sequence(t_abuf *abuf, int color) +{ + char buf[16]; + int clen; + + clen = snprintf(buf, sizeof(buf), "\x1b[%dm", color); + abuf_append(abuf, buf, clen); +} + void display_text_buffer(t_env *env, t_abuf *abuf, int filerow) { - int len; + unsigned char *hl; + char *s; + int len; + int color = 0; + int current_color = -1; + hl = &env->row[filerow].hl[env->coloff]; + s = &env->row[filerow].render[env->coloff]; len = env->row[filerow].rsize - env->coloff; if (len < 0) { len = 0; @@ -49,7 +65,23 @@ display_text_buffer(t_env *env, t_abuf *abuf, int filerow) if (len > env->screencols) { len = env->screencols; } - abuf_append(abuf, &env->row[filerow].render[env->coloff], len); + for (int i = 0; i < len; ++i) { + if (hl[i] == HL_NORMAL) { + if (current_color != -1) { + abuf_append(abuf, "\x1b[39m", 5); + current_color = -1; + } + abuf_append(abuf, &s[i], 1); + } else { + color = syntax_to_color(hl[i]); + if (color != current_color) { + current_color = color; + print_color_escape_sequence(abuf, current_color); + } + abuf_append(abuf, &s[i], 1); + } + } + abuf_append(abuf, "\x1b[39m", 5); } void @@ -60,7 +92,7 @@ display_welcome_message(t_env *env, t_abuf *abuf) int padding; welcomelen = snprintf(welcome, sizeof(welcome), - "eve editor -- version %s", VERSION); + "eve - version %s", VERSION); if (welcomelen > env->screencols) { welcomelen = env->screencols; } diff --git a/src/row_operations/row_operations.c b/src/row_operations/row_operations.c index 9806f63..1de32d1 100644 --- a/src/row_operations/row_operations.c +++ b/src/row_operations/row_operations.c @@ -3,7 +3,7 @@ /* row_operations.c */ /* */ /* Created: 2023/12/31 18:08:27 by cezelot */ -/* Updated: 2024/07/23 19:07:08 by cezelot */ +/* Updated: 2024/09/01 16:06:43 by cezelot */ /* */ /* Copyright (C) 2024 Ismael Benjara */ /* */ @@ -74,6 +74,7 @@ update_row(t_erow *row) } row->render[i] = '\0'; row->rsize = i; + update_syntax(row); } /* Insert the character C into CHARS, at position INDEX, @@ -120,6 +121,7 @@ insert_row(t_env *env, char *str, size_t len, int index) env->row[index].chars[len] = '\0'; env->row[index].rsize = 0; env->row[index].render = NULL; + env->row[index].hl = NULL; update_row(&env->row[index]); env->numrows++; env->dirty++; diff --git a/src/row_operations/row_operations_2.c b/src/row_operations/row_operations_2.c index df4720b..0e800cf 100644 --- a/src/row_operations/row_operations_2.c +++ b/src/row_operations/row_operations_2.c @@ -3,7 +3,7 @@ /* row_operations_2.c */ /* */ /* Created: 2024/07/22 11:34:50 by cezelot */ -/* Updated: 2024/07/27 14:50:42 by cezelot */ +/* Updated: 2024/09/01 10:20:29 by cezelot */ /* */ /* Copyright (C) 2024 Ismael Benjara */ /* */ @@ -78,6 +78,7 @@ free_row(t_erow *row) { free(row->render); free(row->chars); + free(row->hl); } /* Delete ROW at INDEX. */ diff --git a/src/syntax_highlighting.c b/src/syntax_highlighting.c new file mode 100644 index 0000000..4c3427c --- /dev/null +++ b/src/syntax_highlighting.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* syntax_highlighting.c */ +/* */ +/* Created: 2024/09/01 15:58:19 by cezelot */ +/* Updated: 2024/09/01 16:17:10 by cezelot */ +/* */ +/* Copyright (C) 2024 Ismael Benjara */ +/* */ +/* This file is part of eve. */ +/* */ +/* eve is free software: you can redistribute it and/or modify */ +/* it under the terms of the GNU General Public License as published by */ +/* the Free Software Foundation, either version 3 of the License, or */ +/* (at your option) any later version. */ +/* */ +/* eve is distributed in the hope that it will be useful, */ +/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ +/* GNU General Public License for more details. */ +/* */ +/* You should have received a copy of the GNU General Public License */ +/* along with eve. If not, see . */ +/* */ +/* ************************************************************************** */ + +#include "eve.h" + +/* Highlight the characters of ROW. */ +void +update_syntax(t_erow *row) +{ + int i = 0; + + row->hl = realloc(row->hl, row->rsize); + if (row->hl == NULL) + return ; + memset(row->hl, HL_NORMAL, row->rsize); + while (i < row->rsize) { + if (isdigit(row->render[i])) { + row->hl[i] = HL_NUMBER; + } + ++i; + } +} + +/* Map values in hl to ANSI color codes. */ +int +syntax_to_color(int hl) +{ + if (hl == HL_NUMBER) { + return (31); + } + return (37); +}